Chore: actions methods rename

This commit is contained in:
Grail Finder
2025-07-04 14:30:13 +03:00
parent 705881f1ea
commit 2a2bf4e23d
5 changed files with 23 additions and 20 deletions

View File

@ -121,7 +121,7 @@ func (cm *CronManager) CleanupActions() {
}
}()
if err := cm.repo.ActionsDeleteOrphaned(ctx); err != nil {
if err := cm.repo.ActionDeleteOrphaned(ctx); err != nil {
cm.log.Error("failed to delete orphaned actions", "err", err)
if err := tx.Rollback(); err != nil {
cm.log.Error("failed to rollback transaction for actions cleanup", "err", err)
@ -133,4 +133,3 @@ func (cm *CronManager) CleanupActions() {
cm.log.Error("failed to commit transaction for actions cleanup", "err", err)
}
}

View File

@ -68,6 +68,11 @@ func HandleShowColor(w http.ResponseWriter, r *http.Request) {
WordColor: string(color),
Action: models.ActionTypeGuess,
Word: word,
RoomID: fi.Room.ID,
}
if err := repo.ActionCreate(r.Context(), &action); err != nil {
abortWithError(w, err.Error())
return
}
fi.Room.ActionHistory = append(fi.Room.ActionHistory, action)
// if opened card is of color of opp team, change turn

View File

@ -200,7 +200,7 @@ func HandleStartGame(w http.ResponseWriter, r *http.Request) {
// Save action history
action.RoomID = fi.Room.ID
action.CreatedAt = time.Now()
if err := repo.CreateAction(ctx, &action); err != nil {
if err := repo.ActionCreate(ctx, &action); err != nil {
if err := tx.Rollback(); err != nil {
log.Error("failed to rollback transaction", "error", err)
}
@ -338,7 +338,7 @@ func HandleGiveClue(w http.ResponseWriter, r *http.Request) {
Number: num,
}
fi.Room.ActionHistory = append(fi.Room.ActionHistory, action)
if err := repo.CreateAction(r.Context(), &action); err != nil {
if err := repo.ActionCreate(r.Context(), &action); err != nil {
abortWithError(w, err.Error())
return
}

View File

@ -8,14 +8,14 @@ import (
)
type ActionsRepo interface {
ListActions(ctx context.Context, roomID string) ([]models.Action, error)
CreateAction(ctx context.Context, action *models.Action) error
GetLastClue(ctx context.Context, roomID string) (*models.Action, error)
DeleteActionsByRoomID(ctx context.Context, roomID string) error
ActionsDeleteOrphaned(ctx context.Context) error
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
}
func (p *RepoProvider) ListActions(ctx context.Context, roomID string) ([]models.Action, 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 {
@ -24,13 +24,13 @@ func (p *RepoProvider) ListActions(ctx context.Context, roomID string) ([]models
return actions, nil
}
func (p *RepoProvider) CreateAction(ctx context.Context, a *models.Action) error {
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, a.CreatedAt.UnixNano())
return err
}
func (p *RepoProvider) GetLastClue(ctx context.Context, roomID string) (*models.Action, error) {
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 {
@ -39,13 +39,13 @@ func (p *RepoProvider) GetLastClue(ctx context.Context, roomID string) (*models.
return action, nil
}
func (p *RepoProvider) DeleteActionsByRoomID(ctx context.Context, roomID string) error {
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) ActionsDeleteOrphaned(ctx context.Context) error {
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

View File

@ -35,7 +35,7 @@ func setupActionsTestDB(t *testing.T) (*sqlx.DB, func()) {
}
}
func TestActionsRepo_CreateAction(t *testing.T) {
func TestActionsRepo_ActionCreate(t *testing.T) {
db, teardown := setupActionsTestDB(t)
defer teardown()
@ -53,7 +53,7 @@ func TestActionsRepo_CreateAction(t *testing.T) {
RoomID: roomID,
}
err := repo.CreateAction(context.Background(), action)
err := repo.ActionCreate(context.Background(), action)
assert.NoError(t, err)
var retrievedAction models.Action
@ -95,7 +95,7 @@ func TestActionsRepo_ListActions(t *testing.T) {
_, err = db.Exec(`INSERT INTO actions (room_id, actor, actor_color, action_type, word, word_color, number_associated, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, roomID, action2.Actor, action2.ActorColor, action2.Action, action2.Word, action2.WordColor, action2.Number, action2.CreatedAt)
assert.NoError(t, err)
actions, err := repo.ListActions(context.Background(), roomID)
actions, err := repo.ActionList(context.Background(), roomID)
assert.NoError(t, err)
assert.Len(t, actions, 2)
assert.Equal(t, action1.Word, actions[0].Word)
@ -145,7 +145,7 @@ func TestActionsRepo_GetLastClue(t *testing.T) {
_, err = db.Exec(`INSERT INTO actions (room_id, actor, actor_color, action_type, word, word_color, number_associated, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, roomID, action3.Actor, action3.ActorColor, action3.Action, action3.Word, action3.WordColor, action3.Number, action3.CreatedAt)
assert.NoError(t, err)
lastClue, err := repo.GetLastClue(context.Background(), roomID)
lastClue, err := repo.ActionGetLastClue(context.Background(), roomID)
assert.NoError(t, err)
assert.NotNil(t, lastClue)
assert.Equal(t, action2.Word, lastClue.Word)
@ -170,7 +170,7 @@ func TestActionsRepo_DeleteActionsByRoomID(t *testing.T) {
_, err := db.Exec(`INSERT INTO actions (room_id, actor, actor_color, action_type, word, word_color, number_associated, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, roomID, action1.Actor, action1.ActorColor, action1.Action, action1.Word, action1.WordColor, action1.Number, action1.CreatedAt)
assert.NoError(t, err)
err = repo.DeleteActionsByRoomID(context.Background(), roomID)
err = repo.ActionDeleteByRoomID(context.Background(), roomID)
assert.NoError(t, err)
var count int
@ -178,4 +178,3 @@ func TestActionsRepo_DeleteActionsByRoomID(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 0, count)
}