generated from VLADIMIR/template
up
This commit is contained in:
+1
-1
@@ -17,7 +17,7 @@
|
||||
|
||||
# Dependency directories (remove the comment below to include it)
|
||||
# vendor/
|
||||
/bin/
|
||||
# /bin/
|
||||
|
||||
# Go workspace file
|
||||
go.work
|
||||
|
||||
Executable
BIN
Binary file not shown.
@@ -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, "не удалось получить сценарии автора")
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user