From 28045d2635dff2c2b18cbcdc0da424b0ec7da0ac Mon Sep 17 00:00:00 2001 From: Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> Date: Mon, 11 Dec 2023 16:34:10 +0300 Subject: feat: add discussion comment posting (#59) --- backend/api/routes.go | 1 + backend/handlers/discussions.go | 49 +++++++++++++++++++++++++++++++ docs/docs.go | 65 +++++++++++++++++++++++++++++++++++++++++ docs/swagger.json | 65 +++++++++++++++++++++++++++++++++++++++++ docs/swagger.yaml | 40 +++++++++++++++++++++++++ 5 files changed, 220 insertions(+) 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) { v1.GET(mapDiscussionsPath, handlers.FetchMapDiscussions) v1.GET(mapDiscussionIDPath, handlers.FetchMapDiscussion) v1.POST(mapDiscussionsPath, CheckAuth, handlers.CreateMapDiscussion) + v1.POST(mapDiscussionIDPath, CheckAuth, handlers.CreateMapDiscussionComment) v1.PUT(mapDiscussionIDPath, CheckAuth, handlers.EditMapDiscussion) v1.DELETE(mapDiscussionIDPath, CheckAuth, handlers.DeleteMapDiscussion) // 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 { Content string `json:"content" binding:"required"` } +type CreateMapDiscussionCommentRequest struct { + Comment string `json:"comment" binding:"required"` +} + type EditMapDiscussionRequest struct { Title string `json:"title" binding:"required"` Content string `json:"content" binding:"required"` @@ -178,6 +182,51 @@ func CreateMapDiscussion(c *gin.Context) { }) } +// POST Map Discussion Comment +// +// @Description Create map discussion comment with specified map id. +// @Tags maps +// @Produce json +// @Param Authorization header string true "JWT Token" +// @Param mapid path int true "Map ID" +// @Param request body CreateMapDiscussionCommentRequest true "Body" +// @Success 200 {object} models.Response{data=CreateMapDiscussionCommentRequest} +// @Router /maps/{mapid}/discussions/{discussionid} [post] +func CreateMapDiscussionComment(c *gin.Context) { + _, err := strconv.Atoi(c.Param("mapid")) + if err != nil { + c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) + return + } + discussionID, err := strconv.Atoi(c.Param("discussionid")) + if err != nil { + c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) + return + } + user, exists := c.Get("user") + if !exists { + c.JSON(http.StatusOK, models.ErrorResponse("User not logged in.")) + return + } + var request CreateMapDiscussionCommentRequest + if err := c.BindJSON(&request); err != nil { + c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) + return + } + sql := `INSERT INTO map_discussions_comments (discussion_id,user_id,comment) + VALUES($1,$2,$3);` + _, err = database.DB.Exec(sql, discussionID, user.(models.User).SteamID, request.Comment) + if err != nil { + c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) + return + } + c.JSON(http.StatusOK, models.Response{ + Success: true, + Message: "Successfully created map discussion comment.", + Data: request, + }) +} + // PUT Map Discussion // // @Description Edit map discussion with specified map id. diff --git a/docs/docs.go b/docs/docs.go index f689ba6..c2c1bab 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -522,6 +522,60 @@ const docTemplate = `{ } } }, + "post": { + "description": "Create map discussion comment with specified map id.", + "produces": [ + "application/json" + ], + "tags": [ + "maps" + ], + "parameters": [ + { + "type": "string", + "description": "JWT Token", + "name": "Authorization", + "in": "header", + "required": true + }, + { + "type": "integer", + "description": "Map ID", + "name": "mapid", + "in": "path", + "required": true + }, + { + "description": "Body", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/handlers.CreateMapDiscussionCommentRequest" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/models.Response" + }, + { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/handlers.CreateMapDiscussionCommentRequest" + } + } + } + ] + } + } + } + }, "delete": { "description": "Delete map summary with specified map id.", "produces": [ @@ -1325,6 +1379,17 @@ const docTemplate = `{ } } }, + "handlers.CreateMapDiscussionCommentRequest": { + "type": "object", + "required": [ + "comment" + ], + "properties": { + "comment": { + "type": "string" + } + } + }, "handlers.CreateMapDiscussionRequest": { "type": "object", "required": [ diff --git a/docs/swagger.json b/docs/swagger.json index a38240e..043fb6b 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -516,6 +516,60 @@ } } }, + "post": { + "description": "Create map discussion comment with specified map id.", + "produces": [ + "application/json" + ], + "tags": [ + "maps" + ], + "parameters": [ + { + "type": "string", + "description": "JWT Token", + "name": "Authorization", + "in": "header", + "required": true + }, + { + "type": "integer", + "description": "Map ID", + "name": "mapid", + "in": "path", + "required": true + }, + { + "description": "Body", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/handlers.CreateMapDiscussionCommentRequest" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/models.Response" + }, + { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/handlers.CreateMapDiscussionCommentRequest" + } + } + } + ] + } + } + } + }, "delete": { "description": "Delete map summary with specified map id.", "produces": [ @@ -1319,6 +1373,17 @@ } } }, + "handlers.CreateMapDiscussionCommentRequest": { + "type": "object", + "required": [ + "comment" + ], + "properties": { + "comment": { + "type": "string" + } + } + }, "handlers.CreateMapDiscussionRequest": { "type": "object", "required": [ diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 3249483..9de09a3 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -18,6 +18,13 @@ definitions: game: $ref: '#/definitions/models.Game' type: object + handlers.CreateMapDiscussionCommentRequest: + properties: + comment: + type: string + required: + - comment + type: object handlers.CreateMapDiscussionRequest: properties: content: @@ -772,6 +779,39 @@ paths: type: object tags: - maps + post: + description: Create map discussion comment with specified map id. + parameters: + - description: JWT Token + in: header + name: Authorization + required: true + type: string + - description: Map ID + in: path + name: mapid + required: true + type: integer + - description: Body + in: body + name: request + required: true + schema: + $ref: '#/definitions/handlers.CreateMapDiscussionCommentRequest' + produces: + - application/json + responses: + "200": + description: OK + schema: + allOf: + - $ref: '#/definitions/models.Response' + - properties: + data: + $ref: '#/definitions/handlers.CreateMapDiscussionCommentRequest' + type: object + tags: + - maps put: description: Edit map discussion with specified map id. parameters: -- cgit v1.2.3