package repos import ( "context" "gralias/models" "time" "github.com/jmoiron/sqlx" ) type ActionsRepo interface { ActionList(ctx context.Context, roomID string) ([]models.Action, error) ActionCreate(ctx context.Context, action *models.Action) error ActionGetLastClue(ctx context.Context, roomID string) (*models.Action, error) ActionDeleteByRoomID(ctx context.Context, roomID string) error ActionDeleteOrphaned(ctx context.Context) error ActionGetLastTimeByRoomID(ctx context.Context, roomID string) (time.Time, error) } func (p *RepoProvider) ActionList(ctx context.Context, roomID string) ([]models.Action, error) { actions := []models.Action{} err := sqlx.SelectContext(ctx, p.DB, &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 } return actions, nil } func (p *RepoProvider) ActionCreate(ctx context.Context, a *models.Action) error { db := getDB(ctx, p.DB) _, err := db.ExecContext(ctx, `INSERT INTO actions (room_id, actor, actor_color, action_type, word, word_color, number_associated, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, a.RoomID, a.Actor, a.ActorColor, a.Action, a.Word, a.WordColor, a.Number, time.Now()) return err } func (p *RepoProvider) ActionGetLastTimeByRoomID(ctx context.Context, roomID string) (time.Time, error) { lastTime := time.Time{} err := sqlx.GetContext(ctx, p.DB, &lastTime, `SELECT created_at FROM actions WHERE room_id = ? ORDER BY created_at DESC LIMIT 1`, roomID) if err != nil { return lastTime, err } return lastTime, nil } func (p *RepoProvider) ActionGetLastClue(ctx context.Context, roomID string) (*models.Action, error) { action := &models.Action{} err := sqlx.GetContext(ctx, p.DB, 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 } return action, nil } func (p *RepoProvider) ActionDeleteByRoomID(ctx context.Context, roomID string) error { db := getDB(ctx, p.DB) _, err := db.ExecContext(ctx, `DELETE FROM actions WHERE room_id = ?`, roomID) return err } func (p *RepoProvider) ActionDeleteOrphaned(ctx context.Context) error { db := getDB(ctx, p.DB) _, err := db.ExecContext(ctx, `DELETE FROM actions WHERE room_id NOT IN (SELECT id FROM rooms)`) return err }