generated from VLADIMIR/template
add game
This commit is contained in:
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user