aboutsummaryrefslogtreecommitdiff
path: root/backend/handlers/map.go
diff options
context:
space:
mode:
authorArda Serdar Pektezol <1669855+pektezol@users.noreply.github.com>2023-10-09 19:02:39 +0300
committerGitHub <noreply@github.com>2023-10-09 19:02:39 +0300
commit1f8611153233516c9e18fc12b3fac9bae904804b (patch)
tree950d4c1546f6d71b12093a0543a9dfa00770688f /backend/handlers/map.go
parentfeat: add is_disabled to chapters maps (#112) (diff)
downloadlphub-1f8611153233516c9e18fc12b3fac9bae904804b.tar.gz
lphub-1f8611153233516c9e18fc12b3fac9bae904804b.tar.bz2
lphub-1f8611153233516c9e18fc12b3fac9bae904804b.zip
feat: new endpoint for getting every map for a game (#114)
Former-commit-id: 708f57ef08abea7f2b0549e8dad75096fd315558
Diffstat (limited to 'backend/handlers/map.go')
-rw-r--r--backend/handlers/map.go40
1 files changed, 40 insertions, 0 deletions
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 {
31 Maps []models.MapShort `json:"maps"` 31 Maps []models.MapShort `json:"maps"`
32} 32}
33 33
34type GameMapsResponse struct {
35 Game models.Game `json:"game"`
36 Maps []models.MapShort `json:"maps"`
37}
38
34type RecordSingleplayer struct { 39type RecordSingleplayer struct {
35 Placement int `json:"placement"` 40 Placement int `json:"placement"`
36 RecordID int `json:"record_id"` 41 RecordID int `json:"record_id"`
@@ -385,6 +390,41 @@ func FetchChapters(c *gin.Context) {
385 }) 390 })
386} 391}
387 392
393// GET Maps of a Game
394//
395// @Description Get maps from the specified game id.
396// @Tags games & chapters
397// @Produce json
398// @Param gameid path int true "Game ID"
399// @Success 200 {object} models.Response{data=ChaptersResponse}
400// @Router /games/{gameid}/maps [get]
401func FetchMaps(c *gin.Context) {
402 gameID, err := strconv.Atoi(c.Param("gameid"))
403 if err != nil {
404 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
405 return
406 }
407 var response GameMapsResponse
408 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)
409 if err != nil {
410 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
411 return
412 }
413 for rows.Next() {
414 var mapShort models.MapShort
415 if err := rows.Scan(&response.Game.ID, &response.Game.Name, &response.Game.IsCoop, &mapShort.ID, &mapShort.Name, &mapShort.IsDisabled); err != nil {
416 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
417 return
418 }
419 response.Maps = append(response.Maps, mapShort)
420 }
421 c.JSON(http.StatusOK, models.Response{
422 Success: true,
423 Message: "Successfully retrieved maps.",
424 Data: response,
425 })
426}
427
388// GET Maps of a Chapter 428// GET Maps of a Chapter
389// 429//
390// @Description Get maps from the specified chapter id. 430// @Description Get maps from the specified chapter id.