Skip to content

Commit 0c7a7ea

Browse files
authored
Merge pull request #26 from csesoc/feat/API
Added additional functionality as discussed in group meeting
2 parents 0cd6b7b + 05ad904 commit 0c7a7ea

File tree

4 files changed

+130
-119
lines changed

4 files changed

+130
-119
lines changed

server/category.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ import (
88
"go.mongodb.org/mongo-driver/mongo"
99
)
1010

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

17-
var result *Category
17+
var result Category
1818
filter := bson.D{{"categoryID", id}}
1919

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

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

3535
category := Category{
3636
categoryID: catID,
@@ -44,11 +44,11 @@ func NewCat(collection *mongo.Collection, catID int, index int, name string, tok
4444
}
4545
}
4646

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

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

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

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

server/post.go

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010
"go.mongodb.org/mongo-driver/mongo/options"
1111
)
1212

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

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

65-
// NewPost - Add a new post
66-
func NewPost(collection *mongo.Collection, id int, category int, showInMenu bool, title string, subtitle string, postType string, content string, github string, fb string) {
65+
// NewPosts - Add a new post
66+
func NewPosts(collection *mongo.Collection, id int, category int, showInMenu bool, title string, subtitle string, postType string, content string, github string, fb string) {
67+
currTime := time.Now()
6768
post := Post{
68-
postID: id,
69-
postTitle: title,
70-
postSubtitle: subtitle,
71-
postType: postType,
72-
postCategory: category,
73-
createdOn: time.Now(),
74-
lastEditedOn: time.Now(),
75-
postContent: content,
76-
postLinkGithub: github,
77-
postLinkFacebook: fb,
78-
showInMenu: showInMenu,
69+
PostID: id,
70+
PostTitle: title,
71+
PostSubtitle: subtitle,
72+
PostType: postType,
73+
PostCategory: category,
74+
CreatedOn: currTime.Unix(),
75+
LastEditedOn: currTime.Unix(),
76+
PostContent: content,
77+
PostLinkGithub: github,
78+
PostLinkFacebook: fb,
79+
ShowInMenu: showInMenu,
7980
}
8081

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

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

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

115116
// Find a post by id and delete it

server/server.go

Lines changed: 65 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"log"
66
"net/http"
77
"strconv"
8-
"time"
98

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

2120
// Post - struct to contain post data
2221
type Post struct {
23-
postID int
24-
postTitle string
25-
postSubtitle string
26-
postType string
27-
postCategory int
28-
createdOn time.Time
29-
lastEditedOn time.Time
30-
postContent string
31-
postLinkGithub string
32-
postLinkFacebook string
33-
showInMenu bool
22+
PostID int
23+
PostTitle string
24+
PostSubtitle string
25+
PostType string
26+
PostCategory int
27+
CreatedOn int64
28+
LastEditedOn int64
29+
PostContent string
30+
PostLinkGithub string
31+
PostLinkFacebook string
32+
ShowInMenu bool
3433
}
3534

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

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

120-
e.POST("/login/", login(userCollection))
119+
// e.POST("/login/", login(userCollection))
121120

122121
// Routes for posts
123-
e.GET("/post/:id/", getPost(postsCollection))
124-
e.GET("/posts/", getAllPosts(postsCollection))
125-
e.POST("/post/", newPost(postsCollection))
126-
e.PUT("/post/:id/", updatePost(postsCollection))
127-
e.DELETE("/post/:id/", deletePost(postsCollection))
122+
e.GET("/posts/", getPosts(postsCollection))
123+
e.POST("/post/", newPosts(postsCollection))
124+
e.PUT("/post/", updatePosts(postsCollection))
125+
e.DELETE("/post/", deletePosts(postsCollection))
128126

129127
// Routes for categories
130-
e.GET("/category/:id/", getCat(catCollection))
131-
e.POST("/category/", newCat(catCollection))
132-
e.PATCH("/category/", patchCat(catCollection))
133-
e.DELETE("/category/", deleteCat(catCollection))
128+
e.GET("/category/:id/", getCats(catCollection))
129+
e.POST("/category/", newCats(catCollection))
130+
e.PATCH("/category/", patchCats(catCollection))
131+
e.DELETE("/category/", deleteCats(catCollection))
134132

135133
// Routes for sponsors
136-
e.POST("/sponsor/", newSponsor(sponsorCollection))
137-
e.DELETE("/sponsor/", deleteSponsor(sponsorCollection))
134+
e.POST("/sponsor/", newSponsors(sponsorCollection))
135+
e.DELETE("/sponsor/", deleteSponsors(sponsorCollection))
138136
}
139137

140-
func login(collection *mongo.Collection) echo.HandlerFunc {
138+
// func login(collection *mongo.Collection) echo.HandlerFunc {
139+
// return func(c echo.Context) error {
140+
// zid := c.FormValue("zid")
141+
// password := c.FormValue("password")
142+
// permissions := c.FormValue("permissions")
143+
// tokenString := Auth(collection, zid, password, permissions)
144+
// return c.JSON(http.StatusOK, H{
145+
// "token": tokenString,
146+
// })
147+
// }
148+
// }
149+
150+
func getPosts(collection *mongo.Collection) echo.HandlerFunc {
141151
return func(c echo.Context) error {
142-
zid := c.FormValue("zid")
143-
password := c.FormValue("password")
144-
permissions := c.FormValue("permissions")
145-
tokenString := Auth(collection, zid, password, permissions)
146-
return c.JSON(http.StatusOK, H{
147-
"token": tokenString,
148-
})
149-
}
150-
}
151-
152-
func getPost(collection *mongo.Collection) echo.HandlerFunc {
153-
return func(c echo.Context) error {
154-
id, _ := strconv.Atoi(c.QueryParam("id"))
152+
id := c.QueryParam("id")
153+
count, _ := strconv.Atoi(c.QueryParam("nPosts"))
155154
category := c.QueryParam("category")
156-
result := GetPost(collection, id, category)
155+
if id == "" {
156+
posts := GetAllPosts(collection, count, category)
157+
return c.JSON(http.StatusOK, H{
158+
"post": posts,
159+
})
160+
}
161+
162+
idInt, _ := strconv.Atoi(id)
163+
result := GetPosts(collection, idInt, category)
157164
return c.JSON(http.StatusOK, H{
158165
"post": result,
159166
})
160167
}
161168
}
162169

163-
func getAllPosts(collection *mongo.Collection) echo.HandlerFunc {
164-
return func(c echo.Context) error {
165-
count, _ := strconv.Atoi(c.QueryParam("id"))
166-
cat := c.QueryParam("category")
167-
posts := GetAllPosts(collection, count, cat)
168-
return c.JSON(http.StatusOK, H{
169-
"posts": posts,
170-
})
171-
}
172-
}
173-
174-
func newPost(collection *mongo.Collection) echo.HandlerFunc {
170+
func newPosts(collection *mongo.Collection) echo.HandlerFunc {
175171
return func(c echo.Context) error {
176172
id, _ := strconv.Atoi(c.FormValue("id"))
177173
category, _ := strconv.Atoi(c.FormValue("category"))
@@ -182,12 +178,12 @@ func newPost(collection *mongo.Collection) echo.HandlerFunc {
182178
content := c.FormValue("content")
183179
github := c.FormValue("linkGithub")
184180
fb := c.FormValue("linkFacebook")
185-
NewPost(collection, id, category, showInMenu, title, subtitle, postType, content, github, fb)
181+
NewPosts(collection, id, category, showInMenu, title, subtitle, postType, content, github, fb)
186182
return c.JSON(http.StatusOK, H{})
187183
}
188184
}
189185

190-
func updatePost(collection *mongo.Collection) echo.HandlerFunc {
186+
func updatePosts(collection *mongo.Collection) echo.HandlerFunc {
191187
return func(c echo.Context) error {
192188
id, _ := strconv.Atoi(c.FormValue("id"))
193189
category, _ := strconv.Atoi(c.FormValue("category"))
@@ -198,78 +194,78 @@ func updatePost(collection *mongo.Collection) echo.HandlerFunc {
198194
content := c.FormValue("content")
199195
github := c.FormValue("linkGithub")
200196
fb := c.FormValue("linkFacebook")
201-
UpdatePost(collection, id, category, showInMenu, title, subtitle, postType, content, github, fb)
197+
UpdatePosts(collection, id, category, showInMenu, title, subtitle, postType, content, github, fb)
202198
return c.JSON(http.StatusOK, H{})
203199
}
204200
}
205201

206-
func deletePost(collection *mongo.Collection) echo.HandlerFunc {
202+
func deletePosts(collection *mongo.Collection) echo.HandlerFunc {
207203
return func(c echo.Context) error {
208204
id, _ := strconv.Atoi(c.FormValue("id"))
209-
DeletePost(collection, id)
205+
DeletePosts(collection, id)
210206
return c.JSON(http.StatusOK, H{})
211207
}
212208
}
213209

214-
func getCat(collection *mongo.Collection) echo.HandlerFunc {
210+
func getCats(collection *mongo.Collection) echo.HandlerFunc {
215211
return func(c echo.Context) error {
216212
token := c.FormValue("token")
217213
id, _ := strconv.Atoi(c.QueryParam("id"))
218-
result := GetCat(collection, id, token)
214+
result := GetCats(collection, id, token)
219215
return c.JSON(http.StatusOK, H{
220216
"category": result,
221217
})
222218
}
223219
}
224220

225-
func newCat(collection *mongo.Collection) echo.HandlerFunc {
221+
func newCats(collection *mongo.Collection) echo.HandlerFunc {
226222
return func(c echo.Context) error {
227223
token := c.FormValue("token")
228224
catID, _ := strconv.Atoi(c.FormValue("id"))
229225
index, _ := strconv.Atoi(c.FormValue("index"))
230226
name := c.FormValue("name")
231-
NewCat(collection, catID, index, name, token)
227+
NewCats(collection, catID, index, name, token)
232228
return c.JSON(http.StatusOK, H{})
233229
}
234230
}
235231

236-
func patchCat(collection *mongo.Collection) echo.HandlerFunc {
232+
func patchCats(collection *mongo.Collection) echo.HandlerFunc {
237233
return func(c echo.Context) error {
238234
token := c.FormValue("token")
239235
catID, _ := strconv.Atoi(c.FormValue("id"))
240236
name := c.FormValue("name")
241237
index, _ := strconv.Atoi(c.FormValue("index"))
242-
PatchCat(collection, catID, name, index, token)
238+
PatchCats(collection, catID, name, index, token)
243239
return c.JSON(http.StatusOK, H{})
244240
}
245241
}
246242

247-
func deleteCat(collection *mongo.Collection) echo.HandlerFunc {
243+
func deleteCats(collection *mongo.Collection) echo.HandlerFunc {
248244
return func(c echo.Context) error {
249245
token := c.FormValue("token")
250246
id, _ := strconv.Atoi(c.FormValue("id"))
251-
DeleteCat(collection, id, token)
247+
DeleteCats(collection, id, token)
252248
return c.JSON(http.StatusOK, H{})
253249
}
254250
}
255251

256-
func newSponsor(collection *mongo.Collection) echo.HandlerFunc {
252+
func newSponsors(collection *mongo.Collection) echo.HandlerFunc {
257253
return func(c echo.Context) error {
258254
token := c.FormValue("token")
259255
expiryStr := c.FormValue("expiry")
260256
name := c.FormValue("name")
261257
logo := c.FormValue("logo")
262258
tier := c.FormValue("tier")
263-
NewSponsor(collection, expiryStr, name, logo, tier, token)
259+
NewSponsors(collection, expiryStr, name, logo, tier, token)
264260
return c.JSON(http.StatusOK, H{})
265261
}
266262
}
267263

268-
func deleteSponsor(collection *mongo.Collection) echo.HandlerFunc {
264+
func deleteSponsors(collection *mongo.Collection) echo.HandlerFunc {
269265
return func(c echo.Context) error {
270266
token := c.FormValue("token")
271267
id := c.FormValue("id")
272-
DeleteSponsor(collection, id, token)
268+
DeleteSponsors(collection, id, token)
273269
return c.JSON(http.StatusOK, H{})
274270
}
275271
}

0 commit comments

Comments
 (0)