Files
evening_detective_server/internal/repos/scenarios_repo/repo.go
T
2026-10-07 01:43:28 +07:00

349 lines
6.9 KiB
Go

// Package scenarios_repo persists scenarios.
package scenarios_repo
import (
"context"
"errors"
"evening_detective_server/internal/repos"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
)
var (
// ErrScenarioNotFound indicates that a scenario was not found.
ErrScenarioNotFound = errors.New("сценарий не найден")
)
// ScenariosRepo persists scenarios.
type ScenariosRepo struct {
pool *pgxpool.Pool
}
// NewScenariosRepo creates a scenarios repository.
func NewScenariosRepo(pool *pgxpool.Pool) *ScenariosRepo {
return &ScenariosRepo{
pool: pool,
}
}
// AddScenario stores a scenario.
func (r *ScenariosRepo) AddScenario(
ctx context.Context,
name string,
authorID int,
) (int, error) {
id := 0
err := r.pool.QueryRow(
ctx,
`INSERT INTO scenarios (name, author_id)
VALUES ($1, $2)
RETURNING id`,
name,
authorID,
).Scan(&id)
if err != nil {
return 0, err
}
return id, nil
}
// GetScenariosByAuthorID returns scenarios created by an author.
func (r *ScenariosRepo) GetScenariosByAuthorID(
ctx context.Context,
authorID int,
) ([]*repos.Scenario, error) {
return r.getScenarios(ctx, `SELECT
scenarios.id,
scenarios.name,
scenarios.description,
scenarios.image,
scenarios.playlist_id,
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 scenarios.author_id = $1 and is_deleted = FALSE
ORDER BY created_at DESC`,
authorID)
}
// 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.playlist_id,
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.
func (r *ScenariosRepo) GetScenariosByStatus(
ctx context.Context,
status string,
) ([]*repos.Scenario, error) {
return r.getScenarios(ctx, `SELECT
scenarios.id,
scenarios.name,
scenarios.description,
scenarios.image,
scenarios.playlist_id,
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 scenarios.status = $1 and is_deleted = FALSE
ORDER BY created_at DESC`,
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
}
defer rows.Close()
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.PlaylistID,
&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
}
// GetScenarioByID returns a scenario by ID.
func (r *ScenariosRepo) GetScenarioByID(
ctx context.Context,
id int,
) (*repos.Scenario, error) {
scenario := &repos.Scenario{}
var authorID *int
var authorUsername *string
row := r.pool.QueryRow(
ctx,
`SELECT
scenarios.id,
scenarios.name,
scenarios.description,
scenarios.image,
scenarios.playlist_id,
scenarios.scenario,
users.id,
users.username,
scenarios.status,
scenarios.updated_at,
scenarios.created_at,
scenarios.published_at,
scenarios.is_deleted
FROM scenarios
LEFT JOIN users on scenarios.author_id = users.id
WHERE scenarios.id = $1`,
id,
)
err := row.Scan(
&scenario.ID,
&scenario.Name,
&scenario.Description,
&scenario.Image,
&scenario.PlaylistID,
&scenario.Scenario,
&authorID,
&authorUsername,
&scenario.Status,
&scenario.UpdatedAt,
&scenario.CreatedAt,
&scenario.PublishedAt,
&scenario.IsDeleted,
)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return nil, ErrScenarioNotFound
}
return nil, err
}
if authorID != nil && authorUsername != nil {
scenario.Author = &repos.User{ID: *authorID, Username: *authorUsername}
}
return scenario, nil
}
func (r *ScenariosRepo) UpdateScenarioByID(
ctx context.Context,
id int,
name string,
description string,
image string,
) error {
tag, err := r.pool.Exec(
ctx,
`UPDATE scenarios
SET
name = $1,
description = $2,
image = $3,
updated_at = NOW()
WHERE id = $4`,
name,
description,
image,
id,
)
if err != nil {
return err
}
if tag.RowsAffected() == 0 {
return ErrScenarioNotFound
}
return nil
}
func (r *ScenariosRepo) UpdateScenarioStatusByID(
ctx context.Context,
id int,
status string,
allowPublished bool,
) (bool, error) {
tag, err := r.pool.Exec(
ctx,
`UPDATE scenarios
SET
status = $1::varchar,
published_at = CASE
WHEN $1::varchar = 'public' AND published_at IS NULL THEN NOW()
ELSE published_at
END,
updated_at = NOW()
WHERE id = $2 AND ($3 OR status IS DISTINCT FROM 'public')`,
status,
id,
allowPublished,
)
if err != nil {
return false, err
}
return tag.RowsAffected() > 0, nil
}
func (r *ScenariosRepo) DeleteScenarioByID(
ctx context.Context,
id int,
allowPublished bool,
) (bool, error) {
tag, err := r.pool.Exec(
ctx,
`UPDATE scenarios
SET
is_deleted = TRUE
WHERE id = $1 AND ($2 OR status <> 'public')`,
id,
allowPublished,
)
if err != nil {
return false, err
}
return tag.RowsAffected() > 0, nil
}
// GetStoryByScenarioID returns the story JSON for a scenario.
func (r *ScenariosRepo) GetStoryByScenarioID(
ctx context.Context,
id int,
) (string, error) {
story := ""
row := r.pool.QueryRow(
ctx,
`SELECT
scenario
FROM scenarios
WHERE scenarios.id = $1`,
id,
)
err := row.Scan(
&story,
)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return "", ErrScenarioNotFound
}
return "", err
}
return story, nil
}
func (r *ScenariosRepo) UpdateStoryByScenarioID(
ctx context.Context,
id int,
story string,
) error {
tag, err := r.pool.Exec(
ctx,
`UPDATE scenarios
SET
scenario = $1,
updated_at = NOW()
WHERE id = $2`,
story,
id,
)
if err != nil {
return err
}
if tag.RowsAffected() == 0 {
return ErrScenarioNotFound
}
return nil
}