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,
}
}
+9
View File
@@ -99,6 +99,15 @@ func (r *Repo) Moderate(ctx context.Context, id int, status string) (*Review, er
return review, errors.New("Недопустимый статус модерации")
}
func (r *Repo) ScenarioRating(ctx context.Context, scenarioID int) (float64, int32, error) {
var average float64
var count int64
err := r.pool.QueryRow(ctx, `SELECT COALESCE(AVG(r.rating),0), COUNT(*)
FROM game_reviews r JOIN games g ON g.id=r.game_id
WHERE g.scenario_id=$1 AND g.status='finish' AND r.status='approved'`, scenarioID).Scan(&average, &count)
return average, int32(count), err
}
func (r *Repo) queryOne(ctx context.Context, condition string, id int) (*Review, error) {
review := &Review{}
err := r.pool.QueryRow(ctx, `SELECT r.id,r.game_id,r.team_id,t.name,r.rating,r.text,r.status,r.created_at
@@ -62,6 +62,10 @@ func (s *Service) Moderate(ctx context.Context, id int, status string) (*game_re
return s.reviews.Moderate(ctx, id, status)
}
func (s *Service) ScenarioRating(ctx context.Context, scenarioID int) (float64, int32, error) {
return s.reviews.ScenarioRating(ctx, scenarioID)
}
func reviewReminderEmail(job *teams_repo.ReviewReminder, baseURL string) email_sender.Message {
link := strings.TrimRight(baseURL, "/") + "/team-story/" + fmt.Sprint(job.TeamID) + "?" + url.Values{"password": {job.Password}, "review": {"1"}}.Encode()
body := fmt.Sprintf("Команда «%s», поделитесь впечатлениями об игре «%s».\n\nОценка и отзыв займут пару минут: %s\n\nОтзыв можно оставить один раз от команды.", job.TeamName, job.GameName, link)
+13 -11
View File
@@ -7,15 +7,17 @@ import (
)
type Scenario struct {
ID int
Name string
Description *string
Image *string
Story *storytelling.Story
Author *repos.User
Status string
UpdatedAt time.Time
CreatedAt time.Time
PublishedAt *time.Time
IsDeleted bool
ID int
Name string
Description *string
Image *string
Story *storytelling.Story
Author *repos.User
Status string
UpdatedAt time.Time
CreatedAt time.Time
PublishedAt *time.Time
AverageRating float64
ReviewsCount int32
IsDeleted bool
}