generated from VLADIMIR/template
315 lines
5.2 KiB
Go
315 lines
5.2 KiB
Go
// Package games_repo provides database access for games.
|
|
package games_repo
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"evening_detective_server/internal/repos"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
var (
|
|
// ErrGameNotFound indicates that a game was not found.
|
|
ErrGameNotFound = errors.New("игра не найдена")
|
|
)
|
|
|
|
// GamesRepo persists games.
|
|
type GamesRepo struct {
|
|
pool *pgxpool.Pool
|
|
}
|
|
|
|
// NewGamesRepo creates a games repository.
|
|
func NewGamesRepo(pool *pgxpool.Pool) *GamesRepo {
|
|
return &GamesRepo{
|
|
pool: pool,
|
|
}
|
|
}
|
|
|
|
// AddGame stores a game.
|
|
func (r *GamesRepo) AddGame(
|
|
ctx context.Context,
|
|
name string,
|
|
description string,
|
|
location *string,
|
|
startAt time.Time,
|
|
scenarioID int32,
|
|
format string,
|
|
) (int, error) {
|
|
id := 0
|
|
err := r.pool.QueryRow(
|
|
ctx,
|
|
`INSERT INTO games (name, description, location, start_at, scenario_id, format)
|
|
VALUES ($1, $2, $3, $4, $5, $6)
|
|
RETURNING id`,
|
|
name,
|
|
description,
|
|
location,
|
|
startAt,
|
|
scenarioID,
|
|
format,
|
|
).Scan(&id)
|
|
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return id, nil
|
|
}
|
|
|
|
// GetGameByID returns a game by ID.
|
|
func (r *GamesRepo) GetGameByID(
|
|
ctx context.Context,
|
|
id int,
|
|
) (*repos.Game, error) {
|
|
game := &repos.Game{}
|
|
row := r.pool.QueryRow(
|
|
ctx,
|
|
`SELECT
|
|
games.id,
|
|
games.name,
|
|
games.description,
|
|
games.location,
|
|
games.start_at,
|
|
games.format,
|
|
games.status,
|
|
games.scenario_id,
|
|
games.started_at,
|
|
games.ended_at
|
|
FROM games
|
|
WHERE id = $1`,
|
|
id,
|
|
)
|
|
err := row.Scan(
|
|
&game.ID,
|
|
&game.Name,
|
|
&game.Description,
|
|
&game.Location,
|
|
&game.StartAt,
|
|
&game.Format,
|
|
&game.Status,
|
|
&game.ScenarioID,
|
|
&game.StartedAt,
|
|
&game.EndedAt,
|
|
)
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return nil, ErrGameNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
return game, nil
|
|
}
|
|
|
|
// DeleteGameByID removes a game by ID.
|
|
func (r *GamesRepo) DeleteGameByID(
|
|
ctx context.Context,
|
|
id int,
|
|
) error {
|
|
tag, err := r.pool.Exec(
|
|
ctx,
|
|
`DELETE FROM games
|
|
WHERE id = $1`,
|
|
id,
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if tag.RowsAffected() == 0 {
|
|
return ErrGameNotFound
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GetGames returns games matching the requested filters.
|
|
func (r *GamesRepo) GetGames(
|
|
ctx context.Context,
|
|
) ([]*repos.Game, error) {
|
|
rows, err := r.pool.Query(
|
|
ctx,
|
|
`SELECT
|
|
games.id,
|
|
games.name,
|
|
games.description,
|
|
games.location,
|
|
games.start_at,
|
|
games.format,
|
|
games.status
|
|
FROM games
|
|
ORDER BY created_at DESC`,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var games []*repos.Game
|
|
for rows.Next() {
|
|
game := &repos.Game{}
|
|
err := rows.Scan(
|
|
&game.ID,
|
|
&game.Name,
|
|
&game.Description,
|
|
&game.Location,
|
|
&game.StartAt,
|
|
&game.Format,
|
|
&game.Status,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
games = append(games, game)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return games, nil
|
|
}
|
|
|
|
// UpdateGame updates a game's editable fields.
|
|
func (r *GamesRepo) UpdateGame(
|
|
ctx context.Context,
|
|
id int,
|
|
name string,
|
|
description string,
|
|
location *string,
|
|
startAt time.Time,
|
|
scenarioID int32,
|
|
format string,
|
|
) error {
|
|
tag, err := r.pool.Exec(
|
|
ctx,
|
|
`UPDATE games
|
|
SET
|
|
name = $1,
|
|
description = $2,
|
|
location = $3,
|
|
start_at = $4,
|
|
scenario_id = $5,
|
|
format = $6,
|
|
updated_at = NOW()
|
|
WHERE id = $7`,
|
|
name,
|
|
description,
|
|
location,
|
|
startAt,
|
|
scenarioID,
|
|
format,
|
|
id,
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if tag.RowsAffected() == 0 {
|
|
return ErrGameNotFound
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// StartGame marks a game as started.
|
|
func (r *GamesRepo) StartGame(ctx context.Context, id int) error {
|
|
tag, err := r.pool.Exec(
|
|
ctx,
|
|
`UPDATE games
|
|
SET
|
|
status = 'run',
|
|
started_at = NOW(),
|
|
updated_at = NOW()
|
|
WHERE id = $1`,
|
|
id,
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if tag.RowsAffected() == 0 {
|
|
return ErrGameNotFound
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// PauseGame marks a game as paused.
|
|
func (r *GamesRepo) PauseGame(ctx context.Context, id int) error {
|
|
tag, err := r.pool.Exec(
|
|
ctx,
|
|
`UPDATE games
|
|
SET
|
|
status = 'pause',
|
|
updated_at = NOW()
|
|
WHERE id = $1`,
|
|
id,
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if tag.RowsAffected() == 0 {
|
|
return ErrGameNotFound
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// PlayGame resumes a paused game.
|
|
func (r *GamesRepo) PlayGame(ctx context.Context, id int) error {
|
|
tag, err := r.pool.Exec(
|
|
ctx,
|
|
`UPDATE games
|
|
SET
|
|
status = 'run',
|
|
updated_at = NOW()
|
|
WHERE id = $1`,
|
|
id,
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if tag.RowsAffected() == 0 {
|
|
return ErrGameNotFound
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// FinishGame marks a game as finished.
|
|
func (r *GamesRepo) FinishGame(ctx context.Context, id int) error {
|
|
tag, err := r.pool.Exec(
|
|
ctx,
|
|
`UPDATE games
|
|
SET
|
|
status = 'finish',
|
|
ended_at = NOW(),
|
|
updated_at = NOW()
|
|
WHERE id = $1`,
|
|
id,
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if tag.RowsAffected() == 0 {
|
|
return ErrGameNotFound
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ResetGame resets a game's state.
|
|
func (r *GamesRepo) ResetGame(ctx context.Context, id int) error {
|
|
tag, err := r.pool.Exec(
|
|
ctx,
|
|
`UPDATE games
|
|
SET
|
|
status = 'draft',
|
|
started_at = DEFAULT,
|
|
ended_at = DEFAULT,
|
|
updated_at = NOW()
|
|
WHERE id = $1`,
|
|
id,
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if tag.RowsAffected() == 0 {
|
|
return ErrGameNotFound
|
|
}
|
|
return nil
|
|
}
|