From 0a2c6021e36be31aaffe8ace232179e2211b3140 Mon Sep 17 00:00:00 2001 From: Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> Date: Tue, 2 May 2023 23:25:48 +0300 Subject: feat: game, chapter, map endpoints for records page --- backend/controllers/mapController.go | 108 ++++++++++++++++++++++++++++++++++- 1 file changed, 106 insertions(+), 2 deletions(-) (limited to 'backend/controllers') diff --git a/backend/controllers/mapController.go b/backend/controllers/mapController.go index bd85a97..2bf1fdc 100644 --- a/backend/controllers/mapController.go +++ b/backend/controllers/mapController.go @@ -15,7 +15,6 @@ import ( // // @Summary Get map summary with specified id. // @Tags maps -// @Accept json // @Produce json // @Param id path int true "Map ID" // @Success 200 {object} models.Response{data=models.Map{data=models.MapSummary}} @@ -103,7 +102,6 @@ func FetchMapSummary(c *gin.Context) { // // @Summary Get map leaderboards with specified id. // @Tags maps -// @Accept json // @Produce json // @Param id path int true "Map ID" // @Success 200 {object} models.Response{data=models.Map{data=models.MapRecords}} @@ -215,3 +213,109 @@ func FetchMapLeaderboards(c *gin.Context) { Data: mapData, }) } + +// GET Games +// +// @Summary Get games from the leaderboards. +// @Tags games +// @Produce json +// @Success 200 {object} models.Response{data=[]models.Game} +// @Failure 400 {object} models.Response +// @Router /games [get] +func FetchGames(c *gin.Context) { + rows, err := database.DB.Query(`SELECT id, name FROM games`) + if err != nil { + c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) + return + } + var games []models.Game + for rows.Next() { + var game models.Game + if err := rows.Scan(&game.ID, &game.Name); err != nil { + c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) + return + } + games = append(games, game) + } + c.JSON(http.StatusOK, models.Response{ + Success: true, + Message: "Successfully retrieved games.", + Data: games, + }) +} + +// GET Chapters of a Game +// +// @Summary Get chapters from the specified game id. +// @Tags chapters +// @Produce json +// @Param id path int true "Game ID" +// @Success 200 {object} models.Response{data=[]models.Chapter} +// @Failure 400 {object} models.Response +// @Router /chapters/{id} [get] +func FetchChapters(c *gin.Context) { + gameID := c.Param("id") + intID, err := strconv.Atoi(gameID) + if err != nil { + c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) + return + } + rows, err := database.DB.Query(`SELECT id, name FROM chapters WHERE game_id = $1`, gameID) + if err != nil { + c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) + return + } + var chapters []models.Chapter + for rows.Next() { + var chapter models.Chapter + if err := rows.Scan(&chapter.ID, &chapter.Name); err != nil { + c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) + return + } + chapter.GameID = intID + chapters = append(chapters, chapter) + } + c.JSON(http.StatusOK, models.Response{ + Success: true, + Message: "Successfully retrieved chapters.", + Data: chapters, + }) +} + +// GET Maps of a Chapter +// +// @Summary Get maps from the specified chapter id. +// @Tags maps +// @Produce json +// @Param id path int true "Chapter ID" +// @Success 200 {object} models.Response{data=[]models.MapShort} +// @Failure 400 {object} models.Response +// @Router /maps/{id} [get] +func FetchChapterMaps(c *gin.Context) { + chapterID := c.Param("id") + intID, err := strconv.Atoi(chapterID) + if err != nil { + c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) + return + } + rows, err := database.DB.Query(`SELECT id, name FROM maps WHERE chapter_id = $1`, chapterID) + if err != nil { + c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) + return + } + var maps []models.MapShort + for rows.Next() { + var mapShort models.MapShort + if err := rows.Scan(&mapShort.ID, &mapShort.Name); err != nil { + c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) + return + } + mapShort.ChapterID = intID + maps = append(maps, mapShort) + } + c.JSON(http.StatusOK, models.Response{ + Success: true, + Message: "Successfully retrieved maps.", + Data: maps, + }) +} -- cgit v1.2.3