76 lines
1.7 KiB
Go
76 lines
1.7 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/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{
|
|
Id: int(game.ID),
|
|
Title: game.Title,
|
|
Description: game.Description,
|
|
})
|
|
}
|
|
|
|
func (*Admin) mapCreateGameRequest(req *api.GameEditRequest, user *models.User) *models.Game {
|
|
game := &models.Game{
|
|
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,
|
|
}
|
|
for _, te := range req.Tasks {
|
|
task := &models.Task{
|
|
Title: te.Title,
|
|
Text: te.Text,
|
|
MaxTime: 0,
|
|
Solutions: make([]*models.Solution, 0, len(te.Solutions)),
|
|
Codes: make([]*models.Code, 0, len(te.Codes)),
|
|
}
|
|
for _, s := range te.Solutions {
|
|
task.Solutions = append(task.Solutions, &models.Solution{
|
|
After: s.After,
|
|
Text: s.Text,
|
|
})
|
|
}
|
|
for _, ce := range te.Codes {
|
|
task.Codes = append(task.Codes, &models.Code{
|
|
Code: ce.Code,
|
|
Description: ce.Description,
|
|
})
|
|
}
|
|
game.Tasks = append(game.Tasks, task)
|
|
}
|
|
|
|
return game
|
|
}
|