aboutsummaryrefslogtreecommitdiff
path: root/backend
diff options
context:
space:
mode:
authorArda Serdar Pektezol <1669855+pektezol@users.noreply.github.com>2023-12-11 16:34:10 +0300
committerGitHub <noreply@github.com>2023-12-11 16:34:10 +0300
commit28045d2635dff2c2b18cbcdc0da424b0ec7da0ac (patch)
treecb94ec913a230f6bd916b7a5bc3721b45e13ea10 /backend
parentfeat: add content to discussions list (#59) (diff)
downloadlphub-28045d2635dff2c2b18cbcdc0da424b0ec7da0ac.tar.gz
lphub-28045d2635dff2c2b18cbcdc0da424b0ec7da0ac.tar.bz2
lphub-28045d2635dff2c2b18cbcdc0da424b0ec7da0ac.zip
feat: add discussion comment posting (#59)
Diffstat (limited to 'backend')
-rw-r--r--backend/api/routes.go1
-rw-r--r--backend/handlers/discussions.go49
2 files changed, 50 insertions, 0 deletions
diff --git a/backend/api/routes.go b/backend/api/routes.go
index 2581a75..ed1ca90 100644
--- a/backend/api/routes.go
+++ b/backend/api/routes.go
@@ -68,6 +68,7 @@ func InitRoutes(router *gin.Engine) {
68 v1.GET(mapDiscussionsPath, handlers.FetchMapDiscussions) 68 v1.GET(mapDiscussionsPath, handlers.FetchMapDiscussions)
69 v1.GET(mapDiscussionIDPath, handlers.FetchMapDiscussion) 69 v1.GET(mapDiscussionIDPath, handlers.FetchMapDiscussion)
70 v1.POST(mapDiscussionsPath, CheckAuth, handlers.CreateMapDiscussion) 70 v1.POST(mapDiscussionsPath, CheckAuth, handlers.CreateMapDiscussion)
71 v1.POST(mapDiscussionIDPath, CheckAuth, handlers.CreateMapDiscussionComment)
71 v1.PUT(mapDiscussionIDPath, CheckAuth, handlers.EditMapDiscussion) 72 v1.PUT(mapDiscussionIDPath, CheckAuth, handlers.EditMapDiscussion)
72 v1.DELETE(mapDiscussionIDPath, CheckAuth, handlers.DeleteMapDiscussion) 73 v1.DELETE(mapDiscussionIDPath, CheckAuth, handlers.DeleteMapDiscussion)
73 // Rankings, search 74 // Rankings, search
diff --git a/backend/handlers/discussions.go b/backend/handlers/discussions.go
index 600c612..51f5334 100644
--- a/backend/handlers/discussions.go
+++ b/backend/handlers/discussions.go
@@ -39,6 +39,10 @@ type CreateMapDiscussionRequest struct {
39 Content string `json:"content" binding:"required"` 39 Content string `json:"content" binding:"required"`
40} 40}
41 41
42type CreateMapDiscussionCommentRequest struct {
43 Comment string `json:"comment" binding:"required"`
44}
45
42type EditMapDiscussionRequest struct { 46type EditMapDiscussionRequest struct {
43 Title string `json:"title" binding:"required"` 47 Title string `json:"title" binding:"required"`
44 Content string `json:"content" binding:"required"` 48 Content string `json:"content" binding:"required"`
@@ -178,6 +182,51 @@ func CreateMapDiscussion(c *gin.Context) {
178 }) 182 })
179} 183}
180 184
185// POST Map Discussion Comment
186//
187// @Description Create map discussion comment with specified map id.
188// @Tags maps
189// @Produce json
190// @Param Authorization header string true "JWT Token"
191// @Param mapid path int true "Map ID"
192// @Param request body CreateMapDiscussionCommentRequest true "Body"
193// @Success 200 {object} models.Response{data=CreateMapDiscussionCommentRequest}
194// @Router /maps/{mapid}/discussions/{discussionid} [post]
195func CreateMapDiscussionComment(c *gin.Context) {
196 _, err := strconv.Atoi(c.Param("mapid"))
197 if err != nil {
198 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
199 return
200 }
201 discussionID, err := strconv.Atoi(c.Param("discussionid"))
202 if err != nil {
203 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
204 return
205 }
206 user, exists := c.Get("user")
207 if !exists {
208 c.JSON(http.StatusOK, models.ErrorResponse("User not logged in."))
209 return
210 }
211 var request CreateMapDiscussionCommentRequest
212 if err := c.BindJSON(&request); err != nil {
213 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
214 return
215 }
216 sql := `INSERT INTO map_discussions_comments (discussion_id,user_id,comment)
217 VALUES($1,$2,$3);`
218 _, err = database.DB.Exec(sql, discussionID, user.(models.User).SteamID, request.Comment)
219 if err != nil {
220 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
221 return
222 }
223 c.JSON(http.StatusOK, models.Response{
224 Success: true,
225 Message: "Successfully created map discussion comment.",
226 Data: request,
227 })
228}
229
181// PUT Map Discussion 230// PUT Map Discussion
182// 231//
183// @Description Edit map discussion with specified map id. 232// @Description Edit map discussion with specified map id.