nquest/pkg/controller/game.go

46 lines
1,023 B
Go

package controller
import (
"net/http"
"time"
"github.com/labstack/echo/v4"
"gitrepo.ru/neonxp/nquest/api"
"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
}
resp := make(api.GameListResponse, 0, len(games))
for _, game := range games {
gv := api.GameView{
Id: game.ID,
Title: game.Title,
Description: game.Description,
Type: api.MapGameTypeReverse(game.Type),
Points: game.Points,
TaskCount: len(game.Tasks),
Authors: make([]api.UserView, 0, len(game.Authors)),
CreatedAt: game.CreatedAt.Format(time.RFC3339),
Icon: game.IconID,
}
for _, u := range game.Authors {
gv.Authors = append(gv.Authors, api.UserView{
Id: u.ID,
Username: u.Username,
})
}
resp = append(resp, gv)
}
return ctx.JSON(http.StatusOK, resp)
}