From 7f56ba240f71c1f6800d1d51ba278b8d5a968ab3 Mon Sep 17 00:00:00 2001 From: Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> Date: Fri, 22 Dec 2023 12:22:14 +0300 Subject: fix: discussions logic cleanup, docs title changes (#142) --- backend/database/init.sql | 1 + backend/handlers/discussions.go | 35 ++++++++++++++++++----------------- backend/handlers/map.go | 4 ++-- backend/handlers/mod.go | 8 ++++---- backend/handlers/record.go | 4 ++-- docs/docs.go | 35 +++++++++++++++++++---------------- docs/swagger.json | 35 +++++++++++++++++++---------------- docs/swagger.yaml | 34 ++++++++++++++++++---------------- 8 files changed, 83 insertions(+), 73 deletions(-) diff --git a/backend/database/init.sql b/backend/database/init.sql index 9851ad7..54867ca 100644 --- a/backend/database/init.sql +++ b/backend/database/init.sql @@ -91,6 +91,7 @@ CREATE TABLE map_discussions ( content TEXT NOT NULL, created_at TIMESTAMP NOT NULL DEFAULT now(), updated_at TIMESTAMP NOT NULL DEFAULT now(), + is_deleted BOOLEAN NOT NULL DEFAULT false, PRIMARY KEY (id), FOREIGN KEY (map_id) REFERENCES maps(id), FOREIGN KEY (user_id) REFERENCES users(steam_id) diff --git a/backend/handlers/discussions.go b/backend/handlers/discussions.go index 04c91d0..89297f2 100644 --- a/backend/handlers/discussions.go +++ b/backend/handlers/discussions.go @@ -24,6 +24,7 @@ type MapDiscussion struct { Title string `json:"title"` Content string `json:"content"` // Upvotes int `json:"upvotes"` + CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` Comments []MapDiscussionComment `json:"comments"` } @@ -51,7 +52,7 @@ type EditMapDiscussionRequest struct { // GET Map Discussions // // @Description Get map discussions with specified map id. -// @Tags maps +// @Tags maps / discussions // @Produce json // @Param mapid path int true "Map ID" // @Success 200 {object} models.Response{data=MapDiscussionsResponse} @@ -64,8 +65,8 @@ func FetchMapDiscussions(c *gin.Context) { c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) return } - sql := `SELECT md.id, u.steam_id, u.user_name, u.avatar_link, md.title, md.content, md.updated_at FROM map_discussions md - INNER JOIN users u ON md.user_id = u.steam_id WHERE md.map_id = $1 + sql := `SELECT md.id, u.steam_id, u.user_name, u.avatar_link, md.title, md.content, md.created_at, md.updated_at FROM map_discussions md + INNER JOIN users u ON md.user_id = u.steam_id WHERE md.map_id = $1 AND is_deleted = false ORDER BY md.updated_at DESC` rows, err := database.DB.Query(sql, mapID) if err != nil { @@ -75,7 +76,7 @@ func FetchMapDiscussions(c *gin.Context) { // Get discussion data for rows.Next() { discussion := MapDiscussion{} - err := rows.Scan(&discussion.ID, &discussion.Creator.SteamID, &discussion.Creator.UserName, &discussion.Creator.AvatarLink, &discussion.Title, &discussion.Content, &discussion.UpdatedAt) + err := rows.Scan(&discussion.ID, &discussion.Creator.SteamID, &discussion.Creator.UserName, &discussion.Creator.AvatarLink, &discussion.Title, &discussion.Content, &discussion.CreatedAt, &discussion.UpdatedAt) if err != nil { c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) return @@ -92,7 +93,7 @@ func FetchMapDiscussions(c *gin.Context) { // GET Map Discussion // // @Description Get map discussion with specified map and discussion id. -// @Tags maps +// @Tags maps / discussions // @Produce json // @Param mapid path int true "Map ID" // @Param discussionid path int true "Discussion ID" @@ -111,9 +112,9 @@ func FetchMapDiscussion(c *gin.Context) { c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) return } - sql := `SELECT md.id, u.steam_id, u.user_name, u.avatar_link, md.title, md.content, md.updated_at FROM map_discussions md - INNER JOIN users u ON md.user_id = u.steam_id WHERE md.map_id = $1 AND md.id = $2` - err = database.DB.QueryRow(sql, mapID, discussionID).Scan(&response.Discussion.ID, &response.Discussion.Creator.SteamID, &response.Discussion.Creator.UserName, &response.Discussion.Creator.AvatarLink, &response.Discussion.Title, &response.Discussion.Content, &response.Discussion.UpdatedAt) + sql := `SELECT md.id, u.steam_id, u.user_name, u.avatar_link, md.title, md.content, md.created_at, md.updated_at FROM map_discussions md + INNER JOIN users u ON md.user_id = u.steam_id WHERE md.map_id = $1 AND md.id = $2 AND is_deleted = false` + err = database.DB.QueryRow(sql, mapID, discussionID).Scan(&response.Discussion.ID, &response.Discussion.Creator.SteamID, &response.Discussion.Creator.UserName, &response.Discussion.Creator.AvatarLink, &response.Discussion.Title, &response.Discussion.Content, &response.Discussion.CreatedAt, &response.Discussion.UpdatedAt) if err != nil { c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) return @@ -145,7 +146,7 @@ func FetchMapDiscussion(c *gin.Context) { // POST Map Discussion // // @Description Create map discussion with specified map id. -// @Tags maps +// @Tags maps / discussions // @Produce json // @Param Authorization header string true "JWT Token" // @Param mapid path int true "Map ID" @@ -185,11 +186,11 @@ func CreateMapDiscussion(c *gin.Context) { // POST Map Discussion Comment // // @Description Create map discussion comment with specified map id. -// @Tags maps +// @Tags maps / discussions // @Produce json // @Param Authorization header string true "JWT Token" // @Param mapid path int true "Map ID" -// @Param discussionid path int true "Discussion ID" +// @Param discussionid path int true "Discussion ID" // @Param request body CreateMapDiscussionCommentRequest true "Body" // @Success 200 {object} models.Response{data=CreateMapDiscussionCommentRequest} // @Router /maps/{mapid}/discussions/{discussionid} [post] @@ -231,7 +232,7 @@ func CreateMapDiscussionComment(c *gin.Context) { // PUT Map Discussion // // @Description Edit map discussion with specified map id. -// @Tags maps +// @Tags maps / discussions // @Produce json // @Param Authorization header string true "JWT Token" // @Param mapid path int true "Map ID" @@ -260,7 +261,7 @@ func EditMapDiscussion(c *gin.Context) { c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) return } - sql := `UPDATE map_discussions SET title = $4, content = $5, updated_at = $6 WHERE id = $1 AND map_id = $2 AND user_id = $3` + sql := `UPDATE map_discussions SET title = $4, content = $5, updated_at = $6 WHERE id = $1 AND map_id = $2 AND user_id = $3 AND is_deleted = false` result, err := database.DB.Exec(sql, discussionID, mapID, user.(models.User).SteamID, request.Title, request.Content, time.Now().UTC()) if err != nil { c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) @@ -282,10 +283,10 @@ func EditMapDiscussion(c *gin.Context) { }) } -// DELETE Map Summary +// DELETE Map Discussion // -// @Description Delete map summary with specified map id. -// @Tags maps +// @Description Delete map discussion with specified map id. +// @Tags maps / discussions // @Produce json // @Param Authorization header string true "JWT Token" // @Param mapid path int true "Map ID" @@ -308,7 +309,7 @@ func DeleteMapDiscussion(c *gin.Context) { c.JSON(http.StatusOK, models.ErrorResponse("User not logged in.")) return } - sql := `DELETE FROM map_discussions WHERE id = $1 AND map_id = $2 AND user_id = $3` + sql := `UPDATE map_discussions SET is_disabled = true WHERE id = $1 AND map_id = $2 AND user_id = $3` result, err := database.DB.Exec(sql, discussionID, mapID, user.(models.User).SteamID) if err != nil { c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) diff --git a/backend/handlers/map.go b/backend/handlers/map.go index f985a23..dfadade 100644 --- a/backend/handlers/map.go +++ b/backend/handlers/map.go @@ -61,7 +61,7 @@ type RecordMultiplayer struct { // GET Map Summary // // @Description Get map summary with specified id. -// @Tags maps +// @Tags maps / summary // @Produce json // @Param mapid path int true "Map ID" // @Success 200 {object} models.Response{data=MapSummaryResponse} @@ -140,7 +140,7 @@ func FetchMapSummary(c *gin.Context) { // GET Map Leaderboards // // @Description Get map leaderboards with specified id. -// @Tags maps +// @Tags maps / leaderboards // @Produce json // @Param mapid path int true "Map ID" // @Param page query int false "Page Number (default: 1)" diff --git a/backend/handlers/mod.go b/backend/handlers/mod.go index 845152f..3709e1d 100644 --- a/backend/handlers/mod.go +++ b/backend/handlers/mod.go @@ -40,7 +40,7 @@ type EditMapImageRequest struct { // POST Map Summary // // @Description Create map summary with specified map id. -// @Tags maps +// @Tags maps / summary // @Produce json // @Param Authorization header string true "JWT Token" // @Param mapid path int true "Map ID" @@ -124,7 +124,7 @@ func CreateMapSummary(c *gin.Context) { // PUT Map Summary // // @Description Edit map summary with specified map id. -// @Tags maps +// @Tags maps / summary // @Produce json // @Param Authorization header string true "JWT Token" // @Param mapid path int true "Map ID" @@ -209,7 +209,7 @@ func EditMapSummary(c *gin.Context) { // DELETE Map Summary // // @Description Delete map summary with specified map id. -// @Tags maps +// @Tags maps / summary // @Produce json // @Param Authorization header string true "JWT Token" // @Param mapid path int true "Map ID" @@ -298,7 +298,7 @@ func DeleteMapSummary(c *gin.Context) { // PUT Map Image // // @Description Edit map image with specified map id. -// @Tags maps +// @Tags maps / summary // @Produce json // @Param Authorization header string true "JWT Token" // @Param mapid path int true "Map ID" diff --git a/backend/handlers/record.go b/backend/handlers/record.go index 5dff085..79e9d3b 100644 --- a/backend/handlers/record.go +++ b/backend/handlers/record.go @@ -35,7 +35,7 @@ type RecordResponse struct { // POST Record // // @Description Post record with demo of a specific map. -// @Tags maps +// @Tags maps / leaderboards // @Accept mpfd // @Produce json // @Param mapid path int true "Map ID" @@ -202,7 +202,7 @@ func CreateRecordWithDemo(c *gin.Context) { // DELETE Record // // @Description Delete record with specified map and record id. -// @Tags maps +// @Tags maps / leaderboards // @Produce json // @Param mapid path int true "Map ID" // @Param recordid path int true "Record ID" diff --git a/docs/docs.go b/docs/docs.go index fc38d35..812bd64 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -328,7 +328,7 @@ const docTemplate = `{ "application/json" ], "tags": [ - "maps" + "maps / discussions" ], "parameters": [ { @@ -366,7 +366,7 @@ const docTemplate = `{ "application/json" ], "tags": [ - "maps" + "maps / discussions" ], "parameters": [ { @@ -422,7 +422,7 @@ const docTemplate = `{ "application/json" ], "tags": [ - "maps" + "maps / discussions" ], "parameters": [ { @@ -467,7 +467,7 @@ const docTemplate = `{ "application/json" ], "tags": [ - "maps" + "maps / discussions" ], "parameters": [ { @@ -528,7 +528,7 @@ const docTemplate = `{ "application/json" ], "tags": [ - "maps" + "maps / discussions" ], "parameters": [ { @@ -584,12 +584,12 @@ const docTemplate = `{ } }, "delete": { - "description": "Delete map summary with specified map id.", + "description": "Delete map discussion with specified map id.", "produces": [ "application/json" ], "tags": [ - "maps" + "maps / discussions" ], "parameters": [ { @@ -631,7 +631,7 @@ const docTemplate = `{ "application/json" ], "tags": [ - "maps" + "maps / summary" ], "parameters": [ { @@ -687,7 +687,7 @@ const docTemplate = `{ "application/json" ], "tags": [ - "maps" + "maps / leaderboards" ], "parameters": [ { @@ -742,7 +742,7 @@ const docTemplate = `{ "application/json" ], "tags": [ - "maps" + "maps / leaderboards" ], "parameters": [ { @@ -814,7 +814,7 @@ const docTemplate = `{ "application/json" ], "tags": [ - "maps" + "maps / leaderboards" ], "parameters": [ { @@ -856,7 +856,7 @@ const docTemplate = `{ "application/json" ], "tags": [ - "maps" + "maps / summary" ], "parameters": [ { @@ -894,7 +894,7 @@ const docTemplate = `{ "application/json" ], "tags": [ - "maps" + "maps / summary" ], "parameters": [ { @@ -948,7 +948,7 @@ const docTemplate = `{ "application/json" ], "tags": [ - "maps" + "maps / summary" ], "parameters": [ { @@ -1002,7 +1002,7 @@ const docTemplate = `{ "application/json" ], "tags": [ - "maps" + "maps / summary" ], "parameters": [ { @@ -1555,6 +1555,10 @@ const docTemplate = `{ "content": { "type": "string" }, + "created_at": { + "description": "Upvotes int ` + "`" + `json:\"upvotes\"` + "`" + `", + "type": "string" + }, "creator": { "$ref": "#/definitions/models.UserShortWithAvatar" }, @@ -1565,7 +1569,6 @@ const docTemplate = `{ "type": "string" }, "updated_at": { - "description": "Upvotes int ` + "`" + `json:\"upvotes\"` + "`" + `", "type": "string" } } diff --git a/docs/swagger.json b/docs/swagger.json index 32efc4b..5e3fcd7 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -322,7 +322,7 @@ "application/json" ], "tags": [ - "maps" + "maps / discussions" ], "parameters": [ { @@ -360,7 +360,7 @@ "application/json" ], "tags": [ - "maps" + "maps / discussions" ], "parameters": [ { @@ -416,7 +416,7 @@ "application/json" ], "tags": [ - "maps" + "maps / discussions" ], "parameters": [ { @@ -461,7 +461,7 @@ "application/json" ], "tags": [ - "maps" + "maps / discussions" ], "parameters": [ { @@ -522,7 +522,7 @@ "application/json" ], "tags": [ - "maps" + "maps / discussions" ], "parameters": [ { @@ -578,12 +578,12 @@ } }, "delete": { - "description": "Delete map summary with specified map id.", + "description": "Delete map discussion with specified map id.", "produces": [ "application/json" ], "tags": [ - "maps" + "maps / discussions" ], "parameters": [ { @@ -625,7 +625,7 @@ "application/json" ], "tags": [ - "maps" + "maps / summary" ], "parameters": [ { @@ -681,7 +681,7 @@ "application/json" ], "tags": [ - "maps" + "maps / leaderboards" ], "parameters": [ { @@ -736,7 +736,7 @@ "application/json" ], "tags": [ - "maps" + "maps / leaderboards" ], "parameters": [ { @@ -808,7 +808,7 @@ "application/json" ], "tags": [ - "maps" + "maps / leaderboards" ], "parameters": [ { @@ -850,7 +850,7 @@ "application/json" ], "tags": [ - "maps" + "maps / summary" ], "parameters": [ { @@ -888,7 +888,7 @@ "application/json" ], "tags": [ - "maps" + "maps / summary" ], "parameters": [ { @@ -942,7 +942,7 @@ "application/json" ], "tags": [ - "maps" + "maps / summary" ], "parameters": [ { @@ -996,7 +996,7 @@ "application/json" ], "tags": [ - "maps" + "maps / summary" ], "parameters": [ { @@ -1549,6 +1549,10 @@ "content": { "type": "string" }, + "created_at": { + "description": "Upvotes int `json:\"upvotes\"`", + "type": "string" + }, "creator": { "$ref": "#/definitions/models.UserShortWithAvatar" }, @@ -1559,7 +1563,6 @@ "type": "string" }, "updated_at": { - "description": "Upvotes int `json:\"upvotes\"`", "type": "string" } } diff --git a/docs/swagger.yaml b/docs/swagger.yaml index f3a848f..ac29f93 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -130,6 +130,9 @@ definitions: type: array content: type: string + created_at: + description: Upvotes int `json:"upvotes"` + type: string creator: $ref: '#/definitions/models.UserShortWithAvatar' id: @@ -137,7 +140,6 @@ definitions: title: type: string updated_at: - description: Upvotes int `json:"upvotes"` type: string type: object handlers.MapDiscussionComment: @@ -674,7 +676,7 @@ paths: $ref: '#/definitions/handlers.MapDiscussionsResponse' type: object tags: - - maps + - maps / discussions post: description: Create map discussion with specified map id. parameters: @@ -707,10 +709,10 @@ paths: $ref: '#/definitions/handlers.CreateMapDiscussionRequest' type: object tags: - - maps + - maps / discussions /maps/{mapid}/discussions/{discussionid}: delete: - description: Delete map summary with specified map id. + description: Delete map discussion with specified map id. parameters: - description: JWT Token in: header @@ -735,7 +737,7 @@ paths: schema: $ref: '#/definitions/models.Response' tags: - - maps + - maps / discussions get: description: Get map discussion with specified map and discussion id. parameters: @@ -762,7 +764,7 @@ paths: $ref: '#/definitions/handlers.MapDiscussionResponse' type: object tags: - - maps + - maps / discussions post: description: Create map discussion comment with specified map id. parameters: @@ -800,7 +802,7 @@ paths: $ref: '#/definitions/handlers.CreateMapDiscussionCommentRequest' type: object tags: - - maps + - maps / discussions put: description: Edit map discussion with specified map id. parameters: @@ -838,7 +840,7 @@ paths: $ref: '#/definitions/handlers.EditMapDiscussionRequest' type: object tags: - - maps + - maps / discussions /maps/{mapid}/image: put: description: Edit map image with specified map id. @@ -872,7 +874,7 @@ paths: $ref: '#/definitions/handlers.EditMapImageRequest' type: object tags: - - maps + - maps / summary /maps/{mapid}/leaderboards: get: description: Get map leaderboards with specified id. @@ -903,7 +905,7 @@ paths: $ref: '#/definitions/handlers.MapLeaderboardsResponse' type: object tags: - - maps + - maps / leaderboards /maps/{mapid}/record: post: consumes: @@ -950,7 +952,7 @@ paths: $ref: '#/definitions/handlers.RecordResponse' type: object tags: - - maps + - maps / leaderboards /maps/{mapid}/record/{recordid}: delete: description: Delete record with specified map and record id. @@ -978,7 +980,7 @@ paths: schema: $ref: '#/definitions/models.Response' tags: - - maps + - maps / leaderboards /maps/{mapid}/summary: delete: description: Delete map summary with specified map id. @@ -1012,7 +1014,7 @@ paths: $ref: '#/definitions/handlers.DeleteMapSummaryRequest' type: object tags: - - maps + - maps / summary get: description: Get map summary with specified id. parameters: @@ -1034,7 +1036,7 @@ paths: $ref: '#/definitions/handlers.MapSummaryResponse' type: object tags: - - maps + - maps / summary post: description: Create map summary with specified map id. parameters: @@ -1067,7 +1069,7 @@ paths: $ref: '#/definitions/handlers.CreateMapSummaryRequest' type: object tags: - - maps + - maps / summary put: description: Edit map summary with specified map id. parameters: @@ -1100,7 +1102,7 @@ paths: $ref: '#/definitions/handlers.EditMapSummaryRequest' type: object tags: - - maps + - maps / summary /profile: get: consumes: -- cgit v1.2.3