add games operations

This commit is contained in:
2026-07-26 22:38:33 +07:00
parent cf32a6114f
commit 6fc19833d3
24 changed files with 2430 additions and 96 deletions
+32
View File
@@ -0,0 +1,32 @@
package app
import (
"evening_detective_server/internal/services/game_service"
"evening_detective_server/proto"
"google.golang.org/protobuf/types/known/timestamppb"
)
func mapGame(
o *game_service.Game,
) *proto.Game {
return &proto.Game{
Id: int32(o.ID),
Name: o.Name,
Description: o.Description,
StartAt: timestamppb.New(o.StartAt),
Status: o.Status,
Scenario: mapScenario(o.Scenario),
Teams: mapTeams(o.Teams),
}
}
func mapGames(
o []*game_service.Game,
) []*proto.Game {
res := make([]*proto.Game, 0, len(o))
for _, item := range o {
res = append(res, mapGame(item))
}
return res
}
+115 -4
View File
@@ -6,6 +6,7 @@ import (
"evening_detective_server/internal/modules/processor_jwt"
"evening_detective_server/internal/modules/roles"
"evening_detective_server/internal/services/file_service"
"evening_detective_server/internal/services/game_service"
"evening_detective_server/internal/services/scenarios_service"
"evening_detective_server/internal/services/ui_service"
"evening_detective_server/internal/services/users_service"
@@ -23,6 +24,7 @@ type server struct {
uiService *ui_service.UiService
fileService *file_service.FileService
scenarioService *scenarios_service.ScenarioService
gameService *game_service.GameService
}
func NewServer(
@@ -30,12 +32,14 @@ func NewServer(
uiService *ui_service.UiService,
fileService *file_service.FileService,
scenarioService *scenarios_service.ScenarioService,
gameService *game_service.GameService,
) proto.EveningDetectiveServerServer {
return &server{
usersService: usersService,
uiService: uiService,
fileService: fileService,
scenarioService: scenarioService,
gameService: gameService,
}
}
@@ -167,10 +171,7 @@ func (s *server) DeleteUserRole(ctx context.Context, req *proto.DeleteUserRoleRe
return &proto.DeleteUserRoleRsp{}, nil
}
func (s *server) GetPermissions(
ctx context.Context,
req *proto.GetPermissionsReq,
) (*proto.GetPermissionsRsp, error) {
func (s *server) GetPermissions(ctx context.Context, req *proto.GetPermissionsReq) (*proto.GetPermissionsRsp, error) {
claims := ctx.Value("claims").(*processor_jwt.JWTClaims)
permissions := s.uiService.GetPermissions(claims.Roles)
return &proto.GetPermissionsRsp{
@@ -427,3 +428,113 @@ func (s *server) DeleteScenarioPlace(ctx context.Context, req *proto.DeleteScena
return &proto.DeleteScenarioPlaceRsp{}, nil
}
func (s *server) AddGame(ctx context.Context, req *proto.AddGameReq) (*proto.AddGameRsp, error) {
claims := ctx.Value("claims").(*processor_jwt.JWTClaims)
if !roles.HasRole(claims, roles.Organizer) {
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
}
if req.StartAt == nil {
return nil, status.Errorf(codes.InvalidArgument, "startAt must not be nil")
}
id, err := s.gameService.AddGame(
ctx,
req.Name,
req.Description,
req.StartAt.AsTime(),
req.ScenarioId,
)
if err != nil {
return &proto.AddGameRsp{
Error: err.Error(),
}, nil
}
return &proto.AddGameRsp{
Id: int32(id),
}, nil
}
func (s *server) DeleteGame(ctx context.Context, req *proto.DeleteGameReq) (*proto.DeleteGameRsp, error) {
claims := ctx.Value("claims").(*processor_jwt.JWTClaims)
if !roles.HasRole(claims, roles.Organizer) {
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
}
err := s.gameService.DeleteGame(
ctx,
int(req.Id),
)
if err != nil {
return &proto.DeleteGameRsp{
Error: err.Error(),
}, nil
}
return &proto.DeleteGameRsp{}, nil
}
func (s *server) GetGame(ctx context.Context, req *proto.GetGameReq) (*proto.GetGameRsp, error) {
claims := ctx.Value("claims").(*processor_jwt.JWTClaims)
if !roles.HasRole(claims, roles.Organizer) {
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
}
game, err := s.gameService.GetGame(ctx, int(req.Id))
if err != nil {
return &proto.GetGameRsp{
Error: err.Error(),
}, nil
}
return &proto.GetGameRsp{
Game: mapGame(game),
}, nil
}
func (s *server) GetGames(ctx context.Context, req *proto.GetGamesReq) (*proto.GetGamesRsp, error) {
claims := ctx.Value("claims").(*processor_jwt.JWTClaims)
if !roles.HasRole(claims, roles.Organizer) {
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
}
games, err := s.gameService.GetGames(ctx)
if err != nil {
return &proto.GetGamesRsp{
Error: err.Error(),
}, nil
}
return &proto.GetGamesRsp{
Games: mapGames(games),
}, nil
}
func (s *server) UpdateGame(ctx context.Context, req *proto.UpdateGameReq) (*proto.UpdateGameRsp, error) {
claims := ctx.Value("claims").(*processor_jwt.JWTClaims)
if !roles.HasRole(claims, roles.Organizer) {
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
}
if req.StartAt == nil {
return nil, status.Errorf(codes.InvalidArgument, "startAt must not be nil")
}
err := s.gameService.UpdateGame(
ctx,
int(req.Id),
req.Name,
req.Description,
req.StartAt.AsTime(),
req.ScenarioId,
)
if err != nil {
return &proto.UpdateGameRsp{
Error: err.Error(),
}, nil
}
return &proto.UpdateGameRsp{}, nil
}
+3
View File
@@ -18,6 +18,9 @@ func mapScenarios(o []*scenarios_service.Scenario) []*proto.Scenario {
}
func mapScenario(o *scenarios_service.Scenario) *proto.Scenario {
if o == nil {
return nil
}
var publishedAt *timestamppb.Timestamp
if o.PublishedAt != nil {
publishedAt = timestamppb.New(*o.PublishedAt)
+25
View File
@@ -0,0 +1,25 @@
package app
import (
"evening_detective_server/internal/services/game_service"
"evening_detective_server/proto"
)
func mapTeam(
o *game_service.Team,
) *proto.Team {
return &proto.Team{
Id: int32(o.ID),
Name: o.Name,
}
}
func mapTeams(
o []*game_service.Team,
) []*proto.Team {
res := make([]*proto.Team, 0, len(o))
for _, item := range o {
res = append(res, mapTeam(item))
}
return res
}