Files
gralias/repos/actions.go
2025-07-01 16:00:17 +03:00

46 lines
1.9 KiB
Go

package repos
import (
"context"
"gralias/models"
"time"
)
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) {
actions := []models.Action{}
err := p.DB.SelectContext(ctx, &actions, `SELECT actor, actor_color, action_type, word, word_color, number_associated, created_at FROM actions WHERE room_id = ? ORDER BY created_at ASC`, roomID)
if err != nil {
return nil, err
}
for i := range actions {
actions[i].CreatedAt = time.Unix(0, actions[i].CreatedAtUnix)
}
return actions, nil
}
func (p *RepoProvider) CreateAction(ctx context.Context, roomID string, a *models.Action) error {
_, err := p.DB.ExecContext(ctx, `INSERT INTO actions (room_id, actor, actor_color, action_type, word, word_color, number_associated, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, roomID, a.Actor, a.ActorColor, a.Action, a.Word, a.WordColor, a.Number, a.CreatedAt.UnixNano())
return err
}
func (p *RepoProvider) GetLastClue(ctx context.Context, roomID string) (*models.Action, error) {
action := &models.Action{}
err := p.DB.GetContext(ctx, action, `SELECT actor, actor_color, action_type, word, word_color, number_associated, created_at FROM actions WHERE room_id = ? AND action_type = 'gave_clue' ORDER BY created_at DESC LIMIT 1`, roomID)
if err != nil {
return nil, err
}
action.CreatedAt = time.Unix(0, action.CreatedAtUnix)
return action, nil
}
func (p *RepoProvider) DeleteActionsByRoomID(ctx context.Context, roomID string) error {
_, err := p.DB.ExecContext(ctx, `DELETE FROM actions WHERE room_id = ?`, roomID)
return err
}