2023-11-01 23:21:12 +03:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2024-01-06 08:44:26 +03:00
|
|
|
"time"
|
2023-11-01 23:21:12 +03:00
|
|
|
|
|
|
|
"github.com/labstack/echo/v4"
|
2023-11-02 01:21:13 +03:00
|
|
|
"gitrepo.ru/neonxp/nquest/api"
|
2023-11-01 23:21:12 +03:00
|
|
|
"gitrepo.ru/neonxp/nquest/pkg/service"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Game struct {
|
|
|
|
GameService *service.Game
|
|
|
|
}
|
|
|
|
|
2023-11-02 01:21:13 +03:00
|
|
|
// (GET /games)
|
|
|
|
func (g *Game) GetGames(ctx echo.Context) error {
|
|
|
|
games, err := g.GameService.List(ctx.Request().Context())
|
2023-11-01 23:21:12 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-02 01:21:13 +03:00
|
|
|
resp := make(api.GameListResponse, 0, len(games))
|
|
|
|
for _, game := range games {
|
2024-01-06 08:44:26 +03:00
|
|
|
gv := api.GameView{
|
|
|
|
Id: game.ID,
|
2023-11-02 01:21:13 +03:00
|
|
|
Title: game.Title,
|
|
|
|
Description: game.Description,
|
2024-01-06 08:44:26 +03:00
|
|
|
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),
|
|
|
|
}
|
|
|
|
for _, u := range game.Authors {
|
|
|
|
gv.Authors = append(gv.Authors, api.UserView{
|
|
|
|
Id: u.ID,
|
|
|
|
Username: u.Username,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
resp = append(resp, gv)
|
2023-11-02 01:21:13 +03:00
|
|
|
}
|
2023-11-01 23:21:12 +03:00
|
|
|
|
2023-11-02 01:21:13 +03:00
|
|
|
return ctx.JSON(http.StatusOK, resp)
|
2023-11-01 23:21:12 +03:00
|
|
|
}
|