generated from VLADIMIR/template
add teams operations
This commit is contained in:
@@ -3,5 +3,6 @@ package repos
|
||||
type Team struct {
|
||||
ID int
|
||||
Name string
|
||||
Status string
|
||||
Password string
|
||||
}
|
||||
|
||||
@@ -2,11 +2,16 @@ package teams_repo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"evening_detective_server/internal/repos"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrTeamNotFound = errors.New("Команда не найдена")
|
||||
)
|
||||
|
||||
type TeamsRepo struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
@@ -24,20 +29,12 @@ func (r *TeamsRepo) GetTeamsByGameID(
|
||||
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
|
||||
id,
|
||||
name,
|
||||
status,
|
||||
password
|
||||
FROM teams
|
||||
WHERE game_id = $1
|
||||
ORDER BY created_at DESC`,
|
||||
gameId,
|
||||
)
|
||||
@@ -52,6 +49,7 @@ func (r *TeamsRepo) GetTeamsByGameID(
|
||||
err := rows.Scan(
|
||||
&team.ID,
|
||||
&team.Name,
|
||||
&team.Status,
|
||||
&team.Password,
|
||||
)
|
||||
if err != nil {
|
||||
@@ -65,3 +63,65 @@ func (r *TeamsRepo) GetTeamsByGameID(
|
||||
|
||||
return teams, nil
|
||||
}
|
||||
|
||||
func (r *TeamsRepo) AddTeam(
|
||||
ctx context.Context,
|
||||
creatorId int,
|
||||
gameId int,
|
||||
name string,
|
||||
scenarioId int,
|
||||
password string,
|
||||
) (int, error) {
|
||||
id := 0
|
||||
err := r.pool.QueryRow(
|
||||
ctx,
|
||||
`INSERT INTO teams (name, creator_id, game_id, scenario_id, password)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
RETURNING id`,
|
||||
name,
|
||||
creatorId,
|
||||
gameId,
|
||||
scenarioId,
|
||||
password,
|
||||
).Scan(&id)
|
||||
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func (r *TeamsRepo) UpdateTeam(ctx context.Context, teamId int, name string) error {
|
||||
tag, err := r.pool.Exec(
|
||||
ctx,
|
||||
`UPDATE teams
|
||||
SET
|
||||
name = $1
|
||||
WHERE id = $2`,
|
||||
name,
|
||||
teamId,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if tag.RowsAffected() == 0 {
|
||||
return ErrTeamNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *TeamsRepo) DeleteTeam(ctx context.Context, teamId int) error {
|
||||
tag, err := r.pool.Exec(
|
||||
ctx,
|
||||
`DELETE FROM teams
|
||||
WHERE id = $1`,
|
||||
teamId,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if tag.RowsAffected() == 0 {
|
||||
return ErrTeamNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user