generated from VLADIMIR/template
add reviews
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"evening_detective_server/internal/modules/processor_jwt"
|
||||
"evening_detective_server/internal/modules/roles"
|
||||
"evening_detective_server/internal/repos/game_reviews_repo"
|
||||
"evening_detective_server/proto"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
)
|
||||
|
||||
func mapGameReview(r *game_reviews_repo.Review) *proto.GameReview {
|
||||
return &proto.GameReview{Id: int32(r.ID), GameId: int32(r.GameID), TeamId: int32(r.TeamID), TeamName: r.TeamName, Rating: r.Rating, Text: r.Text, Status: r.Status, CreatedAt: timestamppb.New(r.CreatedAt)}
|
||||
}
|
||||
|
||||
func mapGameReviews(reviews []*game_reviews_repo.Review) []*proto.GameReview {
|
||||
result := make([]*proto.GameReview, 0, len(reviews))
|
||||
for _, review := range reviews {
|
||||
result = append(result, mapGameReview(review))
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (s *server) CreateGameReview(ctx context.Context, req *proto.CreateGameReviewReq) (*proto.CreateGameReviewRsp, error) {
|
||||
review, err := s.reviewService.Create(ctx, int(req.GameId), int(req.TeamId), req.Password, req.Rating, req.Text)
|
||||
if err != nil {
|
||||
return &proto.CreateGameReviewRsp{Error: err.Error()}, nil
|
||||
}
|
||||
return &proto.CreateGameReviewRsp{Review: mapGameReview(review)}, nil
|
||||
}
|
||||
|
||||
func (s *server) GetGameReviews(ctx context.Context, req *proto.GetGameReviewsReq) (*proto.GetGameReviewsRsp, error) {
|
||||
reviews, average, count, err := s.reviewService.List(ctx, int(req.GameId), false)
|
||||
if err != nil {
|
||||
return &proto.GetGameReviewsRsp{Error: err.Error()}, nil
|
||||
}
|
||||
return &proto.GetGameReviewsRsp{AverageRating: average, ReviewsCount: count, Reviews: mapGameReviews(reviews)}, nil
|
||||
}
|
||||
|
||||
func (s *server) GetGameReviewsForModeration(ctx context.Context, req *proto.GetGameReviewsReq) (*proto.GetGameReviewsRsp, error) {
|
||||
claims := ctx.Value("claims").(*processor_jwt.JWTClaims)
|
||||
if !roles.HasRole(claims, roles.Admin) {
|
||||
return nil, status.Error(codes.PermissionDenied, "permission denied")
|
||||
}
|
||||
reviews, average, count, err := s.reviewService.List(ctx, int(req.GameId), true)
|
||||
if err != nil {
|
||||
return &proto.GetGameReviewsRsp{Error: err.Error()}, nil
|
||||
}
|
||||
return &proto.GetGameReviewsRsp{AverageRating: average, ReviewsCount: count, Reviews: mapGameReviews(reviews)}, nil
|
||||
}
|
||||
|
||||
func (s *server) ModerateGameReview(ctx context.Context, req *proto.ModerateGameReviewReq) (*proto.ModerateGameReviewRsp, error) {
|
||||
claims := ctx.Value("claims").(*processor_jwt.JWTClaims)
|
||||
if !roles.HasRole(claims, roles.Admin) {
|
||||
return nil, status.Error(codes.PermissionDenied, "permission denied")
|
||||
}
|
||||
review, err := s.reviewService.Moderate(ctx, int(req.Id), req.Status)
|
||||
if err != nil {
|
||||
return &proto.ModerateGameReviewRsp{Error: err.Error()}, nil
|
||||
}
|
||||
return &proto.ModerateGameReviewRsp{Review: mapGameReview(review)}, nil
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"evening_detective_server/internal/repos/screen_sessions_repo"
|
||||
"evening_detective_server/internal/services/answer_service"
|
||||
"evening_detective_server/internal/services/file_service"
|
||||
"evening_detective_server/internal/services/game_review_service"
|
||||
"evening_detective_server/internal/services/game_service"
|
||||
"evening_detective_server/internal/services/gameplay_service"
|
||||
"evening_detective_server/internal/services/participation_service"
|
||||
@@ -25,6 +26,7 @@ type server struct {
|
||||
gameService *game_service.GameService
|
||||
gameplayService *gameplay_service.GameplayService
|
||||
answerService *answer_service.AnswerService
|
||||
reviewService *game_review_service.Service
|
||||
teamService *team_service.TeamService
|
||||
participationService *participation_service.ParticipationService
|
||||
screenRepo *screen_sessions_repo.ScreenSessionsRepo
|
||||
@@ -39,6 +41,7 @@ func NewServer(
|
||||
gameService *game_service.GameService,
|
||||
gameplayService *gameplay_service.GameplayService,
|
||||
answerService *answer_service.AnswerService,
|
||||
reviewService *game_review_service.Service,
|
||||
teamService *team_service.TeamService,
|
||||
participationService *participation_service.ParticipationService,
|
||||
screenRepo *screen_sessions_repo.ScreenSessionsRepo,
|
||||
@@ -52,6 +55,7 @@ func NewServer(
|
||||
gameService: gameService,
|
||||
gameplayService: gameplayService,
|
||||
answerService: answerService,
|
||||
reviewService: reviewService,
|
||||
teamService: teamService,
|
||||
participationService: participationService,
|
||||
screenRepo: screenRepo,
|
||||
|
||||
Reference in New Issue
Block a user