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
|
|
|
|
2024-01-06 08:44:26 +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)
|
2024-01-24 23:15:19 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-25 01:45:09 +03:00
|
|
|
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,
|
2024-01-25 01:45:09 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
tasks = append(tasks, api.TaskEdit{
|
|
|
|
Id: &t.ID,
|
|
|
|
Codes: codes,
|
|
|
|
Text: t.Text,
|
|
|
|
Title: t.Title,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx.JSON(http.StatusOK, api.GameAdminResponse{
|
2024-01-24 23:15:19 +03:00
|
|
|
Description: game.Description,
|
2024-04-29 23:31:44 +03:00
|
|
|
Icon: game.IconID,
|
2024-01-25 01:45:09 +03:00
|
|
|
Id: &game.ID,
|
2024-01-24 23:15:19 +03:00
|
|
|
Points: game.Points,
|
2024-01-25 01:45:09 +03:00
|
|
|
Tasks: tasks,
|
2024-01-24 23:15:19 +03:00
|
|
|
Title: game.Title,
|
|
|
|
Type: api.MapGameTypeReverse(game.Type),
|
2024-01-25 01:45:09 +03:00
|
|
|
Visible: game.Visible,
|
2024-01-24 23:15:19 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-01-25 01:45:09 +03:00
|
|
|
// (GET /games/{uid})
|
2024-01-28 22:19:41 +03:00
|
|
|
func (a *Admin) AdminGetGame(ctx echo.Context, uid uuid.UUID) error {
|
2024-01-25 01:45:09 +03:00
|
|
|
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,
|
2024-01-25 01:45:09 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
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,
|
2024-01-25 01:45:09 +03:00
|
|
|
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 {
|
2024-01-25 01:45:09 +03:00
|
|
|
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,
|
2024-04-29 23:01:24 +03:00
|
|
|
Visible: &game.Visible,
|
2024-01-25 01:45:09 +03:00
|
|
|
}
|
|
|
|
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,
|
2024-01-25 01:45:09 +03:00
|
|
|
Visible: req.Visible,
|
2024-01-05 03:50:33 +03:00
|
|
|
Title: req.Title,
|
|
|
|
Description: req.Description,
|
2024-04-29 23:01:24 +03:00
|
|
|
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
|
|
|
}
|
2024-01-06 08:44:26 +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-25 01:45:09 +03:00
|
|
|
}
|
2024-01-05 03:50:33 +03:00
|
|
|
task := &models.Task{
|
2024-01-28 22:19:41 +03:00
|
|
|
ID: id,
|
2024-01-25 01:45:09 +03:00
|
|
|
Title: te.Title,
|
|
|
|
Text: te.Text,
|
2024-01-05 03:50:33 +03:00
|
|
|
Codes: make([]*models.Code, 0, len(te.Codes)),
|
2024-01-06 08:44:26 +03:00
|
|
|
TaskOrder: uint(order),
|
2024-01-05 03:50:33 +03:00
|
|
|
}
|
2024-01-25 01:45:09 +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-01-25 01:45:09 +03:00
|
|
|
}
|
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
|
|
|
}
|