generated from VLADIMIR/template
177 lines
4.6 KiB
Go
177 lines
4.6 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"evening_detective_server/internal/modules/roles"
|
|
"evening_detective_server/internal/services/team_service"
|
|
proto "evening_detective_server/proto"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
func (s *server) AddTeam(ctx context.Context, req *proto.AddTeamReq) (*proto.AddTeamRsp, error) {
|
|
claims, claimsErr := claimsFromContext(ctx)
|
|
if claimsErr != nil {
|
|
return nil, claimsErr
|
|
}
|
|
if !roles.HasRole(claims, roles.Organizer) && !roles.HasRole(claims, roles.Author) {
|
|
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
|
|
}
|
|
|
|
team, err := s.teamService.AddTeam(
|
|
ctx,
|
|
claims.UserID,
|
|
int(req.GameID),
|
|
req.Name,
|
|
req.CaptainEmail,
|
|
)
|
|
if err != nil {
|
|
return nil, teamErrorStatus(err, "не удалось добавить команду")
|
|
}
|
|
|
|
return &proto.AddTeamRsp{
|
|
ID: int32(team.ID),
|
|
Password: team.Password,
|
|
}, nil
|
|
}
|
|
|
|
func (s *server) UpdateTeam(ctx context.Context, req *proto.UpdateTeamReq) (*proto.UpdateTeamRsp, error) {
|
|
claims, claimsErr := claimsFromContext(ctx)
|
|
if claimsErr != nil {
|
|
return nil, claimsErr
|
|
}
|
|
if !roles.HasRole(claims, roles.Organizer) && !roles.HasRole(claims, roles.Author) {
|
|
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
|
|
}
|
|
|
|
err := s.teamService.UpdateTeam(
|
|
ctx,
|
|
int(req.ID),
|
|
req.Name,
|
|
)
|
|
if err != nil {
|
|
return nil, teamErrorStatus(err, "не удалось изменить команду")
|
|
}
|
|
|
|
return &proto.UpdateTeamRsp{}, nil
|
|
}
|
|
|
|
func (s *server) DeleteTeam(ctx context.Context, req *proto.DeleteTeamReq) (*proto.DeleteTeamRsp, error) {
|
|
claims, claimsErr := claimsFromContext(ctx)
|
|
if claimsErr != nil {
|
|
return nil, claimsErr
|
|
}
|
|
if !roles.HasRole(claims, roles.Organizer) && !roles.HasRole(claims, roles.Author) {
|
|
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
|
|
}
|
|
|
|
err := s.teamService.DeleteTeam(
|
|
ctx,
|
|
int(req.ID),
|
|
)
|
|
if err != nil {
|
|
return nil, teamErrorStatus(err, "не удалось удалить команду")
|
|
}
|
|
|
|
return &proto.DeleteTeamRsp{}, nil
|
|
}
|
|
|
|
func teamErrorStatus(err error, internalMessage string) error {
|
|
switch {
|
|
case errors.Is(err, team_service.ErrGameNotFound), errors.Is(err, team_service.ErrTeamNotFound):
|
|
return status.Error(codes.NotFound, err.Error())
|
|
case errors.Is(err, team_service.ErrInvalidCaptainEmail):
|
|
return status.Error(codes.InvalidArgument, err.Error())
|
|
default:
|
|
return status.Error(codes.Internal, internalMessage)
|
|
}
|
|
}
|
|
|
|
func (s *server) GiveTeamApplications(ctx context.Context, req *proto.GiveTeamApplicationsReq) (*proto.GiveTeamApplicationsRsp, error) {
|
|
claims, claimsErr := claimsFromContext(ctx)
|
|
if claimsErr != nil {
|
|
return nil, claimsErr
|
|
}
|
|
if !roles.HasRole(claims, roles.Organizer) && !roles.HasRole(claims, roles.Author) {
|
|
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
|
|
}
|
|
|
|
err := s.gameplayService.GiveTeamApplications(
|
|
ctx,
|
|
int(req.ID),
|
|
req.Name,
|
|
)
|
|
if err != nil {
|
|
return &proto.GiveTeamApplicationsRsp{
|
|
Error: err.Error(),
|
|
}, nil
|
|
}
|
|
|
|
return &proto.GiveTeamApplicationsRsp{}, nil
|
|
}
|
|
|
|
func (s *server) GetTeamStory(ctx context.Context, req *proto.GetTeamStoryReq) (*proto.GetTeamStoryRsp, error) {
|
|
story, game, actions, err := s.gameplayService.GetTeamActions(
|
|
ctx,
|
|
int(req.ID),
|
|
req.Password,
|
|
)
|
|
if err != nil {
|
|
return &proto.GetTeamStoryRsp{
|
|
Error: err.Error(),
|
|
}, nil
|
|
}
|
|
protoActions := make([]*proto.TeamAction, 0, len(actions))
|
|
for _, action := range actions {
|
|
protoActions = append(protoActions, &proto.TeamAction{Code: action.Code, Mcp: action.ViaMCP})
|
|
}
|
|
|
|
return &proto.GetTeamStoryRsp{
|
|
Story: mapStory(story),
|
|
Game: mapGame(game),
|
|
Actions: protoActions,
|
|
}, nil
|
|
}
|
|
|
|
func (s *server) AddTeamAction(ctx context.Context, req *proto.AddTeamActionReq) (*proto.AddTeamActionRsp, error) {
|
|
err := s.gameplayService.AddTeamAction(
|
|
ctx,
|
|
int(req.ID),
|
|
req.Password,
|
|
req.Code,
|
|
false,
|
|
)
|
|
if err != nil {
|
|
return &proto.AddTeamActionRsp{
|
|
Error: err.Error(),
|
|
}, nil
|
|
}
|
|
|
|
return &proto.AddTeamActionRsp{}, nil
|
|
}
|
|
|
|
func (s *server) DeleteLastTeamAction(ctx context.Context, req *proto.DeleteLastTeamActionReq) (*proto.DeleteLastTeamActionRsp, error) {
|
|
claims, claimsErr := claimsFromContext(ctx)
|
|
if claimsErr != nil {
|
|
return nil, claimsErr
|
|
}
|
|
if !roles.HasRole(claims, roles.Author) {
|
|
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
|
|
}
|
|
|
|
err := s.gameplayService.DeleteLastTeamAction(
|
|
ctx,
|
|
int(req.ID),
|
|
)
|
|
if err != nil {
|
|
return &proto.DeleteLastTeamActionRsp{
|
|
Error: err.Error(),
|
|
}, nil
|
|
}
|
|
|
|
return &proto.DeleteLastTeamActionRsp{}, nil
|
|
}
|