This commit is contained in:
2026-07-28 20:38:03 +07:00
parent f6934e9d5b
commit 20662dc9da
20 changed files with 729 additions and 233 deletions
+103
View File
@@ -0,0 +1,103 @@
package actions_repo
import (
"context"
"github.com/jackc/pgx/v5/pgxpool"
)
type ActionsRepo struct {
pool *pgxpool.Pool
}
func NewActionsRepo(pool *pgxpool.Pool) *ActionsRepo {
return &ActionsRepo{
pool: pool,
}
}
func (r *ActionsRepo) AddAction(ctx context.Context, teamId int, code string) (int, error) {
id := 0
err := r.pool.QueryRow(
ctx,
`INSERT INTO actions (code, team_id)
VALUES ($1, $2)
RETURNING id`,
code,
teamId,
).Scan(&id)
if err != nil {
return 0, err
}
return id, nil
}
func (r *ActionsRepo) GetActionsByTeamID(ctx context.Context, teamId int) ([]string, error) {
rows, err := r.pool.Query(
ctx,
`SELECT
code
FROM actions
WHERE team_id = $1
ORDER BY created_at ASC`,
teamId,
)
if err != nil {
return nil, err
}
defer rows.Close()
var codes []string
for rows.Next() {
code := ""
err := rows.Scan(
&code,
)
if err != nil {
return nil, err
}
codes = append(codes, code)
}
if err := rows.Err(); err != nil {
return nil, err
}
return codes, nil
}
func (r *ActionsRepo) GetActionsCountByTeamIDs(ctx context.Context, teamIds []int) (map[int]int, error) {
rows, err := r.pool.Query(
ctx,
`SELECT
team_id,
count(*)
FROM actions
WHERE team_id = ANY($1)
GROUP BY team_id`,
teamIds,
)
if err != nil {
return nil, err
}
defer rows.Close()
res := make(map[int]int, len(teamIds))
for rows.Next() {
id := 0
count := 0
err := rows.Scan(
&id,
&count,
)
if err != nil {
return nil, err
}
res[id] = count
}
if err := rows.Err(); err != nil {
return nil, err
}
return res, nil
}
+7
View File
@@ -0,0 +1,7 @@
package repos
type Application struct {
Name string
Image string
TeamId int
}
+81
View File
@@ -0,0 +1,81 @@
package applications_repo
import (
"context"
"evening_detective_server/internal/repos"
"github.com/jackc/pgx/v5/pgxpool"
)
type ApplicationsRepo struct {
pool *pgxpool.Pool
}
func NewApplicationsRepo(pool *pgxpool.Pool) *ApplicationsRepo {
return &ApplicationsRepo{
pool: pool,
}
}
func (r *ApplicationsRepo) AddApplication(
ctx context.Context,
teamId int,
name string,
image string,
) (int, error) {
id := 0
err := r.pool.QueryRow(
ctx,
`INSERT INTO applications (name, image, team_id)
VALUES ($1, $2, $3)
RETURNING id`,
name,
image,
teamId,
).Scan(&id)
if err != nil {
return 0, err
}
return id, nil
}
func (r *ApplicationsRepo) GetApplicationsByTeamIDsAndState(
ctx context.Context,
teamIds []int,
state string,
) (map[int][]*repos.Application, error) {
rows, err := r.pool.Query(
ctx,
`SELECT
name,
image,
team_id
FROM applications
WHERE team_id = ANY($1)`,
teamIds,
)
if err != nil {
return nil, err
}
defer rows.Close()
res := make(map[int][]*repos.Application, len(teamIds))
for rows.Next() {
application := &repos.Application{}
err := rows.Scan(
&application.Name,
&application.Image,
&application.TeamId,
)
if err != nil {
return nil, err
}
res[application.TeamId] = append(res[application.TeamId], application)
}
if err := rows.Err(); err != nil {
return nil, err
}
return res, nil
}
+5 -4
View File
@@ -1,8 +1,9 @@
package repos
type Team struct {
ID int
Name string
Status string
Password string
ID int
Name string
Status string
Password string
ScenarioId int
}
+32
View File
@@ -5,6 +5,7 @@ import (
"errors"
"evening_detective_server/internal/repos"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
)
@@ -22,6 +23,37 @@ func NewTeamsRepo(pool *pgxpool.Pool) *TeamsRepo {
}
}
func (r *TeamsRepo) GetTeamByID(ctx context.Context, id int) (*repos.Team, error) {
team := &repos.Team{}
row := r.pool.QueryRow(
ctx,
`SELECT
id,
name,
status,
password,
scenario_id
FROM teams
WHERE id = $1`,
id,
)
err := row.Scan(
&team.ID,
&team.Name,
&team.Status,
&team.Password,
&team.ScenarioId,
)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return nil, ErrTeamNotFound
}
return nil, err
}
return team, nil
}
func (r *TeamsRepo) GetTeamsByGameID(
ctx context.Context,
gameId int,