From 1f8611153233516c9e18fc12b3fac9bae904804b Mon Sep 17 00:00:00 2001 From: Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> Date: Mon, 9 Oct 2023 19:02:39 +0300 Subject: feat: new endpoint for getting every map for a game (#114) Former-commit-id: 708f57ef08abea7f2b0549e8dad75096fd315558 --- backend/api/routes.go | 2 ++ backend/handlers/map.go | 40 ++++++++++++++++++++++++++++++++++++++++ docs/docs.go | 43 +++++++++++++++++++++++++++++++++++++++++++ docs/swagger.json | 43 +++++++++++++++++++++++++++++++++++++++++++ docs/swagger.yaml | 25 +++++++++++++++++++++++++ 5 files changed, 153 insertions(+) diff --git a/backend/api/routes.go b/backend/api/routes.go index 9e703f6..2581a75 100644 --- a/backend/api/routes.go +++ b/backend/api/routes.go @@ -28,6 +28,7 @@ const ( searchPath string = "/search" gamesPath string = "/games" chaptersPath string = "/games/:gameid" + gameMapsPath string = "/games/:gameid/maps" chapterMapsPath string = "/chapters/:chapterid" scoreLogsPath string = "/logs/score" modLogsPath string = "/logs/mod" @@ -76,6 +77,7 @@ func InitRoutes(router *gin.Engine) { v1.GET(gamesPath, handlers.FetchGames) v1.GET(chaptersPath, handlers.FetchChapters) v1.GET(chapterMapsPath, handlers.FetchChapterMaps) + v1.GET(gameMapsPath, handlers.FetchMaps) // Logs v1.GET(scoreLogsPath, handlers.ScoreLogs) v1.GET(modLogsPath, CheckAuth, handlers.ModLogs) diff --git a/backend/handlers/map.go b/backend/handlers/map.go index 28457ce..d8f2ff0 100644 --- a/backend/handlers/map.go +++ b/backend/handlers/map.go @@ -31,6 +31,11 @@ type ChapterMapsResponse struct { Maps []models.MapShort `json:"maps"` } +type GameMapsResponse struct { + Game models.Game `json:"game"` + Maps []models.MapShort `json:"maps"` +} + type RecordSingleplayer struct { Placement int `json:"placement"` RecordID int `json:"record_id"` @@ -385,6 +390,41 @@ func FetchChapters(c *gin.Context) { }) } +// GET Maps of a Game +// +// @Description Get maps from the specified game id. +// @Tags games & chapters +// @Produce json +// @Param gameid path int true "Game ID" +// @Success 200 {object} models.Response{data=ChaptersResponse} +// @Router /games/{gameid}/maps [get] +func FetchMaps(c *gin.Context) { + gameID, err := strconv.Atoi(c.Param("gameid")) + if err != nil { + c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) + return + } + var response GameMapsResponse + rows, err := database.DB.Query(`SELECT g.id, g.name, g.is_coop, m.id, m."name", m.is_disabled FROM games g INNER JOIN maps m ON g.id = m.game_id WHERE g.id = $1 ORDER BY m.id `, gameID) + if err != nil { + c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) + return + } + for rows.Next() { + var mapShort models.MapShort + if err := rows.Scan(&response.Game.ID, &response.Game.Name, &response.Game.IsCoop, &mapShort.ID, &mapShort.Name, &mapShort.IsDisabled); err != nil { + c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) + return + } + response.Maps = append(response.Maps, mapShort) + } + c.JSON(http.StatusOK, models.Response{ + Success: true, + Message: "Successfully retrieved maps.", + Data: response, + }) +} + // GET Maps of a Chapter // // @Description Get maps from the specified chapter id. diff --git a/docs/docs.go b/docs/docs.go index 83d3a5c..d1643c3 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -177,6 +177,46 @@ const docTemplate = `{ } } }, + "/games/{gameid}/maps": { + "get": { + "description": "Get maps from the specified game id.", + "produces": [ + "application/json" + ], + "tags": [ + "games \u0026 chapters" + ], + "parameters": [ + { + "type": "integer", + "description": "Game ID", + "name": "gameid", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/models.Response" + }, + { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/handlers.ChaptersResponse" + } + } + } + ] + } + } + } + } + }, "/login": { "get": { "description": "Get (redirect) login page for Steam auth.", @@ -1884,6 +1924,9 @@ const docTemplate = `{ "id": { "type": "integer" }, + "is_disabled": { + "type": "boolean" + }, "name": { "type": "string" } diff --git a/docs/swagger.json b/docs/swagger.json index 48e337f..3f7deed 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -170,6 +170,46 @@ } } }, + "/games/{gameid}/maps": { + "get": { + "description": "Get maps from the specified game id.", + "produces": [ + "application/json" + ], + "tags": [ + "games \u0026 chapters" + ], + "parameters": [ + { + "type": "integer", + "description": "Game ID", + "name": "gameid", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/models.Response" + }, + { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/handlers.ChaptersResponse" + } + } + } + ] + } + } + } + } + }, "/login": { "get": { "description": "Get (redirect) login page for Steam auth.", @@ -1877,6 +1917,9 @@ "id": { "type": "integer" }, + "is_disabled": { + "type": "boolean" + }, "name": { "type": "string" } diff --git a/docs/swagger.yaml b/docs/swagger.yaml index b34a80c..d40b847 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -409,6 +409,8 @@ definitions: properties: id: type: integer + is_disabled: + type: boolean name: type: string type: object @@ -573,6 +575,29 @@ paths: type: object tags: - games & chapters + /games/{gameid}/maps: + get: + description: Get maps from the specified game id. + parameters: + - description: Game ID + in: path + name: gameid + required: true + type: integer + produces: + - application/json + responses: + "200": + description: OK + schema: + allOf: + - $ref: '#/definitions/models.Response' + - properties: + data: + $ref: '#/definitions/handlers.ChaptersResponse' + type: object + tags: + - games & chapters /login: get: consumes: -- cgit v1.2.3