52 lines
1.2 KiB
Go
52 lines
1.2 KiB
Go
package controller
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
"gitrepo.ru/neonxp/nquest/api"
|
|
"gitrepo.ru/neonxp/nquest/pkg/contextlib"
|
|
"gitrepo.ru/neonxp/nquest/pkg/service"
|
|
)
|
|
|
|
type Game struct {
|
|
GameService *service.Game
|
|
}
|
|
|
|
// (GET /games)
|
|
func (g *Game) GetGames(ctx echo.Context) error {
|
|
games, err := g.GameService.List(ctx.Request().Context())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
user := contextlib.GetUser(ctx)
|
|
userTeamID := uint(0)
|
|
if user != nil && user.Team != nil {
|
|
userTeamID = user.Team.TeamID
|
|
}
|
|
|
|
resp := make(api.GameListResponse, 0, len(games))
|
|
for _, game := range games {
|
|
teams := make([]api.TeamView, 0, len(game.Teams))
|
|
for _, tm := range game.Teams {
|
|
ct := tm.TeamID == userTeamID
|
|
teams = append(teams, api.TeamView{
|
|
CreatedAt: tm.CreatedAt.Format("02.01.06"),
|
|
CurrentTeam: &ct,
|
|
Id: int(tm.TeamID),
|
|
Members: nil,
|
|
Name: tm.Team.Name,
|
|
})
|
|
}
|
|
resp = append(resp, api.GameView{
|
|
Id: int(game.ID),
|
|
Title: game.Title,
|
|
Description: game.Description,
|
|
StartAt: game.StartAt.Format("02.01.06 15:04"),
|
|
Teams: teams,
|
|
})
|
|
}
|
|
|
|
return ctx.JSON(http.StatusOK, resp)
|
|
}
|