aboutsummaryrefslogtreecommitdiff
path: root/backend
diff options
context:
space:
mode:
Diffstat (limited to 'backend')
-rw-r--r--backend/handlers/map.go56
-rw-r--r--backend/models/models.go9
2 files changed, 58 insertions, 7 deletions
diff --git a/backend/handlers/map.go b/backend/handlers/map.go
index 8104243..3166d1d 100644
--- a/backend/handlers/map.go
+++ b/backend/handlers/map.go
@@ -27,8 +27,8 @@ type ChaptersResponse struct {
27} 27}
28 28
29type ChapterMapsResponse struct { 29type ChapterMapsResponse struct {
30 Chapter models.Chapter `json:"chapter"` 30 Chapter models.Chapter `json:"chapter"`
31 Maps []models.MapShort `json:"maps"` 31 Maps []models.MapSelect `json:"maps"`
32} 32}
33 33
34type GameMapsResponse struct { 34type GameMapsResponse struct {
@@ -479,20 +479,62 @@ func FetchChapterMaps(c *gin.Context) {
479 return 479 return
480 } 480 }
481 var response ChapterMapsResponse 481 var response ChapterMapsResponse
482 rows, err := database.DB.Query(`SELECT m.id, m.name, c.name, m.is_disabled, m.image, MIN(mh.score_count) FROM maps m INNER JOIN chapters c ON m.chapter_id = c.id INNER JOIN map_history mh ON m.id = mh.map_id WHERE chapter_id = $1 GROUP BY m.id, c.name ORDER BY m.id;`, chapterID) 482 rows, err := database.DB.Query(`
483 SELECT
484 m.id,
485 m.name AS map_name,
486 c.name AS chapter_name,
487 m.is_disabled,
488 m.image,
489 cat.id,
490 cat.name,
491 mh.min_score_count AS score_count
492 FROM
493 maps m
494 INNER JOIN
495 chapters c ON m.chapter_id = c.id
496 INNER JOIN
497 game_categories gc ON gc.game_id = c.game_id
498 INNER JOIN
499 categories cat ON cat.id = gc.category_id
500 INNER JOIN
501 (
502 SELECT
503 map_id,
504 category_id,
505 MIN(score_count) AS min_score_count
506 FROM
507 map_history
508 GROUP BY
509 map_id,
510 category_id
511 ) mh ON m.id = mh.map_id AND gc.category_id = mh.category_id
512 WHERE
513 m.chapter_id = $1
514 ORDER BY
515 m.id, gc.category_id, mh.min_score_count ASC;
516 `, chapterID)
483 if err != nil { 517 if err != nil {
484 c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) 518 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
485 return 519 return
486 } 520 }
487 var maps []models.MapShort 521 var maps []models.MapSelect
488 var chapterName string 522 var chapterName string
523 var lastMapID int
489 for rows.Next() { 524 for rows.Next() {
490 var mapShort models.MapShort 525 var mapShort models.MapSelect
491 if err := rows.Scan(&mapShort.ID, &mapShort.Name, &chapterName, &mapShort.IsDisabled, &mapShort.Image, &mapShort.PortalCount); err != nil { 526 var categoryPortal models.CategoryPortal
527 if err := rows.Scan(&mapShort.ID, &mapShort.Name, &chapterName, &mapShort.IsDisabled, &mapShort.Image, &categoryPortal.Category.ID, &categoryPortal.Category.Name, &categoryPortal.PortalCount); err != nil {
492 c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) 528 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
493 return 529 return
494 } 530 }
495 maps = append(maps, mapShort) 531 if mapShort.ID == lastMapID {
532 maps[len(maps)-1].CategoryPortals = append(maps[len(maps)-1].CategoryPortals, categoryPortal)
533 } else {
534 mapShort.CategoryPortals = append(mapShort.CategoryPortals, categoryPortal)
535 maps = append(maps, mapShort)
536 lastMapID = mapShort.ID
537 }
496 } 538 }
497 response.Chapter.ID = intID 539 response.Chapter.ID = intID
498 response.Chapter.Name = chapterName 540 response.Chapter.Name = chapterName
diff --git a/backend/models/models.go b/backend/models/models.go
index 64d90d1..a114f2c 100644
--- a/backend/models/models.go
+++ b/backend/models/models.go
@@ -58,6 +58,15 @@ type MapShort struct {
58 Difficulty int `json:"difficulty"` 58 Difficulty int `json:"difficulty"`
59} 59}
60 60
61type MapSelect struct {
62 ID int `json:"id"`
63 Name string `json:"name"`
64 Image string `json:"image"`
65 IsDisabled bool `json:"is_disabled"`
66 Difficulty int `json:"difficulty"`
67 CategoryPortals []CategoryPortal `json:"category_portals"`
68}
69
61type MapSummary struct { 70type MapSummary struct {
62 Routes []MapRoute `json:"routes"` 71 Routes []MapRoute `json:"routes"`
63} 72}