Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
6 changes: 6 additions & 0 deletions db/multiplayer_game_matches.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package db

import (
"github.com/Quaver/api2/enums"
"gorm.io/gorm"
"time"
)

Expand All @@ -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
}
21 changes: 21 additions & 0 deletions db/multiplayer_games.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
25 changes: 24 additions & 1 deletion handlers/multiplayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))

Expand All @@ -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
}
Loading