5
5
"log"
6
6
"net/http"
7
7
"strconv"
8
- "time"
9
8
10
9
"github.com/dgrijalva/jwt-go"
11
10
"github.com/google/uuid"
@@ -20,17 +19,17 @@ type H map[string]interface{}
20
19
21
20
// Post - struct to contain post data
22
21
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
34
33
}
35
34
36
35
// Category - struct to contain category data
@@ -110,68 +109,65 @@ func serveAPI(e *echo.Echo) {
110
109
postsCollection := client .Database ("csesoc" ).Collection ("posts" )
111
110
catCollection := client .Database ("csesoc" ).Collection ("categories" )
112
111
sponsorCollection := client .Database ("csesoc" ).Collection ("sponsors" )
113
- userCollection := client .Database ("csesoc" ).Collection ("users" )
112
+ // userCollection := client.Database("csesoc").Collection("users")
114
113
115
114
// Add more API routes here
116
115
e .GET ("/api/v1/test" , func (c echo.Context ) error {
117
116
return c .String (http .StatusOK , "Hello, World!" )
118
117
})
119
118
120
- e .POST ("/login/" , login (userCollection ))
119
+ // e.POST("/login/", login(userCollection))
121
120
122
121
// 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 ))
128
126
129
127
// 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 ))
134
132
135
133
// 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 ))
138
136
}
139
137
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 {
141
151
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" ))
155
154
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 )
157
164
return c .JSON (http .StatusOK , H {
158
165
"post" : result ,
159
166
})
160
167
}
161
168
}
162
169
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 {
175
171
return func (c echo.Context ) error {
176
172
id , _ := strconv .Atoi (c .FormValue ("id" ))
177
173
category , _ := strconv .Atoi (c .FormValue ("category" ))
@@ -182,12 +178,12 @@ func newPost(collection *mongo.Collection) echo.HandlerFunc {
182
178
content := c .FormValue ("content" )
183
179
github := c .FormValue ("linkGithub" )
184
180
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 )
186
182
return c .JSON (http .StatusOK , H {})
187
183
}
188
184
}
189
185
190
- func updatePost (collection * mongo.Collection ) echo.HandlerFunc {
186
+ func updatePosts (collection * mongo.Collection ) echo.HandlerFunc {
191
187
return func (c echo.Context ) error {
192
188
id , _ := strconv .Atoi (c .FormValue ("id" ))
193
189
category , _ := strconv .Atoi (c .FormValue ("category" ))
@@ -198,78 +194,78 @@ func updatePost(collection *mongo.Collection) echo.HandlerFunc {
198
194
content := c .FormValue ("content" )
199
195
github := c .FormValue ("linkGithub" )
200
196
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 )
202
198
return c .JSON (http .StatusOK , H {})
203
199
}
204
200
}
205
201
206
- func deletePost (collection * mongo.Collection ) echo.HandlerFunc {
202
+ func deletePosts (collection * mongo.Collection ) echo.HandlerFunc {
207
203
return func (c echo.Context ) error {
208
204
id , _ := strconv .Atoi (c .FormValue ("id" ))
209
- DeletePost (collection , id )
205
+ DeletePosts (collection , id )
210
206
return c .JSON (http .StatusOK , H {})
211
207
}
212
208
}
213
209
214
- func getCat (collection * mongo.Collection ) echo.HandlerFunc {
210
+ func getCats (collection * mongo.Collection ) echo.HandlerFunc {
215
211
return func (c echo.Context ) error {
216
212
token := c .FormValue ("token" )
217
213
id , _ := strconv .Atoi (c .QueryParam ("id" ))
218
- result := GetCat (collection , id , token )
214
+ result := GetCats (collection , id , token )
219
215
return c .JSON (http .StatusOK , H {
220
216
"category" : result ,
221
217
})
222
218
}
223
219
}
224
220
225
- func newCat (collection * mongo.Collection ) echo.HandlerFunc {
221
+ func newCats (collection * mongo.Collection ) echo.HandlerFunc {
226
222
return func (c echo.Context ) error {
227
223
token := c .FormValue ("token" )
228
224
catID , _ := strconv .Atoi (c .FormValue ("id" ))
229
225
index , _ := strconv .Atoi (c .FormValue ("index" ))
230
226
name := c .FormValue ("name" )
231
- NewCat (collection , catID , index , name , token )
227
+ NewCats (collection , catID , index , name , token )
232
228
return c .JSON (http .StatusOK , H {})
233
229
}
234
230
}
235
231
236
- func patchCat (collection * mongo.Collection ) echo.HandlerFunc {
232
+ func patchCats (collection * mongo.Collection ) echo.HandlerFunc {
237
233
return func (c echo.Context ) error {
238
234
token := c .FormValue ("token" )
239
235
catID , _ := strconv .Atoi (c .FormValue ("id" ))
240
236
name := c .FormValue ("name" )
241
237
index , _ := strconv .Atoi (c .FormValue ("index" ))
242
- PatchCat (collection , catID , name , index , token )
238
+ PatchCats (collection , catID , name , index , token )
243
239
return c .JSON (http .StatusOK , H {})
244
240
}
245
241
}
246
242
247
- func deleteCat (collection * mongo.Collection ) echo.HandlerFunc {
243
+ func deleteCats (collection * mongo.Collection ) echo.HandlerFunc {
248
244
return func (c echo.Context ) error {
249
245
token := c .FormValue ("token" )
250
246
id , _ := strconv .Atoi (c .FormValue ("id" ))
251
- DeleteCat (collection , id , token )
247
+ DeleteCats (collection , id , token )
252
248
return c .JSON (http .StatusOK , H {})
253
249
}
254
250
}
255
251
256
- func newSponsor (collection * mongo.Collection ) echo.HandlerFunc {
252
+ func newSponsors (collection * mongo.Collection ) echo.HandlerFunc {
257
253
return func (c echo.Context ) error {
258
254
token := c .FormValue ("token" )
259
255
expiryStr := c .FormValue ("expiry" )
260
256
name := c .FormValue ("name" )
261
257
logo := c .FormValue ("logo" )
262
258
tier := c .FormValue ("tier" )
263
- NewSponsor (collection , expiryStr , name , logo , tier , token )
259
+ NewSponsors (collection , expiryStr , name , logo , tier , token )
264
260
return c .JSON (http .StatusOK , H {})
265
261
}
266
262
}
267
263
268
- func deleteSponsor (collection * mongo.Collection ) echo.HandlerFunc {
264
+ func deleteSponsors (collection * mongo.Collection ) echo.HandlerFunc {
269
265
return func (c echo.Context ) error {
270
266
token := c .FormValue ("token" )
271
267
id := c .FormValue ("id" )
272
- DeleteSponsor (collection , id , token )
268
+ DeleteSponsors (collection , id , token )
273
269
return c .JSON (http .StatusOK , H {})
274
270
}
275
271
}
0 commit comments