Feat: room and actions repos
This commit is contained in:
46
repos/actions.go
Normal file
46
repos/actions.go
Normal 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
|
||||
}
|
Reference in New Issue
Block a user