generated from VLADIMIR/template
add games operations
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
package repos
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type Game struct {
|
||||
ID int
|
||||
Name string
|
||||
Description string
|
||||
StartAt time.Time
|
||||
Status string
|
||||
ScenarioId int
|
||||
Scenario *Scenario
|
||||
Teams []*Team
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
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 = errors.New("Игра не найдена")
|
||||
)
|
||||
|
||||
type GamesRepo struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewGamesRepo(pool *pgxpool.Pool) *GamesRepo {
|
||||
return &GamesRepo{
|
||||
pool: pool,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *GamesRepo) AddGame(
|
||||
ctx context.Context,
|
||||
name string,
|
||||
description string,
|
||||
startAt time.Time,
|
||||
scenarioId int32,
|
||||
) (int, error) {
|
||||
id := 0
|
||||
err := r.pool.QueryRow(
|
||||
ctx,
|
||||
`INSERT INTO games (name, description, start_at, scenario_id)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING id`,
|
||||
name,
|
||||
description,
|
||||
startAt,
|
||||
scenarioId,
|
||||
).Scan(&id)
|
||||
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
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.start_at,
|
||||
games.status,
|
||||
games.scenario_id
|
||||
FROM games
|
||||
WHERE id = $1`,
|
||||
id,
|
||||
)
|
||||
err := row.Scan(
|
||||
&game.ID,
|
||||
&game.Name,
|
||||
&game.Description,
|
||||
&game.StartAt,
|
||||
&game.Status,
|
||||
&game.ScenarioId,
|
||||
)
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, ErrGameNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return game, nil
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func (r *GamesRepo) GetGames(
|
||||
ctx context.Context,
|
||||
) ([]*repos.Game, error) {
|
||||
rows, err := r.pool.Query(
|
||||
ctx,
|
||||
`SELECT
|
||||
games.id,
|
||||
games.name,
|
||||
games.description,
|
||||
games.start_at,
|
||||
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.StartAt,
|
||||
&game.Status,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
games = append(games, game)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return games, nil
|
||||
}
|
||||
|
||||
func (r *GamesRepo) UpdateGame(
|
||||
ctx context.Context,
|
||||
id int,
|
||||
name string,
|
||||
description string,
|
||||
startAt time.Time,
|
||||
scenarioId int32,
|
||||
) error {
|
||||
tag, err := r.pool.Exec(
|
||||
ctx,
|
||||
`UPDATE games
|
||||
SET
|
||||
name = $1,
|
||||
description = $2,
|
||||
start_at = $3,
|
||||
scenario_id = $4,
|
||||
updated_at = NOW()
|
||||
WHERE id = $5`,
|
||||
name,
|
||||
description,
|
||||
startAt,
|
||||
scenarioId,
|
||||
id,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if tag.RowsAffected() == 0 {
|
||||
return ErrGameNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -23,13 +23,13 @@ func NewScenariosRepo(pool *pgxpool.Pool) *ScenariosRepo {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ScenariosRepo) AddScenario(
|
||||
func (r *ScenariosRepo) AddScenario(
|
||||
ctx context.Context,
|
||||
name string,
|
||||
authorId int,
|
||||
) (int, error) {
|
||||
id := 0
|
||||
err := s.pool.QueryRow(
|
||||
err := r.pool.QueryRow(
|
||||
ctx,
|
||||
`INSERT INTO scenarios (name, author_id)
|
||||
VALUES ($1, $2)
|
||||
@@ -44,11 +44,11 @@ func (s *ScenariosRepo) AddScenario(
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func (s *ScenariosRepo) GetScenariosByAuthorID(
|
||||
func (r *ScenariosRepo) GetScenariosByAuthorID(
|
||||
ctx context.Context,
|
||||
authorId int,
|
||||
) ([]*repos.Scenario, error) {
|
||||
rows, err := s.pool.Query(
|
||||
rows, err := r.pool.Query(
|
||||
ctx,
|
||||
`SELECT
|
||||
scenarios.id,
|
||||
@@ -103,11 +103,11 @@ func (s *ScenariosRepo) GetScenariosByAuthorID(
|
||||
return scenarios, nil
|
||||
}
|
||||
|
||||
func (s *ScenariosRepo) GetScenariosByStatus(
|
||||
func (r *ScenariosRepo) GetScenariosByStatus(
|
||||
ctx context.Context,
|
||||
status string,
|
||||
) ([]*repos.Scenario, error) {
|
||||
rows, err := s.pool.Query(
|
||||
rows, err := r.pool.Query(
|
||||
ctx,
|
||||
`SELECT
|
||||
scenarios.id,
|
||||
@@ -162,14 +162,14 @@ func (s *ScenariosRepo) GetScenariosByStatus(
|
||||
return scenarios, nil
|
||||
}
|
||||
|
||||
func (s *ScenariosRepo) GetScenarioByID(
|
||||
func (r *ScenariosRepo) GetScenarioByID(
|
||||
ctx context.Context,
|
||||
id int,
|
||||
) (*repos.Scenario, error) {
|
||||
scenario := &repos.Scenario{
|
||||
Author: &repos.User{},
|
||||
}
|
||||
row := s.pool.QueryRow(
|
||||
row := r.pool.QueryRow(
|
||||
ctx,
|
||||
`SELECT
|
||||
scenarios.id,
|
||||
@@ -213,14 +213,14 @@ func (s *ScenariosRepo) GetScenarioByID(
|
||||
return scenario, nil
|
||||
}
|
||||
|
||||
func (s *ScenariosRepo) UpdateScenarioByID(
|
||||
func (r *ScenariosRepo) UpdateScenarioByID(
|
||||
ctx context.Context,
|
||||
id int,
|
||||
name string,
|
||||
description string,
|
||||
image string,
|
||||
) error {
|
||||
tag, err := s.pool.Exec(
|
||||
tag, err := r.pool.Exec(
|
||||
ctx,
|
||||
`UPDATE scenarios
|
||||
SET
|
||||
@@ -243,12 +243,12 @@ func (s *ScenariosRepo) UpdateScenarioByID(
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *ScenariosRepo) UpdateScenarioStatusByID(
|
||||
func (r *ScenariosRepo) UpdateScenarioStatusByID(
|
||||
ctx context.Context,
|
||||
id int,
|
||||
status string,
|
||||
) error {
|
||||
tag, err := s.pool.Exec(
|
||||
tag, err := r.pool.Exec(
|
||||
ctx,
|
||||
`UPDATE scenarios
|
||||
SET
|
||||
@@ -268,11 +268,11 @@ func (s *ScenariosRepo) UpdateScenarioStatusByID(
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *ScenariosRepo) DeleteScenarioByID(
|
||||
func (r *ScenariosRepo) DeleteScenarioByID(
|
||||
ctx context.Context,
|
||||
id int,
|
||||
) error {
|
||||
tag, err := s.pool.Exec(
|
||||
tag, err := r.pool.Exec(
|
||||
ctx,
|
||||
`UPDATE scenarios
|
||||
SET
|
||||
@@ -289,12 +289,12 @@ func (s *ScenariosRepo) DeleteScenarioByID(
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *ScenariosRepo) GetStoryByScenarioID(
|
||||
func (r *ScenariosRepo) GetStoryByScenarioID(
|
||||
ctx context.Context,
|
||||
id int,
|
||||
) (string, error) {
|
||||
story := ""
|
||||
row := s.pool.QueryRow(
|
||||
row := r.pool.QueryRow(
|
||||
ctx,
|
||||
`SELECT
|
||||
scenario
|
||||
@@ -315,12 +315,12 @@ func (s *ScenariosRepo) GetStoryByScenarioID(
|
||||
return story, nil
|
||||
}
|
||||
|
||||
func (s *ScenariosRepo) UpdateStoryByScenarioID(
|
||||
func (r *ScenariosRepo) UpdateStoryByScenarioID(
|
||||
ctx context.Context,
|
||||
id int,
|
||||
story string,
|
||||
) error {
|
||||
tag, err := s.pool.Exec(
|
||||
tag, err := r.pool.Exec(
|
||||
ctx,
|
||||
`UPDATE scenarios
|
||||
SET
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package repos
|
||||
|
||||
type Team struct {
|
||||
ID int
|
||||
Name string
|
||||
Password string
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package teams_repo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"evening_detective_server/internal/repos"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type TeamsRepo struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewTeamsRepo(pool *pgxpool.Pool) *TeamsRepo {
|
||||
return &TeamsRepo{
|
||||
pool: pool,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *TeamsRepo) GetTeamsByGameID(
|
||||
ctx context.Context,
|
||||
gameId int,
|
||||
) ([]*repos.Team, error) {
|
||||
rows, err := r.pool.Query(
|
||||
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 scenarios.author_id = $1 and is_deleted = FALSE
|
||||
ORDER BY created_at DESC`,
|
||||
gameId,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var teams []*repos.Team
|
||||
for rows.Next() {
|
||||
team := &repos.Team{}
|
||||
err := rows.Scan(
|
||||
&team.ID,
|
||||
&team.Name,
|
||||
&team.Password,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
teams = append(teams, team)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return teams, nil
|
||||
}
|
||||
Reference in New Issue
Block a user