From 52f43d9f0a731298159cdb408113eea04d5499fd Mon Sep 17 00:00:00 2001 From: aiae Date: Mon, 9 Nov 2026 11:53:24 +0200 Subject: [PATCH] Add multiplater/match route --- cmd/api/server.go | 1 + db/multiplayer_game_matches.go | 6 ++++++ db/multiplayer_games.go | 21 +++++++++++++++++++++ handlers/multiplayer.go | 25 ++++++++++++++++++++++++- 4 files changed, 52 insertions(+), 1 deletion(-) diff --git a/cmd/api/server.go b/cmd/api/server.go index 44cfdfa..ea4489f 100644 --- a/cmd/api/server.go +++ b/cmd/api/server.go @@ -296,6 +296,7 @@ func initializeRoutes(engine *gin.Engine) { // Multiplayer engine.GET("/v2/multiplayer/games", handlers.CreateHandler(handlers.GetRecentMultiplayerGames)) engine.GET("/v2/multiplayer/game/:id", handlers.CreateHandler(handlers.GetMultiplayerGame)) + engine.GET("/v2/multiplayer/match/:id", handlers.CreateHandler(handlers.GetMultiplayerMatch)) // Playlists engine.POST("/v2/playlists", middleware.RequireAuth, handlers.CreateHandler(handlers.CreatePlaylist)) diff --git a/db/multiplayer_game_matches.go b/db/multiplayer_game_matches.go index 191bc88..754602f 100644 --- a/db/multiplayer_game_matches.go +++ b/db/multiplayer_game_matches.go @@ -2,6 +2,7 @@ package db import ( "github.com/Quaver/api2/enums" + "gorm.io/gorm" "time" ) @@ -27,3 +28,8 @@ type MultiplayerGameMatches struct { func (*MultiplayerGameMatches) TableName() string { return "multiplayer_game_matches" } + +func (m *MultiplayerGameMatches) AfterFind(*gorm.DB) error { + m.TimePlayedJSON = time.UnixMilli(m.TimePlayed) + return nil +} diff --git a/db/multiplayer_games.go b/db/multiplayer_games.go index ef927d1..15f5458 100644 --- a/db/multiplayer_games.go +++ b/db/multiplayer_games.go @@ -74,3 +74,24 @@ func GetMultiplayerGame(id int) (*MultiplayerGame, error) { return game, nil } + +// GetMultiplayerMatch retrieves an individual multiplayer match by its match id. +func GetMultiplayerMatch(id int) (*MultiplayerGameMatches, *MultiplayerGame, error) { + var match *MultiplayerGameMatches + result := SQL. + Preload("Map"). + Preload("Scores"). + Preload("Scores.User"). + Where("id = ?", id). + First(&match) + if result.Error != nil { + return nil, nil, result.Error + } + + var game MultiplayerGame + if err := SQL.Where("id = ?", match.GameId).First(&game).Error; err != nil { + return nil, nil, err + } + + return match, &game, nil +} diff --git a/handlers/multiplayer.go b/handlers/multiplayer.go index d605aa8..d1a5c59 100644 --- a/handlers/multiplayer.go +++ b/handlers/multiplayer.go @@ -38,7 +38,7 @@ func GetRecentMultiplayerGames(c *gin.Context) *APIError { } // GetMultiplayerGame Gets an individual multiplayer game -// Endpoint: GET /v2/multiplayer/games/:id +// Endpoint: GET /v2/multiplayer/game/:id func GetMultiplayerGame(c *gin.Context) *APIError { id, err := strconv.Atoi(c.Param("id")) @@ -59,3 +59,26 @@ func GetMultiplayerGame(c *gin.Context) *APIError { c.JSON(http.StatusOK, gin.H{"game": game}) return nil } + +// GetMultiplayerMatch Gets an individual multiplayer match. +// Endpoint: GET /v2/multiplayer/match/:id +func GetMultiplayerMatch(c *gin.Context) *APIError { + id, err := strconv.Atoi(c.Param("id")) + if err != nil { + return APIErrorBadRequest("You must provide a valid match id") + } + + match, game, err := db.GetMultiplayerMatch(id) + if err != nil && err != gorm.ErrRecordNotFound { + return APIErrorServerError("Error retrieving multiplayer match", err) + } + if match == nil { + return APIErrorNotFound("Multiplayer Match") + } + + c.JSON(http.StatusOK, gin.H{ + "game": game, + "match": match, + }) + return nil +}