package controller import ( "net/http" "time" "github.com/google/uuid" "github.com/labstack/echo/v4" "gitrepo.ru/neonxp/nquest/api" "gitrepo.ru/neonxp/nquest/pkg/contextlib" "gitrepo.ru/neonxp/nquest/pkg/models" "gitrepo.ru/neonxp/nquest/pkg/service" ) type Admin struct { GameService *service.Game } // (POST /admin/games) func (a *Admin) CreateGame(ctx echo.Context) error { user := contextlib.GetUser(ctx) req := &api.GameEditRequest{} if err := ctx.Bind(req); err != nil { return err } game := a.mapCreateGameRequest(req, user) var err error game, err = a.GameService.CreateGame(ctx.Request().Context(), game) if err != nil { return err } return ctx.JSON(http.StatusCreated, api.GameResponse{ Authors: make([]api.UserView, 0, len(game.Authors)), CreatedAt: game.CreatedAt.Format(time.RFC3339), Description: game.Description, Icon: game.IconID, Id: game.ID, Points: game.Points, TaskCount: len(game.Tasks), Title: game.Title, Type: api.MapGameTypeReverse(game.Type), }) } // (POST /games/{uid}) func (a *Admin) EditGame(ctx echo.Context, uid uuid.UUID) error { user := contextlib.GetUser(ctx) req := &api.GameEditRequest{} if err := ctx.Bind(req); err != nil { return err } game := a.mapCreateGameRequest(req, user) var err error game, err = a.GameService.UpdateGame(ctx.Request().Context(), uid, game) if err != nil { return err } return ctx.JSON(http.StatusCreated, api.GameResponse{ Authors: make([]api.UserView, 0, len(game.Authors)), CreatedAt: game.CreatedAt.Format(time.RFC3339), Description: game.Description, Icon: game.IconID, Id: game.ID, Points: game.Points, TaskCount: len(game.Tasks), Title: game.Title, Type: api.MapGameTypeReverse(game.Type), }) } func (*Admin) mapCreateGameRequest(req *api.GameEditRequest, user *models.User) *models.Game { game := &models.Game{ Model: models.Model{ ID: uuid.New(), }, Visible: false, Title: req.Title, Description: req.Description, Authors: []*models.User{ user, }, Type: api.MapGameType(req.Type), Tasks: make([]*models.Task, 0, len(req.Tasks)), Points: req.Points, IconID: req.Icon, } for order, te := range req.Tasks { task := &models.Task{ Model: models.Model{ ID: uuid.New(), }, Title: te.Title, Text: te.Text, MaxTime: 0, // Solutions: make([]*models.Solution, 0, len(te.Solutions)), Codes: make([]*models.Code, 0, len(te.Codes)), TaskOrder: uint(order), } // for _, s := range te.Solutions { // task.Solutions = append(task.Solutions, &models.Solution{ // Model: models.Model{ // ID: uuid.New(), // }, // After: s.After, // Text: s.Text, // }) // } for _, ce := range te.Codes { task.Codes = append(task.Codes, &models.Code{ Model: models.Model{ ID: uuid.New(), }, Code: ce.Code, Description: ce.Description, }) } game.Tasks = append(game.Tasks, task) } return game }