diff --git a/.gitignore b/.gitignore index ccf631b..1dd4974 100644 --- a/.gitignore +++ b/.gitignore @@ -17,7 +17,7 @@ # Dependency directories (remove the comment below to include it) # vendor/ -/bin/ +# /bin/ # Go workspace file go.work diff --git a/bin/evening_detective_server b/bin/evening_detective_server new file mode 100755 index 0000000..2f89323 Binary files /dev/null and b/bin/evening_detective_server differ diff --git a/internal/app/scenarios.go b/internal/app/scenarios.go index 8ff8ab7..5081551 100644 --- a/internal/app/scenarios.go +++ b/internal/app/scenarios.go @@ -38,7 +38,13 @@ func (s *server) GetMyScenarios(ctx context.Context, _ *proto.GetMyScenariosReq) return nil, status.Errorf(codes.PermissionDenied, "permission denied") } - scenarios, err := s.scenarioService.GetScenariosByAuthorID(ctx, claims.UserID) + var scenarios []*scenarios_service.Scenario + var err error + if roles.HasRole(claims, roles.Admin) { + scenarios, err = s.scenarioService.GetAllScenarios(ctx) + } else { + scenarios, err = s.scenarioService.GetScenariosByAuthorID(ctx, claims.UserID) + } if err != nil { return nil, scenarioErrorStatus(err, "не удалось получить сценарии автора") } diff --git a/internal/repos/scenarios_repo/repo.go b/internal/repos/scenarios_repo/repo.go index 0c3b705..1b0260d 100644 --- a/internal/repos/scenarios_repo/repo.go +++ b/internal/repos/scenarios_repo/repo.go @@ -54,9 +54,7 @@ func (r *ScenariosRepo) GetScenariosByAuthorID( ctx context.Context, authorID int, ) ([]*repos.Scenario, error) { - rows, err := r.pool.Query( - ctx, - `SELECT + return r.getScenarios(ctx, `SELECT scenarios.id, scenarios.name, scenarios.description, @@ -72,44 +70,27 @@ func (r *ScenariosRepo) GetScenariosByAuthorID( LEFT JOIN users on scenarios.author_id = users.id WHERE scenarios.author_id = $1 and is_deleted = FALSE ORDER BY created_at DESC`, - authorID, - ) - if err != nil { - return nil, err - } - defer rows.Close() + authorID) +} - var scenarios []*repos.Scenario - for rows.Next() { - scenario := &repos.Scenario{} - var authorID *int - var authorUsername *string - err := rows.Scan( - &scenario.ID, - &scenario.Name, - &scenario.Description, - &scenario.Image, - &scenario.Scenario, - &authorID, - &authorUsername, - &scenario.Status, - &scenario.UpdatedAt, - &scenario.CreatedAt, - &scenario.PublishedAt, - ) - if err != nil { - return nil, err - } - if authorID != nil && authorUsername != nil { - scenario.Author = &repos.User{ID: *authorID, Username: *authorUsername} - } - scenarios = append(scenarios, scenario) - } - if err := rows.Err(); err != nil { - return nil, err - } - - return scenarios, nil +// GetAllScenarios returns all non-deleted scenarios. +func (r *ScenariosRepo) GetAllScenarios(ctx context.Context) ([]*repos.Scenario, error) { + return r.getScenarios(ctx, `SELECT + scenarios.id, + scenarios.name, + scenarios.description, + scenarios.image, + scenarios.scenario, + users.id, + users.username, + scenarios.status, + scenarios.updated_at, + scenarios.created_at, + scenarios.published_at + FROM scenarios + LEFT JOIN users on scenarios.author_id = users.id + WHERE is_deleted = FALSE + ORDER BY created_at DESC`) } // GetScenariosByStatus returns scenarios with the requested status. @@ -117,9 +98,7 @@ func (r *ScenariosRepo) GetScenariosByStatus( ctx context.Context, status string, ) ([]*repos.Scenario, error) { - rows, err := r.pool.Query( - ctx, - `SELECT + return r.getScenarios(ctx, `SELECT scenarios.id, scenarios.name, scenarios.description, @@ -135,8 +114,11 @@ func (r *ScenariosRepo) GetScenariosByStatus( LEFT JOIN users on scenarios.author_id = users.id WHERE scenarios.status = $1 and is_deleted = FALSE ORDER BY created_at DESC`, - status, - ) + status) +} + +func (r *ScenariosRepo) getScenarios(ctx context.Context, query string, args ...any) ([]*repos.Scenario, error) { + rows, err := r.pool.Query(ctx, query, args...) if err != nil { return nil, err } diff --git a/internal/services/scenarios_service/service.go b/internal/services/scenarios_service/service.go index ef1f453..59786d7 100644 --- a/internal/services/scenarios_service/service.go +++ b/internal/services/scenarios_service/service.go @@ -17,6 +17,7 @@ import ( type IScenariosRepository interface { AddScenario(ctx context.Context, name string, authorID int) (int, error) GetScenariosByAuthorID(ctx context.Context, authorID int) ([]*repos.Scenario, error) + GetAllScenarios(ctx context.Context) ([]*repos.Scenario, error) GetScenariosByStatus(ctx context.Context, status string) ([]*repos.Scenario, error) GetScenarioByID(ctx context.Context, id int) (*repos.Scenario, error) UpdateScenarioByID(ctx context.Context, id int, name string, description string, image string) error @@ -77,6 +78,15 @@ func (s *ScenarioService) GetScenariosByAuthorID( return mapScenariosLight(scenarios, s.domain), nil } +// GetAllScenarios returns all non-deleted scenarios. +func (s *ScenarioService) GetAllScenarios(ctx context.Context) ([]*Scenario, error) { + scenarios, err := s.scenariosRepo.GetAllScenarios(ctx) + if err != nil { + return nil, err + } + return mapScenariosLight(scenarios, s.domain), nil +} + // GetScenariosCatalog returns scenarios available in the catalog. func (s *ScenarioService) GetScenariosCatalog( ctx context.Context, diff --git a/internal/services/scenarios_service/service_test.go b/internal/services/scenarios_service/service_test.go index aff6abc..120c8a3 100644 --- a/internal/services/scenarios_service/service_test.go +++ b/internal/services/scenarios_service/service_test.go @@ -193,6 +193,16 @@ func (r *fakeScenariosRepo) GetScenariosByAuthorID(_ context.Context, authorID i return res, nil } +func (r *fakeScenariosRepo) GetAllScenarios(_ context.Context) ([]*repos.Scenario, error) { + r.mu.Lock() + defer r.mu.Unlock() + res := make([]*repos.Scenario, 0, len(r.byID)) + for _, s := range r.byID { + res = append(res, s) + } + return res, nil +} + func (r *fakeScenariosRepo) GetScenariosByStatus(_ context.Context, status string) ([]*repos.Scenario, error) { r.mu.Lock() defer r.mu.Unlock()