Skip to content

Commit ab2c9f3

Browse files
committed
Combined get post and get all posts, added get sponsors
1 parent 3f0cf82 commit ab2c9f3

File tree

4 files changed

+74
-70
lines changed

4 files changed

+74
-70
lines changed

server/category.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ 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 {
11+
// GetCats - Retrieve a category from the database
12+
func GetCats(collection *mongo.Collection, id int, token string) *Category {
1313
// if !validToken(token) {
1414
// return nil
1515
// }
@@ -26,8 +26,8 @@ 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) {
29+
// NewCats - Add a new category
30+
func NewCats(collection *mongo.Collection, catID int, index int, name string, token string) {
3131
// if !validToken(token) {
3232
// return
3333
// }
@@ -44,8 +44,8 @@ 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) {
47+
// PatchCats - Update a category with new information
48+
func PatchCats(collection *mongo.Collection, catID int, name string, index int, token string) {
4949
// if !validToken(token) {
5050
// return
5151
// }
@@ -65,8 +65,8 @@ 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) {
68+
// DeleteCats - Delete a category from the database
69+
func DeleteCats(collection *mongo.Collection, id int, token string) {
7070
// if !validToken(token) {
7171
// return
7272
// }

server/post.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ 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 {
13+
// GetPosts - Retrieve a post from the database
14+
func GetPosts(collection *mongo.Collection, id int, category string) *Post {
1515
var result *Post
1616

1717
// Search for post by id and category
@@ -62,8 +62,8 @@ 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) {
6767
currtime := time.Now()
6868
post := Post{
6969
postID: id,
@@ -85,8 +85,8 @@ func NewPost(collection *mongo.Collection, id int, category int, showInMenu bool
8585
}
8686
}
8787

88-
// UpdatePost - Update a post with new information
89-
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) {
9090
filter := bson.D{{"postID", id}}
9191
update := bson.D{
9292
{"$set", bson.D{
@@ -109,8 +109,8 @@ func UpdatePost(collection *mongo.Collection, id int, category int, showInMenu b
109109
}
110110
}
111111

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

116116
// Find a post by id and delete it

server/server.go

Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -119,21 +119,20 @@ func serveAPI(e *echo.Echo) {
119119
// e.POST("/login/", login(userCollection))
120120

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

128127
// Routes for categories
129-
e.GET("/category/:id/", getCat(catCollection))
130-
e.POST("/category/", newCat(catCollection))
131-
e.PATCH("/category/", patchCat(catCollection))
132-
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))
133132

134133
// Routes for sponsors
135-
e.POST("/sponsor/", newSponsor(sponsorCollection))
136-
e.DELETE("/sponsor/", deleteSponsor(sponsorCollection))
134+
e.POST("/sponsor/", newSponsors(sponsorCollection))
135+
e.DELETE("/sponsor/", deleteSponsors(sponsorCollection))
137136
}
138137

139138
// func login(collection *mongo.Collection) echo.HandlerFunc {
@@ -148,29 +147,27 @@ func serveAPI(e *echo.Echo) {
148147
// }
149148
// }
150149

151-
func getPost(collection *mongo.Collection) echo.HandlerFunc {
150+
func getPosts(collection *mongo.Collection) echo.HandlerFunc {
152151
return func(c echo.Context) error {
153-
id, _ := strconv.Atoi(c.QueryParam("id"))
152+
id := c.QueryParam("id")
153+
count, _ := strconv.Atoi(c.QueryParam("nPosts"))
154154
category := c.QueryParam("category")
155-
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)
156164
return c.JSON(http.StatusOK, H{
157165
"post": result,
158166
})
159167
}
160168
}
161169

162-
func getAllPosts(collection *mongo.Collection) echo.HandlerFunc {
163-
return func(c echo.Context) error {
164-
count, _ := strconv.Atoi(c.QueryParam("id"))
165-
cat := c.QueryParam("category")
166-
posts := GetAllPosts(collection, count, cat)
167-
return c.JSON(http.StatusOK, H{
168-
"posts": posts,
169-
})
170-
}
171-
}
172-
173-
func newPost(collection *mongo.Collection) echo.HandlerFunc {
170+
func newPosts(collection *mongo.Collection) echo.HandlerFunc {
174171
return func(c echo.Context) error {
175172
id, _ := strconv.Atoi(c.FormValue("id"))
176173
category, _ := strconv.Atoi(c.FormValue("category"))
@@ -181,12 +178,12 @@ func newPost(collection *mongo.Collection) echo.HandlerFunc {
181178
content := c.FormValue("content")
182179
github := c.FormValue("linkGithub")
183180
fb := c.FormValue("linkFacebook")
184-
NewPost(collection, id, category, showInMenu, title, subtitle, postType, content, github, fb)
181+
NewPosts(collection, id, category, showInMenu, title, subtitle, postType, content, github, fb)
185182
return c.JSON(http.StatusOK, H{})
186183
}
187184
}
188185

189-
func updatePost(collection *mongo.Collection) echo.HandlerFunc {
186+
func updatePosts(collection *mongo.Collection) echo.HandlerFunc {
190187
return func(c echo.Context) error {
191188
id, _ := strconv.Atoi(c.FormValue("id"))
192189
category, _ := strconv.Atoi(c.FormValue("category"))
@@ -197,78 +194,78 @@ func updatePost(collection *mongo.Collection) echo.HandlerFunc {
197194
content := c.FormValue("content")
198195
github := c.FormValue("linkGithub")
199196
fb := c.FormValue("linkFacebook")
200-
UpdatePost(collection, id, category, showInMenu, title, subtitle, postType, content, github, fb)
197+
UpdatePosts(collection, id, category, showInMenu, title, subtitle, postType, content, github, fb)
201198
return c.JSON(http.StatusOK, H{})
202199
}
203200
}
204201

205-
func deletePost(collection *mongo.Collection) echo.HandlerFunc {
202+
func deletePosts(collection *mongo.Collection) echo.HandlerFunc {
206203
return func(c echo.Context) error {
207204
id, _ := strconv.Atoi(c.FormValue("id"))
208-
DeletePost(collection, id)
205+
DeletePosts(collection, id)
209206
return c.JSON(http.StatusOK, H{})
210207
}
211208
}
212209

213-
func getCat(collection *mongo.Collection) echo.HandlerFunc {
210+
func getCats(collection *mongo.Collection) echo.HandlerFunc {
214211
return func(c echo.Context) error {
215212
token := c.FormValue("token")
216213
id, _ := strconv.Atoi(c.QueryParam("id"))
217-
result := GetCat(collection, id, token)
214+
result := GetCats(collection, id, token)
218215
return c.JSON(http.StatusOK, H{
219216
"category": result,
220217
})
221218
}
222219
}
223220

224-
func newCat(collection *mongo.Collection) echo.HandlerFunc {
221+
func newCats(collection *mongo.Collection) echo.HandlerFunc {
225222
return func(c echo.Context) error {
226223
token := c.FormValue("token")
227224
catID, _ := strconv.Atoi(c.FormValue("id"))
228225
index, _ := strconv.Atoi(c.FormValue("index"))
229226
name := c.FormValue("name")
230-
NewCat(collection, catID, index, name, token)
227+
NewCats(collection, catID, index, name, token)
231228
return c.JSON(http.StatusOK, H{})
232229
}
233230
}
234231

235-
func patchCat(collection *mongo.Collection) echo.HandlerFunc {
232+
func patchCats(collection *mongo.Collection) echo.HandlerFunc {
236233
return func(c echo.Context) error {
237234
token := c.FormValue("token")
238235
catID, _ := strconv.Atoi(c.FormValue("id"))
239236
name := c.FormValue("name")
240237
index, _ := strconv.Atoi(c.FormValue("index"))
241-
PatchCat(collection, catID, name, index, token)
238+
PatchCats(collection, catID, name, index, token)
242239
return c.JSON(http.StatusOK, H{})
243240
}
244241
}
245242

246-
func deleteCat(collection *mongo.Collection) echo.HandlerFunc {
243+
func deleteCats(collection *mongo.Collection) echo.HandlerFunc {
247244
return func(c echo.Context) error {
248245
token := c.FormValue("token")
249246
id, _ := strconv.Atoi(c.FormValue("id"))
250-
DeleteCat(collection, id, token)
247+
DeleteCats(collection, id, token)
251248
return c.JSON(http.StatusOK, H{})
252249
}
253250
}
254251

255-
func newSponsor(collection *mongo.Collection) echo.HandlerFunc {
252+
func newSponsors(collection *mongo.Collection) echo.HandlerFunc {
256253
return func(c echo.Context) error {
257254
token := c.FormValue("token")
258255
expiryStr := c.FormValue("expiry")
259256
name := c.FormValue("name")
260257
logo := c.FormValue("logo")
261258
tier := c.FormValue("tier")
262-
NewSponsor(collection, expiryStr, name, logo, tier, token)
259+
NewSponsors(collection, expiryStr, name, logo, tier, token)
263260
return c.JSON(http.StatusOK, H{})
264261
}
265262
}
266263

267-
func deleteSponsor(collection *mongo.Collection) echo.HandlerFunc {
264+
func deleteSponsors(collection *mongo.Collection) echo.HandlerFunc {
268265
return func(c echo.Context) error {
269266
token := c.FormValue("token")
270267
id := c.FormValue("id")
271-
DeleteSponsor(collection, id, token)
268+
DeleteSponsors(collection, id, token)
272269
return c.JSON(http.StatusOK, H{})
273270
}
274271
}

server/sponsor.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,22 @@ import (
1010
"go.mongodb.org/mongo-driver/mongo"
1111
)
1212

13-
// NewSponsor - Add a new sponsor
14-
func NewSponsor(collection *mongo.Collection, expiryStr string, name string, logo string, tier string, token string) {
13+
// GetSponsors - Retrieve a sponsor from the database
14+
func GetSponsors(collection *mongo.Collection, id string, token string) *Sponsor {
15+
parsedID := uuid.Must(uuid.Parse(id))
16+
17+
var result *Sponsor
18+
filter := bson.D{{"sponsorID", parsedID}}
19+
err := collection.FindOne(context.TODO(), filter).Decode(&result)
20+
if err != nil {
21+
log.Fatal(err)
22+
}
23+
24+
return result
25+
}
26+
27+
// NewSponsors - Add a new sponsor
28+
func NewSponsors(collection *mongo.Collection, expiryStr string, name string, logo string, tier string, token string) {
1529
// if !validToken(token) {
1630
// return
1731
// }
@@ -33,8 +47,8 @@ func NewSponsor(collection *mongo.Collection, expiryStr string, name string, log
3347
}
3448
}
3549

36-
// DeleteSponsor - Delete a sponsor from the database
37-
func DeleteSponsor(collection *mongo.Collection, id string, token string) {
50+
// DeleteSponsors - Delete a sponsor from the database
51+
func DeleteSponsors(collection *mongo.Collection, id string, token string) {
3852
// if !validToken(token) {
3953
// return
4054
// }
@@ -48,10 +62,3 @@ func DeleteSponsor(collection *mongo.Collection, id string, token string) {
4862
log.Fatal(err)
4963
}
5064
}
51-
52-
func GetSponsor(collection *mongo.Collection, id string, token string) {
53-
parsedID := uuid.Must(uuid.Parse(id))
54-
55-
filter := bson.D{{"sponsorID".parsedID}}
56-
_, err := collection.FindOne()
57-
}

0 commit comments

Comments
 (0)