This commit is contained in:
2026-09-18 02:46:48 +07:00
parent e9e35c60d5
commit d255860b76
9 changed files with 97 additions and 33 deletions
+19
View File
@@ -5,6 +5,7 @@ import (
"evening_detective_server/internal/modules/processor_jwt"
"evening_detective_server/internal/modules/roles"
"evening_detective_server/internal/services/scenarios_service"
proto "evening_detective_server/proto"
"google.golang.org/grpc/codes"
@@ -53,6 +54,9 @@ func (s *server) GetScenariosCatalog(ctx context.Context, req *proto.GetScenario
Error: err.Error(),
}, nil
}
if err := s.addScenarioRatings(ctx, scenarios); err != nil {
return &proto.GetScenariosCatalogRsp{Error: err.Error()}, nil
}
return &proto.GetScenariosCatalogRsp{
Scenarios: mapScenarios(scenarios),
@@ -84,12 +88,27 @@ func (s *server) GetScenario(ctx context.Context, req *proto.GetScenarioReq) (*p
Error: err.Error(),
}, nil
}
if err := s.addScenarioRatings(ctx, []*scenarios_service.Scenario{scenario}); err != nil {
return &proto.GetScenarioRsp{Error: err.Error()}, nil
}
return &proto.GetScenarioRsp{
Scenario: mapScenario(scenario),
}, nil
}
func (s *server) addScenarioRatings(ctx context.Context, scenarios []*scenarios_service.Scenario) error {
for _, scenario := range scenarios {
average, count, err := s.reviewService.ScenarioRating(ctx, scenario.ID)
if err != nil {
return err
}
scenario.AverageRating = average
scenario.ReviewsCount = count
}
return nil
}
func (s *server) UpdateScenario(ctx context.Context, req *proto.UpdateScenarioReq) (*proto.UpdateScenarioRsp, error) {
claims := ctx.Value("claims").(*processor_jwt.JWTClaims)
if !roles.HasRole(claims, roles.Author) {
+12 -10
View File
@@ -26,16 +26,18 @@ func mapScenario(o *scenarios_service.Scenario) *proto.Scenario {
publishedAt = timestamppb.New(*o.PublishedAt)
}
return &proto.Scenario{
Id: int32(o.ID),
Name: o.Name,
Description: o.Description,
Image: o.Image,
Story: mapStory(o.Story),
Author: mapUser(o.Author),
Status: o.Status,
UpdatedAt: timestamppb.New(o.UpdatedAt),
CreatedAt: timestamppb.New(o.CreatedAt),
PublishedAt: publishedAt,
Id: int32(o.ID),
Name: o.Name,
Description: o.Description,
Image: o.Image,
Story: mapStory(o.Story),
Author: mapUser(o.Author),
Status: o.Status,
UpdatedAt: timestamppb.New(o.UpdatedAt),
CreatedAt: timestamppb.New(o.CreatedAt),
PublishedAt: publishedAt,
AverageRating: o.AverageRating,
ReviewsCount: o.ReviewsCount,
}
}