|
| 1 | +package postgres |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "database/sql" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "time" |
| 9 | + |
| 10 | + "CodeSCE/internal/core/models" |
| 11 | + "CodeSCE/internal/core/repositories" |
| 12 | +) |
| 13 | + |
| 14 | +type attemptRepository struct { |
| 15 | + db *sql.DB |
| 16 | +} |
| 17 | + |
| 18 | +func NewAttemptRepository(db *sql.DB) repositories.AttemptRepository { |
| 19 | + return &attemptRepository{db: db} |
| 20 | +} |
| 21 | + |
| 22 | +func (r *attemptRepository) Create(ctx context.Context, attempt *models.AssessmentAttempt) error { |
| 23 | + const query = ` |
| 24 | + INSERT INTO assessment_attempts ( |
| 25 | + invite_id, started_at, completed_at, status, total_score |
| 26 | + ) |
| 27 | + VALUES ($1, $2, $3, COALESCE(NULLIF($4, ''), 'in_progress'), $5) |
| 28 | + RETURNING id, status, created_at, updated_at |
| 29 | + ` |
| 30 | + if err := r.db.QueryRowContext( |
| 31 | + ctx, query, |
| 32 | + attempt.InviteID, |
| 33 | + attempt.StartedAt, |
| 34 | + attempt.CompletedAt, |
| 35 | + attempt.Status, |
| 36 | + attempt.TotalScore, |
| 37 | + ).Scan(&attempt.ID, &attempt.Status, &attempt.CreatedAt, &attempt.UpdatedAt); err != nil { |
| 38 | + return fmt.Errorf("create assessment attempt: %w", err) |
| 39 | + } |
| 40 | + return nil |
| 41 | +} |
| 42 | + |
| 43 | +func (r *attemptRepository) GetByID(ctx context.Context, id int64) (*models.AssessmentAttempt, error) { |
| 44 | + const query = ` |
| 45 | + SELECT id, invite_id, started_at, completed_at, status, total_score, created_at, updated_at |
| 46 | + FROM assessment_attempts |
| 47 | + WHERE id = $1 |
| 48 | + ` |
| 49 | + var attempt models.AssessmentAttempt |
| 50 | + err := r.db.QueryRowContext(ctx, query, id).Scan( |
| 51 | + &attempt.ID, |
| 52 | + &attempt.InviteID, |
| 53 | + &attempt.StartedAt, |
| 54 | + &attempt.CompletedAt, |
| 55 | + &attempt.Status, |
| 56 | + &attempt.TotalScore, |
| 57 | + &attempt.CreatedAt, |
| 58 | + &attempt.UpdatedAt, |
| 59 | + ) |
| 60 | + if errors.Is(err, sql.ErrNoRows) { |
| 61 | + return nil, repositories.ErrNotFound |
| 62 | + } |
| 63 | + if err != nil { |
| 64 | + return nil, fmt.Errorf("get assessment attempt by id: %w", err) |
| 65 | + } |
| 66 | + return &attempt, nil |
| 67 | +} |
| 68 | + |
| 69 | +func (r *attemptRepository) ListByAssessmentID(ctx context.Context, assessmentID int64) ([]models.AssessmentAttempt, error) { |
| 70 | + // Attempts link to assessments via assessment_invites.assessment_template_id. |
| 71 | + const query = ` |
| 72 | + SELECT a.id, a.invite_id, a.started_at, a.completed_at, a.status, |
| 73 | + a.total_score, a.created_at, a.updated_at |
| 74 | + FROM assessment_attempts a |
| 75 | + JOIN assessment_invites i ON i.id = a.invite_id |
| 76 | + WHERE i.assessment_template_id = $1 |
| 77 | + ORDER BY a.id |
| 78 | + ` |
| 79 | + rows, err := r.db.QueryContext(ctx, query, assessmentID) |
| 80 | + if err != nil { |
| 81 | + return nil, fmt.Errorf("list assessment attempts: %w", err) |
| 82 | + } |
| 83 | + defer rows.Close() |
| 84 | + |
| 85 | + var attempts []models.AssessmentAttempt |
| 86 | + for rows.Next() { |
| 87 | + var attempt models.AssessmentAttempt |
| 88 | + if err := rows.Scan( |
| 89 | + &attempt.ID, |
| 90 | + &attempt.InviteID, |
| 91 | + &attempt.StartedAt, |
| 92 | + &attempt.CompletedAt, |
| 93 | + &attempt.Status, |
| 94 | + &attempt.TotalScore, |
| 95 | + &attempt.CreatedAt, |
| 96 | + &attempt.UpdatedAt, |
| 97 | + ); err != nil { |
| 98 | + return nil, fmt.Errorf("scan assessment attempt: %w", err) |
| 99 | + } |
| 100 | + attempts = append(attempts, attempt) |
| 101 | + } |
| 102 | + if err := rows.Err(); err != nil { |
| 103 | + return nil, fmt.Errorf("iterate assessment attempts: %w", err) |
| 104 | + } |
| 105 | + return attempts, nil |
| 106 | +} |
| 107 | + |
| 108 | +func (r *attemptRepository) UpdateStatus(ctx context.Context, id int64, status string) error { |
| 109 | + const query = ` |
| 110 | + UPDATE assessment_attempts |
| 111 | + SET status = $2, updated_at = NOW() |
| 112 | + WHERE id = $1 |
| 113 | + ` |
| 114 | + return r.execAffectingOne(ctx, query, "update assessment attempt status", id, status) |
| 115 | +} |
| 116 | + |
| 117 | +func (r *attemptRepository) SetScore(ctx context.Context, id int64, score int) error { |
| 118 | + const query = ` |
| 119 | + UPDATE assessment_attempts |
| 120 | + SET total_score = $2, updated_at = NOW() |
| 121 | + WHERE id = $1 |
| 122 | + ` |
| 123 | + return r.execAffectingOne(ctx, query, "set assessment attempt score", id, score) |
| 124 | +} |
| 125 | + |
| 126 | +func (r *attemptRepository) SetCompletedAt(ctx context.Context, id int64, completedAt time.Time) error { |
| 127 | + const query = ` |
| 128 | + UPDATE assessment_attempts |
| 129 | + SET completed_at = $2, updated_at = NOW() |
| 130 | + WHERE id = $1 |
| 131 | + ` |
| 132 | + return r.execAffectingOne(ctx, query, "set assessment attempt completed_at", id, completedAt) |
| 133 | +} |
| 134 | + |
| 135 | +func (r *attemptRepository) execAffectingOne(ctx context.Context, query, op string, args ...any) error { |
| 136 | + res, err := r.db.ExecContext(ctx, query, args...) |
| 137 | + if err != nil { |
| 138 | + return fmt.Errorf("%s: %w", op, err) |
| 139 | + } |
| 140 | + rowsAffected, err := res.RowsAffected() |
| 141 | + if err != nil { |
| 142 | + return fmt.Errorf("%s rows affected: %w", op, err) |
| 143 | + } |
| 144 | + if rowsAffected == 0 { |
| 145 | + return repositories.ErrNotFound |
| 146 | + } |
| 147 | + return nil |
| 148 | +} |
0 commit comments