Feat: room and actions repos

This commit is contained in:
Grail Finder
2025-07-01 12:49:46 +03:00
parent 42348ff625
commit 83d3a19d05
6 changed files with 171 additions and 50 deletions

46
repos/actions.go Normal file
View File

@ -0,0 +1,46 @@
package repos
import (
"context"
"gralias/models"
"github.com/jackc/pgx/v5"
)
type ActionsRepo interface {
ListActions(ctx context.Context, roomID string) ([]models.Action, error)
CreateAction(ctx context.Context, roomID string, action *models.Action) error
GetLastClue(ctx context.Context, roomID string) (*models.Action, error)
DeleteActionsByRoomID(ctx context.Context, roomID string) error
}
func (p *RepoProvider) ListActions(ctx context.Context, roomID string) ([]models.Action, error) {
rows, err := p.DB.Query(ctx, `SELECT actor, actor_color, action_type, word, word_color, number_associated FROM actions WHERE room_id = $1 ORDER BY created_at ASC`, roomID)
if err != nil {
return nil, err
}
return pgx.CollectRows(rows, pgx.RowToStructByName[models.Action])
}
func (p *RepoProvider) CreateAction(ctx context.Context, roomID string, a *models.Action) error {
_, err := p.DB.Exec(ctx, `INSERT INTO actions (room_id, actor, actor_color, action_type, word, word_color, number_associated) VALUES ($1, $2, $3, $4, $5, $6, $7)`,
roomID, a.Actor, a.ActorColor, a.Action, a.Word, a.WordColor, a.Number)
return err
}
func (p *RepoProvider) GetLastClue(ctx context.Context, roomID string) (*models.Action, error) {
rows, err := p.DB.Query(ctx, `SELECT actor, actor_color, action_type, word, word_color, number_associated FROM actions WHERE room_id = $1 AND action_type = 'gave_clue' ORDER BY created_at DESC LIMIT 1`, roomID)
if err != nil {
return nil, err
}
action, err := pgx.CollectOneRow(rows, pgx.RowToStructByName[models.Action])
if err != nil {
return nil, err
}
return &action, nil
}
func (p *RepoProvider) DeleteActionsByRoomID(ctx context.Context, roomID string) error {
_, err := p.DB.Exec(ctx, `DELETE FROM actions WHERE room_id = $1`, roomID)
return err
}

View File

@ -1,9 +1,11 @@
package repos
import "github.com/jackc/pgx/v5/pgxpool"
type AllRepos interface {
RoomsRepo
}
type RepoProvider struct {
// db connection
DB *pgxpool.Pool
}

View File

@ -1,15 +1,55 @@
package repos
import "gralias/models"
import (
"context"
"gralias/models"
"github.com/jackc/pgx/v5"
)
type RoomsRepo interface {
ListRooms() ([]models.Room, error)
GetRoomByID(id string) (*models.Room, error)
CreateRoom(room *models.Room) error
DeleteRoomByID(id string) error
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
}
// provider implementation
func (p RepoProvider) ListRooms() ([]models.Room, error) {
return nil, nil
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
}