Skip to content

Added additional functionality as discussed in group meeting #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions server/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
"go.mongodb.org/mongo-driver/mongo"
)

// GetCat - Retrieve a category from the database
func GetCat(collection *mongo.Collection, id int, token string) *Category {
if !validToken(token) {
return nil
}
// GetCats - Retrieve a category from the database
func GetCats(collection *mongo.Collection, id int, token string) Category {
// if !validToken(token) {
// return nil
// }

var result *Category
var result Category
filter := bson.D{{"categoryID", id}}

// Find a category
Expand All @@ -26,11 +26,11 @@ func GetCat(collection *mongo.Collection, id int, token string) *Category {
return result
}

// NewCat - Add a new category
func NewCat(collection *mongo.Collection, catID int, index int, name string, token string) {
if !validToken(token) {
return
}
// NewCats - Add a new category
func NewCats(collection *mongo.Collection, catID int, index int, name string, token string) {
// if !validToken(token) {
// return
// }

category := Category{
categoryID: catID,
Expand All @@ -44,11 +44,11 @@ func NewCat(collection *mongo.Collection, catID int, index int, name string, tok
}
}

// PatchCat - Update a category with new information
func PatchCat(collection *mongo.Collection, catID int, name string, index int, token string) {
if !validToken(token) {
return
}
// PatchCats - Update a category with new information
func PatchCats(collection *mongo.Collection, catID int, name string, index int, token string) {
// if !validToken(token) {
// return
// }

filter := bson.D{{"categoryID", catID}}
update := bson.D{
Expand All @@ -65,11 +65,11 @@ func PatchCat(collection *mongo.Collection, catID int, name string, index int, t
}
}

// DeleteCat - Delete a category from the database
func DeleteCat(collection *mongo.Collection, id int, token string) {
if !validToken(token) {
return
}
// DeleteCats - Delete a category from the database
func DeleteCats(collection *mongo.Collection, id int, token string) {
// if !validToken(token) {
// return
// }

filter := bson.D{{"categoryID", id}}

Expand Down
41 changes: 21 additions & 20 deletions server/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"go.mongodb.org/mongo-driver/mongo/options"
)

// GetPost - Retrieve a post from the database
func GetPost(collection *mongo.Collection, id int, category string) *Post {
var result *Post
// GetPosts - Retrieve a post from the database
func GetPosts(collection *mongo.Collection, id int, category string) Post {
var result Post

// Search for post by id and category
filter := bson.D{{"postID", id}, {"category", category}}
Expand Down Expand Up @@ -62,20 +62,21 @@ func GetAllPosts(collection *mongo.Collection, count int, cat string) []*Post {
return posts
}

// NewPost - Add a new post
func NewPost(collection *mongo.Collection, id int, category int, showInMenu bool, title string, subtitle string, postType string, content string, github string, fb string) {
// NewPosts - Add a new post
func NewPosts(collection *mongo.Collection, id int, category int, showInMenu bool, title string, subtitle string, postType string, content string, github string, fb string) {
currTime := time.Now()
post := Post{
postID: id,
postTitle: title,
postSubtitle: subtitle,
postType: postType,
postCategory: category,
createdOn: time.Now(),
lastEditedOn: time.Now(),
postContent: content,
postLinkGithub: github,
postLinkFacebook: fb,
showInMenu: showInMenu,
PostID: id,
PostTitle: title,
PostSubtitle: subtitle,
PostType: postType,
PostCategory: category,
CreatedOn: currTime.Unix(),
LastEditedOn: currTime.Unix(),
PostContent: content,
PostLinkGithub: github,
PostLinkFacebook: fb,
ShowInMenu: showInMenu,
}

_, err := collection.InsertOne(context.TODO(), post)
Expand All @@ -84,8 +85,8 @@ func NewPost(collection *mongo.Collection, id int, category int, showInMenu bool
}
}

// UpdatePost - Update a post with new information
func UpdatePost(collection *mongo.Collection, id int, category int, showInMenu bool, title string, subtitle string, postType string, content string, github string, fb string) {
// UpdatePosts - Update a post with new information
func UpdatePosts(collection *mongo.Collection, id int, category int, showInMenu bool, title string, subtitle string, postType string, content string, github string, fb string) {
filter := bson.D{{"postID", id}}
update := bson.D{
{"$set", bson.D{
Expand All @@ -108,8 +109,8 @@ func UpdatePost(collection *mongo.Collection, id int, category int, showInMenu b
}
}

// DeletePost - Delete a post from the database
func DeletePost(collection *mongo.Collection, id int) {
// DeletePosts - Delete a post from the database
func DeletePosts(collection *mongo.Collection, id int) {
filter := bson.D{{"postID", id}}

// Find a post by id and delete it
Expand Down
134 changes: 65 additions & 69 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"log"
"net/http"
"strconv"
"time"

"github.com/dgrijalva/jwt-go"
"github.com/google/uuid"
Expand All @@ -20,17 +19,17 @@ type H map[string]interface{}

// Post - struct to contain post data
type Post struct {
postID int
postTitle string
postSubtitle string
postType string
postCategory int
createdOn time.Time
lastEditedOn time.Time
postContent string
postLinkGithub string
postLinkFacebook string
showInMenu bool
PostID int
PostTitle string
PostSubtitle string
PostType string
PostCategory int
CreatedOn int64
LastEditedOn int64
PostContent string
PostLinkGithub string
PostLinkFacebook string
ShowInMenu bool
}

// Category - struct to contain category data
Expand Down Expand Up @@ -110,68 +109,65 @@ func serveAPI(e *echo.Echo) {
postsCollection := client.Database("csesoc").Collection("posts")
catCollection := client.Database("csesoc").Collection("categories")
sponsorCollection := client.Database("csesoc").Collection("sponsors")
userCollection := client.Database("csesoc").Collection("users")
// userCollection := client.Database("csesoc").Collection("users")

// Add more API routes here
e.GET("/api/v1/test", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})

e.POST("/login/", login(userCollection))
// e.POST("/login/", login(userCollection))

// Routes for posts
e.GET("/post/:id/", getPost(postsCollection))
e.GET("/posts/", getAllPosts(postsCollection))
e.POST("/post/", newPost(postsCollection))
e.PUT("/post/:id/", updatePost(postsCollection))
e.DELETE("/post/:id/", deletePost(postsCollection))
e.GET("/posts/", getPosts(postsCollection))
e.POST("/post/", newPosts(postsCollection))
e.PUT("/post/", updatePosts(postsCollection))
e.DELETE("/post/", deletePosts(postsCollection))

// Routes for categories
e.GET("/category/:id/", getCat(catCollection))
e.POST("/category/", newCat(catCollection))
e.PATCH("/category/", patchCat(catCollection))
e.DELETE("/category/", deleteCat(catCollection))
e.GET("/category/:id/", getCats(catCollection))
e.POST("/category/", newCats(catCollection))
e.PATCH("/category/", patchCats(catCollection))
e.DELETE("/category/", deleteCats(catCollection))

// Routes for sponsors
e.POST("/sponsor/", newSponsor(sponsorCollection))
e.DELETE("/sponsor/", deleteSponsor(sponsorCollection))
e.POST("/sponsor/", newSponsors(sponsorCollection))
e.DELETE("/sponsor/", deleteSponsors(sponsorCollection))
}

func login(collection *mongo.Collection) echo.HandlerFunc {
// func login(collection *mongo.Collection) echo.HandlerFunc {
// return func(c echo.Context) error {
// zid := c.FormValue("zid")
// password := c.FormValue("password")
// permissions := c.FormValue("permissions")
// tokenString := Auth(collection, zid, password, permissions)
// return c.JSON(http.StatusOK, H{
// "token": tokenString,
// })
// }
// }

func getPosts(collection *mongo.Collection) echo.HandlerFunc {
return func(c echo.Context) error {
zid := c.FormValue("zid")
password := c.FormValue("password")
permissions := c.FormValue("permissions")
tokenString := Auth(collection, zid, password, permissions)
return c.JSON(http.StatusOK, H{
"token": tokenString,
})
}
}

func getPost(collection *mongo.Collection) echo.HandlerFunc {
return func(c echo.Context) error {
id, _ := strconv.Atoi(c.QueryParam("id"))
id := c.QueryParam("id")
count, _ := strconv.Atoi(c.QueryParam("nPosts"))
category := c.QueryParam("category")
result := GetPost(collection, id, category)
if id == "" {
posts := GetAllPosts(collection, count, category)
return c.JSON(http.StatusOK, H{
"post": posts,
})
}

idInt, _ := strconv.Atoi(id)
result := GetPosts(collection, idInt, category)
return c.JSON(http.StatusOK, H{
"post": result,
})
}
}

func getAllPosts(collection *mongo.Collection) echo.HandlerFunc {
return func(c echo.Context) error {
count, _ := strconv.Atoi(c.QueryParam("id"))
cat := c.QueryParam("category")
posts := GetAllPosts(collection, count, cat)
return c.JSON(http.StatusOK, H{
"posts": posts,
})
}
}

func newPost(collection *mongo.Collection) echo.HandlerFunc {
func newPosts(collection *mongo.Collection) echo.HandlerFunc {
return func(c echo.Context) error {
id, _ := strconv.Atoi(c.FormValue("id"))
category, _ := strconv.Atoi(c.FormValue("category"))
Expand All @@ -182,12 +178,12 @@ func newPost(collection *mongo.Collection) echo.HandlerFunc {
content := c.FormValue("content")
github := c.FormValue("linkGithub")
fb := c.FormValue("linkFacebook")
NewPost(collection, id, category, showInMenu, title, subtitle, postType, content, github, fb)
NewPosts(collection, id, category, showInMenu, title, subtitle, postType, content, github, fb)
return c.JSON(http.StatusOK, H{})
}
}

func updatePost(collection *mongo.Collection) echo.HandlerFunc {
func updatePosts(collection *mongo.Collection) echo.HandlerFunc {
return func(c echo.Context) error {
id, _ := strconv.Atoi(c.FormValue("id"))
category, _ := strconv.Atoi(c.FormValue("category"))
Expand All @@ -198,78 +194,78 @@ func updatePost(collection *mongo.Collection) echo.HandlerFunc {
content := c.FormValue("content")
github := c.FormValue("linkGithub")
fb := c.FormValue("linkFacebook")
UpdatePost(collection, id, category, showInMenu, title, subtitle, postType, content, github, fb)
UpdatePosts(collection, id, category, showInMenu, title, subtitle, postType, content, github, fb)
return c.JSON(http.StatusOK, H{})
}
}

func deletePost(collection *mongo.Collection) echo.HandlerFunc {
func deletePosts(collection *mongo.Collection) echo.HandlerFunc {
return func(c echo.Context) error {
id, _ := strconv.Atoi(c.FormValue("id"))
DeletePost(collection, id)
DeletePosts(collection, id)
return c.JSON(http.StatusOK, H{})
}
}

func getCat(collection *mongo.Collection) echo.HandlerFunc {
func getCats(collection *mongo.Collection) echo.HandlerFunc {
return func(c echo.Context) error {
token := c.FormValue("token")
id, _ := strconv.Atoi(c.QueryParam("id"))
result := GetCat(collection, id, token)
result := GetCats(collection, id, token)
return c.JSON(http.StatusOK, H{
"category": result,
})
}
}

func newCat(collection *mongo.Collection) echo.HandlerFunc {
func newCats(collection *mongo.Collection) echo.HandlerFunc {
return func(c echo.Context) error {
token := c.FormValue("token")
catID, _ := strconv.Atoi(c.FormValue("id"))
index, _ := strconv.Atoi(c.FormValue("index"))
name := c.FormValue("name")
NewCat(collection, catID, index, name, token)
NewCats(collection, catID, index, name, token)
return c.JSON(http.StatusOK, H{})
}
}

func patchCat(collection *mongo.Collection) echo.HandlerFunc {
func patchCats(collection *mongo.Collection) echo.HandlerFunc {
return func(c echo.Context) error {
token := c.FormValue("token")
catID, _ := strconv.Atoi(c.FormValue("id"))
name := c.FormValue("name")
index, _ := strconv.Atoi(c.FormValue("index"))
PatchCat(collection, catID, name, index, token)
PatchCats(collection, catID, name, index, token)
return c.JSON(http.StatusOK, H{})
}
}

func deleteCat(collection *mongo.Collection) echo.HandlerFunc {
func deleteCats(collection *mongo.Collection) echo.HandlerFunc {
return func(c echo.Context) error {
token := c.FormValue("token")
id, _ := strconv.Atoi(c.FormValue("id"))
DeleteCat(collection, id, token)
DeleteCats(collection, id, token)
return c.JSON(http.StatusOK, H{})
}
}

func newSponsor(collection *mongo.Collection) echo.HandlerFunc {
func newSponsors(collection *mongo.Collection) echo.HandlerFunc {
return func(c echo.Context) error {
token := c.FormValue("token")
expiryStr := c.FormValue("expiry")
name := c.FormValue("name")
logo := c.FormValue("logo")
tier := c.FormValue("tier")
NewSponsor(collection, expiryStr, name, logo, tier, token)
NewSponsors(collection, expiryStr, name, logo, tier, token)
return c.JSON(http.StatusOK, H{})
}
}

func deleteSponsor(collection *mongo.Collection) echo.HandlerFunc {
func deleteSponsors(collection *mongo.Collection) echo.HandlerFunc {
return func(c echo.Context) error {
token := c.FormValue("token")
id := c.FormValue("id")
DeleteSponsor(collection, id, token)
DeleteSponsors(collection, id, token)
return c.JSON(http.StatusOK, H{})
}
}
Loading