Enha: sqlx instead of pgx
This commit is contained in:
@ -3,8 +3,7 @@ package repos
|
||||
import (
|
||||
"context"
|
||||
"gralias/models"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ActionsRepo interface {
|
||||
@ -15,32 +14,33 @@ type ActionsRepo interface {
|
||||
}
|
||||
|
||||
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)
|
||||
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
|
||||
}
|
||||
return pgx.CollectRows(rows, pgx.RowToStructByName[models.Action])
|
||||
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.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)
|
||||
_, 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) {
|
||||
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)
|
||||
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, err := pgx.CollectOneRow(rows, pgx.RowToStructByName[models.Action])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &action, nil
|
||||
action.CreatedAt = time.Unix(0, action.CreatedAtUnix)
|
||||
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)
|
||||
_, err := p.DB.ExecContext(ctx, `DELETE FROM actions WHERE room_id = ?`, roomID)
|
||||
return err
|
||||
}
|
Reference in New Issue
Block a user