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 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) (limited to 'backend') 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. -- cgit v1.2.3