diff options
| -rw-r--r-- | backend/controllers/mapController.go | 36 | ||||
| -rw-r--r-- | backend/models/models.go | 18 |
2 files changed, 40 insertions, 14 deletions
diff --git a/backend/controllers/mapController.go b/backend/controllers/mapController.go index cccd62f..bd85a97 100644 --- a/backend/controllers/mapController.go +++ b/backend/controllers/mapController.go | |||
| @@ -3,6 +3,7 @@ package controllers | |||
| 3 | import ( | 3 | import ( |
| 4 | "net/http" | 4 | "net/http" |
| 5 | "strconv" | 5 | "strconv" |
| 6 | "time" | ||
| 6 | 7 | ||
| 7 | "github.com/gin-gonic/gin" | 8 | "github.com/gin-gonic/gin" |
| 8 | "github.com/lib/pq" | 9 | "github.com/lib/pq" |
| @@ -25,6 +26,7 @@ func FetchMapSummary(c *gin.Context) { | |||
| 25 | // Get map data | 26 | // Get map data |
| 26 | var mapData models.Map | 27 | var mapData models.Map |
| 27 | var mapSummaryData models.MapSummary | 28 | var mapSummaryData models.MapSummary |
| 29 | var mapHistoryData []models.MapHistory | ||
| 28 | intID, err := strconv.Atoi(id) | 30 | intID, err := strconv.Atoi(id) |
| 29 | if err != nil { | 31 | if err != nil { |
| 30 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 32 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| @@ -34,13 +36,6 @@ func FetchMapSummary(c *gin.Context) { | |||
| 34 | var routers pq.StringArray | 36 | var routers pq.StringArray |
| 35 | sql := `SELECT g.name, c.name, m.name, m.description, m.showcase, | 37 | sql := `SELECT g.name, c.name, m.name, m.description, m.showcase, |
| 36 | ( | 38 | ( |
| 37 | SELECT user_name | ||
| 38 | FROM map_history | ||
| 39 | WHERE map_id = $1 | ||
| 40 | ORDER BY score_count | ||
| 41 | LIMIT 1 | ||
| 42 | ), | ||
| 43 | ( | ||
| 44 | SELECT array_agg(user_name) | 39 | SELECT array_agg(user_name) |
| 45 | FROM map_routers | 40 | FROM map_routers |
| 46 | WHERE map_id = $1 | 41 | WHERE map_id = $1 |
| @@ -64,11 +59,36 @@ func FetchMapSummary(c *gin.Context) { | |||
| 64 | INNER JOIN chapters c ON m.chapter_id = c.id | 59 | INNER JOIN chapters c ON m.chapter_id = c.id |
| 65 | WHERE m.id = $1;` | 60 | WHERE m.id = $1;` |
| 66 | // TODO: CategoryScores | 61 | // TODO: CategoryScores |
| 67 | err = database.DB.QueryRow(sql, id).Scan(&mapData.GameName, &mapData.ChapterName, &mapData.MapName, &mapSummaryData.Description, &mapSummaryData.Showcase, &mapSummaryData.FirstCompletion, &routers, &mapSummaryData.Rating) | 62 | err = database.DB.QueryRow(sql, id).Scan(&mapData.GameName, &mapData.ChapterName, &mapData.MapName, &mapSummaryData.Description, &mapSummaryData.Showcase, &routers, &mapSummaryData.Rating) |
| 63 | if err != nil { | ||
| 64 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 65 | return | ||
| 66 | } | ||
| 67 | var historyNames pq.StringArray | ||
| 68 | var historyScores pq.Int32Array | ||
| 69 | var historyDates pq.StringArray | ||
| 70 | sql = `SELECT array_agg(user_name), array_agg(score_count), array_agg(record_date) | ||
| 71 | FROM map_history | ||
| 72 | WHERE map_id = $1;` | ||
| 73 | err = database.DB.QueryRow(sql, id).Scan(&historyNames, &historyScores, &historyDates) | ||
| 68 | if err != nil { | 74 | if err != nil { |
| 69 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 75 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| 70 | return | 76 | return |
| 71 | } | 77 | } |
| 78 | for i := 0; i < len(historyNames); i++ { | ||
| 79 | var history models.MapHistory | ||
| 80 | history.RunnerName = historyNames[i] | ||
| 81 | history.ScoreCount = int(historyScores[i]) | ||
| 82 | layout := "2006-01-02 15:04:05" | ||
| 83 | date, err := time.Parse(layout, historyDates[i]) | ||
| 84 | if err != nil { | ||
| 85 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 86 | return | ||
| 87 | } | ||
| 88 | history.Date = date | ||
| 89 | mapHistoryData = append(mapHistoryData, history) | ||
| 90 | } | ||
| 91 | mapSummaryData.History = mapHistoryData | ||
| 72 | mapSummaryData.Routers = routers | 92 | mapSummaryData.Routers = routers |
| 73 | mapData.Data = mapSummaryData | 93 | mapData.Data = mapSummaryData |
| 74 | // Return response | 94 | // Return response |
diff --git a/backend/models/models.go b/backend/models/models.go index ae340df..7ba0da3 100644 --- a/backend/models/models.go +++ b/backend/models/models.go | |||
| @@ -32,12 +32,12 @@ type Map struct { | |||
| 32 | } | 32 | } |
| 33 | 33 | ||
| 34 | type MapSummary struct { | 34 | type MapSummary struct { |
| 35 | Description string `json:"description"` | 35 | Description string `json:"description"` |
| 36 | Showcase string `json:"showcase"` | 36 | Showcase string `json:"showcase"` |
| 37 | CategoryScores MapCategoryScores `json:"category_scores"` | 37 | CategoryScores MapCategoryScores `json:"category_scores"` |
| 38 | Rating float32 `json:"rating"` | 38 | Rating float32 `json:"rating"` |
| 39 | Routers []string `json:"routers"` | 39 | Routers []string `json:"routers"` |
| 40 | FirstCompletion any `json:"first_completion"` | 40 | History any `json:"history"` |
| 41 | } | 41 | } |
| 42 | 42 | ||
| 43 | type MapCategoryScores struct { | 43 | type MapCategoryScores struct { |
| @@ -51,6 +51,12 @@ type MapRecords struct { | |||
| 51 | Records any `json:"records"` | 51 | Records any `json:"records"` |
| 52 | } | 52 | } |
| 53 | 53 | ||
| 54 | type MapHistory struct { | ||
| 55 | RunnerName string `json:"runner_name"` | ||
| 56 | ScoreCount int `json:"score_count"` | ||
| 57 | Date time.Time `json:"date"` | ||
| 58 | } | ||
| 59 | |||
| 54 | type RecordSP struct { | 60 | type RecordSP struct { |
| 55 | RecordID int `json:"record_id"` | 61 | RecordID int `json:"record_id"` |
| 56 | Placement int `json:"placement"` | 62 | Placement int `json:"placement"` |