nquest/pkg/controller/admin.go

198 lines
4.2 KiB
Go
Raw Normal View History

2023-11-19 22:54:54 +03:00
package controller
import (
"net/http"
2024-01-20 21:37:49 +03:00
"time"
2023-11-19 22:54:54 +03:00
"github.com/google/uuid"
2023-11-19 22:54:54 +03:00
"github.com/labstack/echo/v4"
"gitrepo.ru/neonxp/nquest/api"
"gitrepo.ru/neonxp/nquest/pkg/contextlib"
2024-01-05 03:50:33 +03:00
"gitrepo.ru/neonxp/nquest/pkg/models"
2023-11-19 22:54:54 +03:00
"gitrepo.ru/neonxp/nquest/pkg/service"
)
type Admin struct {
GameService *service.Game
}
2024-01-28 22:19:41 +03:00
// (POST /games/{uid})
func (a *Admin) AdminEditGame(ctx echo.Context) error {
2023-11-19 22:54:54 +03:00
user := contextlib.GetUser(ctx)
2024-01-28 22:19:41 +03:00
req := &api.AdminEditGameJSONRequestBody{}
2024-01-05 03:50:33 +03:00
if err := ctx.Bind(req); err != nil {
return err
}
game := a.mapCreateGameRequest(req, user)
2024-01-28 22:19:41 +03:00
game, err := a.GameService.UpsertGame(ctx.Request().Context(), game)
if err != nil {
return err
}
tasks := make([]api.TaskEdit, 0, len(game.Tasks))
for _, t := range game.Tasks {
t := t
codes := make([]api.CodeEdit, 0, len(t.Codes))
for _, c := range t.Codes {
codes = append(codes, api.CodeEdit{
Id: &c.ID,
Code: c.Code,
2024-04-29 23:31:44 +03:00
Description: &c.Description,
})
}
tasks = append(tasks, api.TaskEdit{
Id: &t.ID,
Codes: codes,
Text: t.Text,
Title: t.Title,
})
}
return ctx.JSON(http.StatusOK, api.GameAdminResponse{
Description: game.Description,
2024-04-29 23:31:44 +03:00
Icon: game.IconID,
Id: &game.ID,
Points: game.Points,
Tasks: tasks,
Title: game.Title,
Type: api.MapGameTypeReverse(game.Type),
Visible: game.Visible,
})
}
// (GET /games/{uid})
2024-01-28 22:19:41 +03:00
func (a *Admin) AdminGetGame(ctx echo.Context, uid uuid.UUID) error {
user := contextlib.GetUser(ctx)
game, err := a.GameService.GetByID(ctx.Request().Context(), uid)
if err != nil {
return echo.ErrNotFound
}
if user.Role != models.RoleAdmin {
isAuthor := false
for _, u := range game.Authors {
if u.ID == user.ID {
isAuthor = true
break
}
}
if !isAuthor {
return echo.ErrForbidden
}
}
tasks := make([]api.TaskEdit, 0, len(game.Tasks))
for _, t := range game.Tasks {
t := t
codes := make([]api.CodeEdit, 0, len(t.Codes))
for _, c := range t.Codes {
codes = append(codes, api.CodeEdit{
Id: &c.ID,
Code: c.Code,
2024-04-29 23:31:44 +03:00
Description: &c.Description,
})
}
tasks = append(tasks, api.TaskEdit{
Id: &t.ID,
Codes: codes,
Text: t.Text,
Title: t.Title,
})
}
return ctx.JSON(http.StatusOK, api.GameAdminResponse{
Description: game.Description,
2024-04-29 23:31:44 +03:00
Icon: game.IconID,
Id: &game.ID,
Points: game.Points,
Tasks: tasks,
Title: game.Title,
Type: api.MapGameTypeReverse(game.Type),
Visible: game.Visible,
})
}
2024-01-28 22:19:41 +03:00
func (a *Admin) AdminListGames(ctx echo.Context) error {
user := contextlib.GetUser(ctx)
games, err := a.GameService.ListByAuthor(ctx.Request().Context(), user)
if err != nil {
return echo.ErrNotFound
}
resp := make(api.GameListResponse, 0, len(games))
for _, game := range games {
gv := api.GameView{
Id: game.ID,
Title: game.Title,
Type: api.MapGameTypeReverse(game.Type),
Points: game.Points,
TaskCount: len(game.Tasks),
CreatedAt: game.CreatedAt.Format(time.RFC3339),
Icon: game.IconID,
Visible: &game.Visible,
}
resp = append(resp, gv)
}
return ctx.JSON(http.StatusOK, resp)
}
2024-01-28 22:19:41 +03:00
func (*Admin) mapCreateGameRequest(req *api.GameEdit, user *models.User) *models.Game {
id := uuid.New()
if req.Id != nil {
id = *req.Id
}
2024-04-29 23:31:44 +03:00
2024-01-05 03:50:33 +03:00
game := &models.Game{
2024-01-28 22:19:41 +03:00
ID: id,
Visible: req.Visible,
2024-01-05 03:50:33 +03:00
Title: req.Title,
Description: req.Description,
Authors: []*models.User{
// user,
2024-01-05 03:50:33 +03:00
},
Type: api.MapGameType(req.Type),
Tasks: make([]*models.Task, 0, len(req.Tasks)),
Points: req.Points,
2024-04-29 23:31:44 +03:00
IconID: req.Icon,
2024-01-05 03:50:33 +03:00
}
for order, te := range req.Tasks {
2024-01-28 22:19:41 +03:00
id := uuid.New()
if te.Id != nil {
id = *te.Id
}
2024-01-05 03:50:33 +03:00
task := &models.Task{
2024-01-28 22:19:41 +03:00
ID: id,
Title: te.Title,
Text: te.Text,
2024-01-05 03:50:33 +03:00
Codes: make([]*models.Code, 0, len(te.Codes)),
TaskOrder: uint(order),
2024-01-05 03:50:33 +03:00
}
2024-01-05 03:50:33 +03:00
for _, ce := range te.Codes {
2024-01-28 22:19:41 +03:00
id := uuid.New()
if ce.Id != nil {
id = *ce.Id
}
2024-04-29 23:31:44 +03:00
desc := ""
if ce.Description != nil {
desc = *ce.Description
}
2024-01-05 03:50:33 +03:00
task.Codes = append(task.Codes, &models.Code{
2024-01-28 22:19:41 +03:00
ID: id,
2024-01-05 03:50:33 +03:00
Code: ce.Code,
2024-04-29 23:31:44 +03:00
Description: desc,
2024-01-05 03:50:33 +03:00
})
}
game.Tasks = append(game.Tasks, task)
2023-11-19 22:54:54 +03:00
}
2024-01-05 03:50:33 +03:00
return game
2023-11-19 22:54:54 +03:00
}