generated from VLADIMIR/template
243 lines
6.8 KiB
Go
243 lines
6.8 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
|
|
"evening_detective_server/internal/modules/processor_jwt"
|
|
"evening_detective_server/internal/modules/roles"
|
|
proto "evening_detective_server/proto"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
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) && !roles.HasRole(claims, roles.Author) {
|
|
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,
|
|
req.Format,
|
|
)
|
|
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) && !roles.HasRole(claims, roles.Author) {
|
|
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) && !roles.HasRole(claims, roles.Author) {
|
|
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
|
|
}
|
|
|
|
game, err := s.gameService.GetFullGame(ctx, int(req.Id))
|
|
if err != nil {
|
|
return &proto.GetGameRsp{
|
|
Error: err.Error(),
|
|
}, nil
|
|
}
|
|
|
|
return &proto.GetGameRsp{
|
|
Game: mapGame(game),
|
|
}, nil
|
|
}
|
|
|
|
func (s *server) GetGameAnswers(ctx context.Context, req *proto.GetGameAnswersReq) (*proto.GetGameAnswersRsp, error) {
|
|
claims := ctx.Value("claims").(*processor_jwt.JWTClaims)
|
|
if !roles.HasRole(claims, roles.Organizer) && !roles.HasRole(claims, roles.Author) {
|
|
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
|
|
}
|
|
|
|
gameAnswers, err := s.answerService.GetGameAnswers(ctx, int(req.GameId))
|
|
if err != nil {
|
|
return &proto.GetGameAnswersRsp{
|
|
Error: err.Error(),
|
|
}, nil
|
|
}
|
|
|
|
return &proto.GetGameAnswersRsp{
|
|
Questions: mapQuestions(gameAnswers.Questions),
|
|
Teams: mapTeamAnswers(gameAnswers.Teams),
|
|
}, nil
|
|
}
|
|
|
|
func (s *server) GetGameResults(ctx context.Context, req *proto.GetGameResultsReq) (*proto.GetGameResultsRsp, error) {
|
|
claims := ctx.Value("claims").(*processor_jwt.JWTClaims)
|
|
if !roles.HasRole(claims, roles.Organizer) && !roles.HasRole(claims, roles.Author) {
|
|
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
|
|
}
|
|
|
|
results, err := s.answerService.GetGameResults(ctx, int(req.GameId))
|
|
if err != nil {
|
|
return &proto.GetGameResultsRsp{
|
|
Error: err.Error(),
|
|
}, nil
|
|
}
|
|
|
|
return &proto.GetGameResultsRsp{
|
|
Questions: mapResultsQuestions(results.Questions),
|
|
Teams: mapTeamResults(results.Teams),
|
|
}, 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) && !roles.HasRole(claims, roles.Author) {
|
|
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) && !roles.HasRole(claims, roles.Author) {
|
|
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,
|
|
req.Format,
|
|
)
|
|
if err != nil {
|
|
return &proto.UpdateGameRsp{
|
|
Error: err.Error(),
|
|
}, nil
|
|
}
|
|
|
|
return &proto.UpdateGameRsp{}, nil
|
|
}
|
|
|
|
func (s *server) StartGame(ctx context.Context, req *proto.StartGameReq) (*proto.StartGameRsp, error) {
|
|
claims := ctx.Value("claims").(*processor_jwt.JWTClaims)
|
|
if !roles.HasRole(claims, roles.Organizer) && !roles.HasRole(claims, roles.Author) {
|
|
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
|
|
}
|
|
|
|
err := s.gameService.StartGame(ctx, int(req.Id))
|
|
if err != nil {
|
|
return &proto.StartGameRsp{
|
|
Error: err.Error(),
|
|
}, nil
|
|
}
|
|
|
|
return &proto.StartGameRsp{}, nil
|
|
}
|
|
|
|
func (s *server) PauseGame(ctx context.Context, req *proto.PauseGameReq) (*proto.PauseGameRsp, error) {
|
|
claims := ctx.Value("claims").(*processor_jwt.JWTClaims)
|
|
if !roles.HasRole(claims, roles.Organizer) && !roles.HasRole(claims, roles.Author) {
|
|
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
|
|
}
|
|
|
|
err := s.gameService.PauseGame(ctx, int(req.Id))
|
|
if err != nil {
|
|
return &proto.PauseGameRsp{
|
|
Error: err.Error(),
|
|
}, nil
|
|
}
|
|
|
|
return &proto.PauseGameRsp{}, nil
|
|
}
|
|
|
|
func (s *server) PlayGame(ctx context.Context, req *proto.PlayGameReq) (*proto.PlayGameRsp, error) {
|
|
claims := ctx.Value("claims").(*processor_jwt.JWTClaims)
|
|
if !roles.HasRole(claims, roles.Organizer) && !roles.HasRole(claims, roles.Author) {
|
|
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
|
|
}
|
|
|
|
err := s.gameService.PlayGame(ctx, int(req.Id))
|
|
if err != nil {
|
|
return &proto.PlayGameRsp{
|
|
Error: err.Error(),
|
|
}, nil
|
|
}
|
|
|
|
return &proto.PlayGameRsp{}, nil
|
|
}
|
|
|
|
func (s *server) FinishGame(ctx context.Context, req *proto.FinishGameReq) (*proto.FinishGameRsp, error) {
|
|
claims := ctx.Value("claims").(*processor_jwt.JWTClaims)
|
|
if !roles.HasRole(claims, roles.Organizer) && !roles.HasRole(claims, roles.Author) {
|
|
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
|
|
}
|
|
|
|
err := s.gameService.FinishGame(ctx, int(req.Id))
|
|
if err != nil {
|
|
return &proto.FinishGameRsp{
|
|
Error: err.Error(),
|
|
}, nil
|
|
}
|
|
|
|
return &proto.FinishGameRsp{}, nil
|
|
}
|
|
|
|
func (s *server) ResetGame(ctx context.Context, req *proto.ResetGameReq) (*proto.ResetGameRsp, error) {
|
|
claims := ctx.Value("claims").(*processor_jwt.JWTClaims)
|
|
if !roles.HasRole(claims, roles.Organizer) && !roles.HasRole(claims, roles.Author) {
|
|
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
|
|
}
|
|
|
|
err := s.gameService.ResetGame(ctx, int(req.Id))
|
|
if err != nil {
|
|
return &proto.ResetGameRsp{
|
|
Error: err.Error(),
|
|
}, nil
|
|
}
|
|
|
|
return &proto.ResetGameRsp{}, nil
|
|
}
|