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
2221type 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