nquest/pkg/controller/admin.go

254 lines
5.5 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-05 03:50:33 +03:00
// (POST /admin/games)
func (a *Admin) CreateGame(ctx echo.Context) error {
2023-11-19 22:54:54 +03:00
user := contextlib.GetUser(ctx)
2024-01-05 03:50:33 +03:00
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)
2023-11-19 22:54:54 +03:00
if err != nil {
return err
}
2024-01-05 03:50:33 +03:00
tasks := make([]api.TaskEdit, 0, len(game.Tasks))
for _, t := range game.Tasks {
codes := make([]api.CodeEdit, 0, len(t.Codes))
for _, c := range t.Codes {
codes = append(codes, api.CodeEdit{
Code: c.Code,
Description: c.Description,
})
}
tasks = append(tasks, api.TaskEdit{
Codes: codes,
Text: t.Text,
Title: t.Title,
})
}
return ctx.JSON(http.StatusOK, api.GameAdminResponse{
2024-01-20 21:37:49 +03:00
Description: game.Description,
Icon: game.IconID,
Id: &game.ID,
2024-01-20 21:37:49 +03:00
Points: game.Points,
Tasks: tasks,
2024-01-05 03:50:33 +03:00
Title: game.Title,
2024-01-20 21:37:49 +03:00
Type: api.MapGameTypeReverse(game.Type),
Visible: game.Visible,
2024-01-05 03:50:33 +03:00
})
}
// (POST /games/{uid})
func (a *Admin) EditGame(ctx echo.Context, uid uuid.UUID) error {
user := contextlib.GetUser(ctx)
req := &api.GameEditRequest{}
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
}
}
if err = ctx.Bind(req); err != nil {
return err
}
game = a.mapCreateGameRequest(req, user)
game, err = a.GameService.UpdateGame(ctx.Request().Context(), uid, 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,
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,
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})
func (a *Admin) GetGameByAdmin(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,
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,
Icon: game.IconID,
Id: &game.ID,
Points: game.Points,
Tasks: tasks,
Title: game.Title,
Type: api.MapGameTypeReverse(game.Type),
Visible: game.Visible,
})
}
func (a *Admin) ListGamesByAdmin(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,
}
resp = append(resp, gv)
}
return ctx.JSON(http.StatusOK, resp)
}
2024-01-05 03:50:33 +03:00
func (*Admin) mapCreateGameRequest(req *api.GameEditRequest, user *models.User) *models.Game {
game := &models.Game{
Model: models.Model{
ID: uuid.New(),
},
Visible: req.Visible,
2024-01-05 03:50:33 +03:00
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,
2024-01-20 21:37:49 +03:00
IconID: req.Icon,
2024-01-05 03:50:33 +03:00
}
for order, te := range req.Tasks {
if te.Id == nil {
u := uuid.New()
te.Id = &u
}
2024-01-05 03:50:33 +03:00
task := &models.Task{
Model: models.Model{
ID: *te.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 {
if ce.Id == nil {
u := uuid.New()
ce.Id = &u
}
2024-01-05 03:50:33 +03:00
task.Codes = append(task.Codes, &models.Code{
Model: models.Model{
ID: *ce.Id,
},
2024-01-05 03:50:33 +03:00
Code: ce.Code,
Description: ce.Description,
})
}
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
}