Files
gralias/repos/rooms.go
2025-07-01 12:49:46 +03:00

56 lines
2.4 KiB
Go

package repos
import (
"context"
"gralias/models"
"github.com/jackc/pgx/v5"
)
type RoomsRepo interface {
ListRooms(ctx context.Context) ([]models.Room, error)
GetRoomByID(ctx context.Context, id string) (*models.Room, error)
CreateRoom(ctx context.Context, room *models.Room) error
DeleteRoomByID(ctx context.Context, id string) error
UpdateRoom(ctx context.Context, room *models.Room) error
}
func (p *RepoProvider) ListRooms(ctx context.Context) ([]models.Room, error) {
rows, err := p.DB.Query(ctx, `SELECT * FROM rooms`)
if err != nil {
return nil, err
}
rooms, err := pgx.CollectRows(rows, pgx.RowToStructByName[models.Room])
if err != nil {
return nil, err
}
return rooms, nil
}
func (p *RepoProvider) GetRoomByID(ctx context.Context, id string) (*models.Room, error) {
rows, err := p.DB.Query(ctx, `SELECT * FROM rooms WHERE id = $1`, id)
if err != nil {
return nil, err
}
room, err := pgx.CollectOneRow(rows, pgx.RowToStructByName[models.Room])
if err != nil {
return nil, err
}
return &room, nil
}
func (p *RepoProvider) CreateRoom(ctx context.Context, r *models.Room) error {
_, err := p.DB.Exec(ctx, `INSERT INTO rooms (id, created_at, creator_name, team_turn, this_turn_limit, opened_this_turn, blue_counter, red_counter, red_turn, mime_done, is_public, is_running, language, round_time, is_over, team_won, room_pass) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17)`, r.ID, r.CreatedAt, r.CreatorName, r.TeamTurn, r.ThisTurnLimit, r.OpenedThisTurn, r.BlueCounter, r.RedCounter, r.RedTurn, r.MimeDone, r.IsPublic, r.IsRunning, r.Language, r.RoundTime, r.IsOver, r.TeamWon, r.Settings.RoomPass)
return err
}
func (p *RepoProvider) DeleteRoomByID(ctx context.Context, id string) error {
_, err := p.DB.Exec(ctx, `DELETE FROM rooms WHERE id = $1`, id)
return err
}
func (p *RepoProvider) UpdateRoom(ctx context.Context, r *models.Room) error {
_, err := p.DB.Exec(ctx, `UPDATE rooms SET team_turn = $1, this_turn_limit = $2, opened_this_turn = $3, blue_counter = $4, red_counter = $5, red_turn = $6, mime_done = $7, is_public = $8, is_running = $9, language = $10, round_time = $11, is_over = $12, team_won = $13, room_pass = $14 WHERE id = $15`, r.TeamTurn, r.ThisTurnLimit, r.OpenedThisTurn, r.BlueCounter, r.RedCounter, r.RedTurn, r.MimeDone, r.IsPublic, r.IsRunning, r.Language, r.RoundTime, r.IsOver, r.TeamWon, r.Settings.RoomPass, r.ID)
return err
}