diff options
| author | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2023-09-22 17:48:43 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-09-22 17:48:43 +0300 |
| commit | 3cda2073cc29317d6251e7b88102b70659aed6d6 (patch) | |
| tree | ec96bc99b6bc5d4a1f3a45efcfe6cb2e8514b7fa /backend/handlers/map.go | |
| parent | feat: completion count on summary for each cm entry (#63) (diff) | |
| download | lphub-3cda2073cc29317d6251e7b88102b70659aed6d6.tar.gz lphub-3cda2073cc29317d6251e7b88102b70659aed6d6.tar.bz2 lphub-3cda2073cc29317d6251e7b88102b70659aed6d6.zip | |
fix: change all status codes to 200 (#66)
Former-commit-id: ae632415e3f6f79a462240f151ada2e428318c6b
Diffstat (limited to 'backend/handlers/map.go')
| -rw-r--r-- | backend/handlers/map.go | 46 |
1 files changed, 23 insertions, 23 deletions
diff --git a/backend/handlers/map.go b/backend/handlers/map.go index e55dab4..f3198ff 100644 --- a/backend/handlers/map.go +++ b/backend/handlers/map.go | |||
| @@ -67,7 +67,7 @@ func FetchMapSummary(c *gin.Context) { | |||
| 67 | response := MapSummaryResponse{Map: models.Map{}, Summary: models.MapSummary{Routes: []models.MapRoute{}}} | 67 | response := MapSummaryResponse{Map: models.Map{}, Summary: models.MapSummary{Routes: []models.MapRoute{}}} |
| 68 | intID, err := strconv.Atoi(id) | 68 | intID, err := strconv.Atoi(id) |
| 69 | if err != nil { | 69 | if err != nil { |
| 70 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 70 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 71 | return | 71 | return |
| 72 | } | 72 | } |
| 73 | // Get map data | 73 | // Get map data |
| @@ -79,7 +79,7 @@ func FetchMapSummary(c *gin.Context) { | |||
| 79 | WHERE m.id = $1` | 79 | WHERE m.id = $1` |
| 80 | err = database.DB.QueryRow(sql, id).Scan(&response.Map.ID, &response.Map.GameName, &response.Map.ChapterName, &response.Map.MapName, &response.Map.Image, &response.Map.IsCoop) | 80 | err = database.DB.QueryRow(sql, id).Scan(&response.Map.ID, &response.Map.GameName, &response.Map.ChapterName, &response.Map.MapName, &response.Map.Image, &response.Map.IsCoop) |
| 81 | if err != nil { | 81 | if err != nil { |
| 82 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 82 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 83 | return | 83 | return |
| 84 | } | 84 | } |
| 85 | // Get map routes and histories | 85 | // Get map routes and histories |
| @@ -91,14 +91,14 @@ func FetchMapSummary(c *gin.Context) { | |||
| 91 | ORDER BY h.record_date ASC;` | 91 | ORDER BY h.record_date ASC;` |
| 92 | rows, err := database.DB.Query(sql, id) | 92 | rows, err := database.DB.Query(sql, id) |
| 93 | if err != nil { | 93 | if err != nil { |
| 94 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 94 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 95 | return | 95 | return |
| 96 | } | 96 | } |
| 97 | for rows.Next() { | 97 | for rows.Next() { |
| 98 | route := models.MapRoute{Category: models.Category{}, History: models.MapHistory{}} | 98 | route := models.MapRoute{Category: models.Category{}, History: models.MapHistory{}} |
| 99 | err = rows.Scan(&route.RouteID, &route.Category.ID, &route.Category.Name, &route.History.RunnerName, &route.History.ScoreCount, &route.History.Date, &route.Description, &route.Showcase, &route.Rating) | 99 | err = rows.Scan(&route.RouteID, &route.Category.ID, &route.Category.Name, &route.History.RunnerName, &route.History.ScoreCount, &route.History.Date, &route.Description, &route.Showcase, &route.Rating) |
| 100 | if err != nil { | 100 | if err != nil { |
| 101 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 101 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 102 | return | 102 | return |
| 103 | } | 103 | } |
| 104 | // Get completion count | 104 | // Get completion count |
| @@ -109,7 +109,7 @@ func FetchMapSummary(c *gin.Context) { | |||
| 109 | ) sub WHERE sub.rn = 1 AND score_count = $2` | 109 | ) sub WHERE sub.rn = 1 AND score_count = $2` |
| 110 | err = database.DB.QueryRow(sql, response.Map.ID, route.History.ScoreCount).Scan(&route.CompletionCount) | 110 | err = database.DB.QueryRow(sql, response.Map.ID, route.History.ScoreCount).Scan(&route.CompletionCount) |
| 111 | if err != nil { | 111 | if err != nil { |
| 112 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 112 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 113 | return | 113 | return |
| 114 | } | 114 | } |
| 115 | } else { | 115 | } else { |
| @@ -119,7 +119,7 @@ func FetchMapSummary(c *gin.Context) { | |||
| 119 | ) sub WHERE rn = 1 AND score_count = $2` | 119 | ) sub WHERE rn = 1 AND score_count = $2` |
| 120 | err = database.DB.QueryRow(sql, response.Map.ID, route.History.ScoreCount).Scan(&route.CompletionCount) | 120 | err = database.DB.QueryRow(sql, response.Map.ID, route.History.ScoreCount).Scan(&route.CompletionCount) |
| 121 | if err != nil { | 121 | if err != nil { |
| 122 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 122 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 123 | return | 123 | return |
| 124 | } | 124 | } |
| 125 | } | 125 | } |
| @@ -159,7 +159,7 @@ func FetchMapLeaderboards(c *gin.Context) { | |||
| 159 | var isDisabled bool | 159 | var isDisabled bool |
| 160 | intID, err := strconv.Atoi(id) | 160 | intID, err := strconv.Atoi(id) |
| 161 | if err != nil { | 161 | if err != nil { |
| 162 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 162 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 163 | return | 163 | return |
| 164 | } | 164 | } |
| 165 | response.Map.ID = intID | 165 | response.Map.ID = intID |
| @@ -170,11 +170,11 @@ func FetchMapLeaderboards(c *gin.Context) { | |||
| 170 | WHERE m.id = $1` | 170 | WHERE m.id = $1` |
| 171 | err = database.DB.QueryRow(sql, id).Scan(&response.Map.GameName, &response.Map.ChapterName, &response.Map.MapName, &isDisabled, &response.Map.Image, &response.Map.IsCoop) | 171 | err = database.DB.QueryRow(sql, id).Scan(&response.Map.GameName, &response.Map.ChapterName, &response.Map.MapName, &isDisabled, &response.Map.Image, &response.Map.IsCoop) |
| 172 | if err != nil { | 172 | if err != nil { |
| 173 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 173 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 174 | return | 174 | return |
| 175 | } | 175 | } |
| 176 | if isDisabled { | 176 | if isDisabled { |
| 177 | c.JSON(http.StatusBadRequest, models.ErrorResponse("Map is not available for competitive boards.")) | 177 | c.JSON(http.StatusOK, models.ErrorResponse("Map is not available for competitive boards.")) |
| 178 | return | 178 | return |
| 179 | } | 179 | } |
| 180 | totalRecords := 0 | 180 | totalRecords := 0 |
| @@ -214,7 +214,7 @@ func FetchMapLeaderboards(c *gin.Context) { | |||
| 214 | ORDER BY score_count, score_time` | 214 | ORDER BY score_count, score_time` |
| 215 | rows, err := database.DB.Query(sql, id) | 215 | rows, err := database.DB.Query(sql, id) |
| 216 | if err != nil { | 216 | if err != nil { |
| 217 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 217 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 218 | return | 218 | return |
| 219 | } | 219 | } |
| 220 | placement := 1 | 220 | placement := 1 |
| @@ -223,7 +223,7 @@ func FetchMapLeaderboards(c *gin.Context) { | |||
| 223 | var record RecordMultiplayer | 223 | var record RecordMultiplayer |
| 224 | err := rows.Scan(&record.RecordID, &record.Host.SteamID, &record.Host.UserName, &record.Host.AvatarLink, &record.Partner.SteamID, &record.Partner.UserName, &record.Partner.AvatarLink, &record.ScoreCount, &record.ScoreTime, &record.HostDemoID, &record.PartnerDemoID, &record.RecordDate) | 224 | err := rows.Scan(&record.RecordID, &record.Host.SteamID, &record.Host.UserName, &record.Host.AvatarLink, &record.Partner.SteamID, &record.Partner.UserName, &record.Partner.AvatarLink, &record.ScoreCount, &record.ScoreTime, &record.HostDemoID, &record.PartnerDemoID, &record.RecordDate) |
| 225 | if err != nil { | 225 | if err != nil { |
| 226 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 226 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 227 | return | 227 | return |
| 228 | } | 228 | } |
| 229 | if len(records) != 0 && records[len(records)-1].ScoreCount == record.ScoreCount && records[len(records)-1].ScoreTime == record.ScoreTime { | 229 | if len(records) != 0 && records[len(records)-1].ScoreCount == record.ScoreCount && records[len(records)-1].ScoreTime == record.ScoreTime { |
| @@ -240,7 +240,7 @@ func FetchMapLeaderboards(c *gin.Context) { | |||
| 240 | if totalRecords != 0 { | 240 | if totalRecords != 0 { |
| 241 | totalPages = (totalRecords + pageSize - 1) / pageSize | 241 | totalPages = (totalRecords + pageSize - 1) / pageSize |
| 242 | if page > totalPages { | 242 | if page > totalPages { |
| 243 | c.JSON(http.StatusBadRequest, models.ErrorResponse("Invalid page number.")) | 243 | c.JSON(http.StatusOK, models.ErrorResponse("Invalid page number.")) |
| 244 | return | 244 | return |
| 245 | } | 245 | } |
| 246 | startIndex := (page - 1) * pageSize | 246 | startIndex := (page - 1) * pageSize |
| @@ -264,7 +264,7 @@ func FetchMapLeaderboards(c *gin.Context) { | |||
| 264 | ORDER BY score_count, score_time` | 264 | ORDER BY score_count, score_time` |
| 265 | rows, err := database.DB.Query(sql, id) | 265 | rows, err := database.DB.Query(sql, id) |
| 266 | if err != nil { | 266 | if err != nil { |
| 267 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 267 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 268 | return | 268 | return |
| 269 | } | 269 | } |
| 270 | placement := 1 | 270 | placement := 1 |
| @@ -273,7 +273,7 @@ func FetchMapLeaderboards(c *gin.Context) { | |||
| 273 | var record RecordSingleplayer | 273 | var record RecordSingleplayer |
| 274 | err := rows.Scan(&record.RecordID, &record.User.SteamID, &record.User.UserName, &record.User.AvatarLink, &record.ScoreCount, &record.ScoreTime, &record.DemoID, &record.RecordDate) | 274 | err := rows.Scan(&record.RecordID, &record.User.SteamID, &record.User.UserName, &record.User.AvatarLink, &record.ScoreCount, &record.ScoreTime, &record.DemoID, &record.RecordDate) |
| 275 | if err != nil { | 275 | if err != nil { |
| 276 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 276 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 277 | return | 277 | return |
| 278 | } | 278 | } |
| 279 | if len(records) != 0 && records[len(records)-1].ScoreCount == record.ScoreCount && records[len(records)-1].ScoreTime == record.ScoreTime { | 279 | if len(records) != 0 && records[len(records)-1].ScoreCount == record.ScoreCount && records[len(records)-1].ScoreTime == record.ScoreTime { |
| @@ -290,7 +290,7 @@ func FetchMapLeaderboards(c *gin.Context) { | |||
| 290 | if totalRecords != 0 { | 290 | if totalRecords != 0 { |
| 291 | totalPages = (totalRecords + pageSize - 1) / pageSize | 291 | totalPages = (totalRecords + pageSize - 1) / pageSize |
| 292 | if page > totalPages { | 292 | if page > totalPages { |
| 293 | c.JSON(http.StatusBadRequest, models.ErrorResponse("Invalid page number.")) | 293 | c.JSON(http.StatusOK, models.ErrorResponse("Invalid page number.")) |
| 294 | return | 294 | return |
| 295 | } | 295 | } |
| 296 | startIndex := (page - 1) * pageSize | 296 | startIndex := (page - 1) * pageSize |
| @@ -325,14 +325,14 @@ func FetchMapLeaderboards(c *gin.Context) { | |||
| 325 | func FetchGames(c *gin.Context) { | 325 | func FetchGames(c *gin.Context) { |
| 326 | rows, err := database.DB.Query(`SELECT id, name, is_coop FROM games`) | 326 | rows, err := database.DB.Query(`SELECT id, name, is_coop FROM games`) |
| 327 | if err != nil { | 327 | if err != nil { |
| 328 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 328 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 329 | return | 329 | return |
| 330 | } | 330 | } |
| 331 | var games []models.Game | 331 | var games []models.Game |
| 332 | for rows.Next() { | 332 | for rows.Next() { |
| 333 | var game models.Game | 333 | var game models.Game |
| 334 | if err := rows.Scan(&game.ID, &game.Name, &game.IsCoop); err != nil { | 334 | if err := rows.Scan(&game.ID, &game.Name, &game.IsCoop); err != nil { |
| 335 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 335 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 336 | return | 336 | return |
| 337 | } | 337 | } |
| 338 | games = append(games, game) | 338 | games = append(games, game) |
| @@ -357,13 +357,13 @@ func FetchChapters(c *gin.Context) { | |||
| 357 | gameID := c.Param("id") | 357 | gameID := c.Param("id") |
| 358 | intID, err := strconv.Atoi(gameID) | 358 | intID, err := strconv.Atoi(gameID) |
| 359 | if err != nil { | 359 | if err != nil { |
| 360 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 360 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 361 | return | 361 | return |
| 362 | } | 362 | } |
| 363 | var response ChaptersResponse | 363 | var response ChaptersResponse |
| 364 | rows, err := database.DB.Query(`SELECT c.id, c.name, g.name FROM chapters c INNER JOIN games g ON c.game_id = g.id WHERE game_id = $1`, gameID) | 364 | rows, err := database.DB.Query(`SELECT c.id, c.name, g.name FROM chapters c INNER JOIN games g ON c.game_id = g.id WHERE game_id = $1`, gameID) |
| 365 | if err != nil { | 365 | if err != nil { |
| 366 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 366 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 367 | return | 367 | return |
| 368 | } | 368 | } |
| 369 | var chapters []models.Chapter | 369 | var chapters []models.Chapter |
| @@ -371,7 +371,7 @@ func FetchChapters(c *gin.Context) { | |||
| 371 | for rows.Next() { | 371 | for rows.Next() { |
| 372 | var chapter models.Chapter | 372 | var chapter models.Chapter |
| 373 | if err := rows.Scan(&chapter.ID, &chapter.Name, &gameName); err != nil { | 373 | if err := rows.Scan(&chapter.ID, &chapter.Name, &gameName); err != nil { |
| 374 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 374 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 375 | return | 375 | return |
| 376 | } | 376 | } |
| 377 | chapters = append(chapters, chapter) | 377 | chapters = append(chapters, chapter) |
| @@ -399,13 +399,13 @@ func FetchChapterMaps(c *gin.Context) { | |||
| 399 | chapterID := c.Param("id") | 399 | chapterID := c.Param("id") |
| 400 | intID, err := strconv.Atoi(chapterID) | 400 | intID, err := strconv.Atoi(chapterID) |
| 401 | if err != nil { | 401 | if err != nil { |
| 402 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 402 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 403 | return | 403 | return |
| 404 | } | 404 | } |
| 405 | var response ChapterMapsResponse | 405 | var response ChapterMapsResponse |
| 406 | rows, err := database.DB.Query(`SELECT m.id, m.name, c.name FROM maps m INNER JOIN chapters c ON m.chapter_id = c.id WHERE chapter_id = $1`, chapterID) | 406 | rows, err := database.DB.Query(`SELECT m.id, m.name, c.name FROM maps m INNER JOIN chapters c ON m.chapter_id = c.id WHERE chapter_id = $1`, chapterID) |
| 407 | if err != nil { | 407 | if err != nil { |
| 408 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 408 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 409 | return | 409 | return |
| 410 | } | 410 | } |
| 411 | var maps []models.MapShort | 411 | var maps []models.MapShort |
| @@ -413,7 +413,7 @@ func FetchChapterMaps(c *gin.Context) { | |||
| 413 | for rows.Next() { | 413 | for rows.Next() { |
| 414 | var mapShort models.MapShort | 414 | var mapShort models.MapShort |
| 415 | if err := rows.Scan(&mapShort.ID, &mapShort.Name, &chapterName); err != nil { | 415 | if err := rows.Scan(&mapShort.ID, &mapShort.Name, &chapterName); err != nil { |
| 416 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 416 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 417 | return | 417 | return |
| 418 | } | 418 | } |
| 419 | maps = append(maps, mapShort) | 419 | maps = append(maps, mapShort) |