diff options
| author | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2024-07-05 21:03:46 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-05 21:03:46 +0300 |
| commit | d35a9a945ecaeeceb21bd91eeeaa4c6ec0a15e24 (patch) | |
| tree | c7eca0aa1e5a84b3580a629bcce49215b6faf80e /backend/handlers | |
| parent | feat: update map tables db schema (#157) (diff) | |
| download | lphub-d35a9a945ecaeeceb21bd91eeeaa4c6ec0a15e24.tar.gz lphub-d35a9a945ecaeeceb21bd91eeeaa4c6ec0a15e24.tar.bz2 lphub-d35a9a945ecaeeceb21bd91eeeaa4c6ec0a15e24.zip | |
feat: return portal count from games and chapter map select (#155)
Diffstat (limited to 'backend/handlers')
| -rw-r--r-- | backend/handlers/map.go | 42 |
1 files changed, 40 insertions, 2 deletions
diff --git a/backend/handlers/map.go b/backend/handlers/map.go index bf7c821..8104243 100644 --- a/backend/handlers/map.go +++ b/backend/handlers/map.go | |||
| @@ -339,6 +339,44 @@ func FetchGames(c *gin.Context) { | |||
| 339 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 339 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 340 | return | 340 | return |
| 341 | } | 341 | } |
| 342 | categoryPortalRows, err := database.DB.Query(`SELECT c.id, c.name FROM game_categories gc JOIN categories c ON gc.category_id = c.id WHERE gc.game_id = $1`, game.ID) | ||
| 343 | if err != nil { | ||
| 344 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | ||
| 345 | return | ||
| 346 | } | ||
| 347 | for categoryPortalRows.Next() { | ||
| 348 | var categoryPortals models.CategoryPortal | ||
| 349 | if err := categoryPortalRows.Scan(&categoryPortals.Category.ID, &categoryPortals.Category.Name); err != nil { | ||
| 350 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | ||
| 351 | return | ||
| 352 | } | ||
| 353 | getCategoryPortalCount := ` | ||
| 354 | SELECT | ||
| 355 | SUM(mh.lowest_score_count) AS total_lowest_scores | ||
| 356 | FROM ( | ||
| 357 | SELECT | ||
| 358 | map_id, | ||
| 359 | category_id, | ||
| 360 | MIN(score_count) AS lowest_score_count | ||
| 361 | FROM | ||
| 362 | map_history | ||
| 363 | GROUP BY | ||
| 364 | map_id, | ||
| 365 | category_id | ||
| 366 | ) mh | ||
| 367 | JOIN maps m ON mh.map_id = m.id | ||
| 368 | JOIN games g ON m.game_id = g.id | ||
| 369 | WHERE | ||
| 370 | mh.category_id = $1 and g.id = $2 | ||
| 371 | GROUP BY | ||
| 372 | g.id, | ||
| 373 | g.name, | ||
| 374 | mh.category_id; | ||
| 375 | ` | ||
| 376 | database.DB.QueryRow(getCategoryPortalCount, categoryPortals.Category.ID, game.ID).Scan(&categoryPortals.PortalCount) | ||
| 377 | // not checking for errors since there can be no record for category - just let it have 0 | ||
| 378 | game.CategoryPortals = append(game.CategoryPortals, categoryPortals) | ||
| 379 | } | ||
| 342 | games = append(games, game) | 380 | games = append(games, game) |
| 343 | } | 381 | } |
| 344 | c.JSON(http.StatusOK, models.Response{ | 382 | c.JSON(http.StatusOK, models.Response{ |
| @@ -441,7 +479,7 @@ func FetchChapterMaps(c *gin.Context) { | |||
| 441 | return | 479 | return |
| 442 | } | 480 | } |
| 443 | var response ChapterMapsResponse | 481 | var response ChapterMapsResponse |
| 444 | rows, err := database.DB.Query(`SELECT m.id, m.name, c.name, m.is_disabled, m.image FROM maps m INNER JOIN chapters c ON m.chapter_id = c.id WHERE chapter_id = $1`, chapterID) | 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) |
| 445 | if err != nil { | 483 | if err != nil { |
| 446 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 484 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 447 | return | 485 | return |
| @@ -450,7 +488,7 @@ func FetchChapterMaps(c *gin.Context) { | |||
| 450 | var chapterName string | 488 | var chapterName string |
| 451 | for rows.Next() { | 489 | for rows.Next() { |
| 452 | var mapShort models.MapShort | 490 | var mapShort models.MapShort |
| 453 | if err := rows.Scan(&mapShort.ID, &mapShort.Name, &chapterName, &mapShort.IsDisabled, &mapShort.Image); err != nil { | 491 | if err := rows.Scan(&mapShort.ID, &mapShort.Name, &chapterName, &mapShort.IsDisabled, &mapShort.Image, &mapShort.PortalCount); err != nil { |
| 454 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 492 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 455 | return | 493 | return |
| 456 | } | 494 | } |