generated from VLADIMIR/template
add questions
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"evening_detective_server/internal/services/game_service"
|
||||
proto "evening_detective_server/proto"
|
||||
)
|
||||
|
||||
func mapAnswers(o []game_service.Answer) []*proto.Answer {
|
||||
res := make([]*proto.Answer, 0, len(o))
|
||||
for _, item := range o {
|
||||
res = append(res, &proto.Answer{
|
||||
QuestionCode: item.QuestionCode,
|
||||
Answer: item.Text,
|
||||
})
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func convertAnswers(o []*proto.Answer) []game_service.Answer {
|
||||
res := make([]game_service.Answer, 0, len(o))
|
||||
for _, item := range o {
|
||||
res = append(res, game_service.Answer{
|
||||
QuestionCode: item.QuestionCode,
|
||||
Text: item.Answer,
|
||||
})
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func mapAnswerScores(o []game_service.AnswerScore) []*proto.AnswerScore {
|
||||
res := make([]*proto.AnswerScore, 0, len(o))
|
||||
for _, item := range o {
|
||||
score := &proto.AnswerScore{
|
||||
QuestionCode: item.QuestionCode,
|
||||
Answer: item.Text,
|
||||
}
|
||||
if item.Score != nil {
|
||||
v := int32(*item.Score)
|
||||
score.Score = &v
|
||||
}
|
||||
res = append(res, score)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func mapTeamAnswers(o []game_service.TeamAnswers) []*proto.TeamAnswers {
|
||||
res := make([]*proto.TeamAnswers, 0, len(o))
|
||||
for _, item := range o {
|
||||
res = append(res, &proto.TeamAnswers{
|
||||
Team: mapTeam(item.Team),
|
||||
Answers: mapAnswerScores(item.Answers),
|
||||
})
|
||||
}
|
||||
return res
|
||||
}
|
||||
@@ -599,6 +599,73 @@ func (s *server) DeleteScenarioPlace(ctx context.Context, req *proto.DeleteScena
|
||||
return &proto.DeleteScenarioPlaceRsp{}, nil
|
||||
}
|
||||
|
||||
func (s *server) AddScenarioQuestion(ctx context.Context, req *proto.AddScenarioQuestionReq) (*proto.AddScenarioQuestionRsp, error) {
|
||||
claims := ctx.Value("claims").(*processor_jwt.JWTClaims)
|
||||
if !roles.HasRole(claims, roles.Author) {
|
||||
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
|
||||
}
|
||||
|
||||
err := s.scenarioService.AddScenarioQuestion(
|
||||
ctx,
|
||||
int(req.Id),
|
||||
convertQuestion(req.Question),
|
||||
claims.UserID,
|
||||
roles.HasRole(claims, roles.Admin),
|
||||
)
|
||||
if err != nil {
|
||||
return &proto.AddScenarioQuestionRsp{
|
||||
Error: err.Error(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
return &proto.AddScenarioQuestionRsp{}, nil
|
||||
}
|
||||
|
||||
func (s *server) UpdateScenarioQuestion(ctx context.Context, req *proto.UpdateScenarioQuestionReq) (*proto.UpdateScenarioQuestionRsp, error) {
|
||||
claims := ctx.Value("claims").(*processor_jwt.JWTClaims)
|
||||
if !roles.HasRole(claims, roles.Author) {
|
||||
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
|
||||
}
|
||||
|
||||
err := s.scenarioService.UpdateScenarioQuestion(
|
||||
ctx,
|
||||
int(req.Id),
|
||||
req.Code,
|
||||
convertQuestion(req.Question),
|
||||
claims.UserID,
|
||||
roles.HasRole(claims, roles.Admin),
|
||||
)
|
||||
if err != nil {
|
||||
return &proto.UpdateScenarioQuestionRsp{
|
||||
Error: err.Error(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
return &proto.UpdateScenarioQuestionRsp{}, nil
|
||||
}
|
||||
|
||||
func (s *server) DeleteScenarioQuestion(ctx context.Context, req *proto.DeleteScenarioQuestionReq) (*proto.DeleteScenarioQuestionRsp, error) {
|
||||
claims := ctx.Value("claims").(*processor_jwt.JWTClaims)
|
||||
if !roles.HasRole(claims, roles.Author) {
|
||||
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
|
||||
}
|
||||
|
||||
err := s.scenarioService.DeleteScenarioQuestion(
|
||||
ctx,
|
||||
int(req.Id),
|
||||
req.Code,
|
||||
claims.UserID,
|
||||
roles.HasRole(claims, roles.Admin),
|
||||
)
|
||||
if err != nil {
|
||||
return &proto.DeleteScenarioQuestionRsp{
|
||||
Error: err.Error(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
return &proto.DeleteScenarioQuestionRsp{}, nil
|
||||
}
|
||||
|
||||
func (s *server) UpdateScenarioIntro(ctx context.Context, req *proto.UpdateScenarioIntroReq) (*proto.UpdateScenarioIntroRsp, error) {
|
||||
claims := ctx.Value("claims").(*processor_jwt.JWTClaims)
|
||||
if !roles.HasRole(claims, roles.Author) {
|
||||
@@ -762,6 +829,25 @@ func (s *server) GetGame(ctx context.Context, req *proto.GetGameReq) (*proto.Get
|
||||
}, 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.gameService.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) 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) {
|
||||
@@ -1022,3 +1108,63 @@ func (s *server) DeleteLastTeamAction(ctx context.Context, req *proto.DeleteLast
|
||||
|
||||
return &proto.DeleteLastTeamActionRsp{}, nil
|
||||
}
|
||||
|
||||
func (s *server) GetTeamAnswers(ctx context.Context, req *proto.GetTeamAnswersReq) (*proto.GetTeamAnswersRsp, error) {
|
||||
answers, err := s.gameService.GetTeamAnswers(
|
||||
ctx,
|
||||
int(req.Id),
|
||||
req.Password,
|
||||
)
|
||||
if err != nil {
|
||||
return &proto.GetTeamAnswersRsp{
|
||||
Error: err.Error(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
return &proto.GetTeamAnswersRsp{
|
||||
Answers: mapAnswers(answers),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *server) SubmitTeamAnswers(ctx context.Context, req *proto.SubmitTeamAnswersReq) (*proto.SubmitTeamAnswersRsp, error) {
|
||||
err := s.gameService.SubmitTeamAnswers(
|
||||
ctx,
|
||||
int(req.Id),
|
||||
req.Password,
|
||||
convertAnswers(req.Answers),
|
||||
)
|
||||
if err != nil {
|
||||
return &proto.SubmitTeamAnswersRsp{
|
||||
Error: err.Error(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
return &proto.SubmitTeamAnswersRsp{}, nil
|
||||
}
|
||||
|
||||
func (s *server) SetTeamAnswerScore(ctx context.Context, req *proto.SetTeamAnswerScoreReq) (*proto.SetTeamAnswerScoreRsp, 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")
|
||||
}
|
||||
|
||||
var score *int
|
||||
if req.Score != nil {
|
||||
v := int(req.GetScore())
|
||||
score = &v
|
||||
}
|
||||
|
||||
err := s.gameService.SetTeamAnswerScore(
|
||||
ctx,
|
||||
int(req.Id),
|
||||
req.QuestionCode,
|
||||
score,
|
||||
)
|
||||
if err != nil {
|
||||
return &proto.SetTeamAnswerScoreRsp{
|
||||
Error: err.Error(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
return &proto.SetTeamAnswerScoreRsp{}, nil
|
||||
}
|
||||
|
||||
@@ -49,6 +49,7 @@ func mapStory(o *storytelling.Story) *proto.Story {
|
||||
return &proto.Story{
|
||||
Introduction: mapIntroduction(o.Introduction),
|
||||
Places: mapPlaces(o.Places),
|
||||
Questions: mapQuestions(o.Questions),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -197,3 +198,33 @@ func convertIntroduction(o *proto.Introduction) *storytelling.Introduction {
|
||||
Audio: o.Audio,
|
||||
}
|
||||
}
|
||||
|
||||
func mapQuestions(o []*storytelling.Question) []*proto.Question {
|
||||
res := make([]*proto.Question, 0, len(o))
|
||||
for _, item := range o {
|
||||
res = append(res, mapQuestion(item))
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func mapQuestion(o *storytelling.Question) *proto.Question {
|
||||
if o == nil {
|
||||
return nil
|
||||
}
|
||||
return &proto.Question{
|
||||
Code: o.Code,
|
||||
Text: o.Text,
|
||||
Answer: o.Answer,
|
||||
}
|
||||
}
|
||||
|
||||
func convertQuestion(o *proto.Question) *storytelling.Question {
|
||||
if o == nil {
|
||||
return nil
|
||||
}
|
||||
return &storytelling.Question{
|
||||
Code: o.Code,
|
||||
Text: textFormatter.FormatText(o.Text),
|
||||
Answer: textFormatter.FormatText(o.Answer),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user