Conform Go code to project conventions
- Propagate context.Context through all exported store/service methods
that perform I/O; use QueryContext/ExecContext/QueryRowContext throughout
- Add package-level sentinel errors (ErrNotFound, ErrAlreadyCheckedIn,
ErrNotCheckedIn) and replace nil,nil returns with explicit errors
- Update handlers to use errors.Is() instead of nil checks, with correct
HTTP status codes per error type
- Fix SQLite datetime('now') → MySQL NOW() in volunteer, schedule,
timeoff, and checkin stores
- Refactor db.Migrate to execute schema statements individually (MySQL
driver does not support multi-statement Exec)
- Fix import grouping in handler files (stdlib, external, internal)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -2,12 +2,13 @@ package timeoff
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"git.unsupervised.ca/walkies/internal/respond"
|
||||
"git.unsupervised.ca/walkies/internal/server/middleware"
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
@@ -25,7 +26,7 @@ func (h *Handler) List(w http.ResponseWriter, r *http.Request) {
|
||||
if claims.Role != "admin" {
|
||||
volunteerID = claims.VolunteerID
|
||||
}
|
||||
requests, err := h.store.List(volunteerID)
|
||||
requests, err := h.store.List(r.Context(), volunteerID)
|
||||
if err != nil {
|
||||
respond.Error(w, http.StatusInternalServerError, "could not list time off requests")
|
||||
return
|
||||
@@ -48,7 +49,7 @@ func (h *Handler) Create(w http.ResponseWriter, r *http.Request) {
|
||||
respond.Error(w, http.StatusBadRequest, "starts_at and ends_at are required")
|
||||
return
|
||||
}
|
||||
req, err := h.store.Create(claims.VolunteerID, in)
|
||||
req, err := h.store.Create(r.Context(), claims.VolunteerID, in)
|
||||
if err != nil {
|
||||
respond.Error(w, http.StatusInternalServerError, "could not create time off request")
|
||||
return
|
||||
@@ -73,14 +74,14 @@ func (h *Handler) Review(w http.ResponseWriter, r *http.Request) {
|
||||
respond.Error(w, http.StatusBadRequest, "status must be 'approved' or 'rejected'")
|
||||
return
|
||||
}
|
||||
req, err := h.store.Review(id, claims.VolunteerID, in.Status)
|
||||
req, err := h.store.Review(r.Context(), id, claims.VolunteerID, in.Status)
|
||||
if errors.Is(err, ErrNotFound) {
|
||||
respond.Error(w, http.StatusNotFound, "time off request not found")
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
respond.Error(w, http.StatusInternalServerError, "could not review time off request")
|
||||
return
|
||||
}
|
||||
if req == nil {
|
||||
respond.Error(w, http.StatusNotFound, "time off request not found")
|
||||
return
|
||||
}
|
||||
respond.JSON(w, http.StatusOK, req)
|
||||
}
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
package timeoff
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
var ErrNotFound = fmt.Errorf("time off request not found")
|
||||
|
||||
type Request struct {
|
||||
ID int64 `json:"id"`
|
||||
VolunteerID int64 `json:"volunteer_id"`
|
||||
@@ -38,8 +41,8 @@ func NewStore(db *sql.DB) *Store {
|
||||
return &Store{db: db}
|
||||
}
|
||||
|
||||
func (s *Store) Create(volunteerID int64, in CreateInput) (*Request, error) {
|
||||
res, err := s.db.Exec(
|
||||
func (s *Store) Create(ctx context.Context, volunteerID int64, in CreateInput) (*Request, error) {
|
||||
res, err := s.db.ExecContext(ctx,
|
||||
`INSERT INTO time_off_requests (volunteer_id, starts_at, ends_at, reason) VALUES (?, ?, ?, ?)`,
|
||||
volunteerID, in.StartsAt, in.EndsAt, in.Reason,
|
||||
)
|
||||
@@ -47,22 +50,22 @@ func (s *Store) Create(volunteerID int64, in CreateInput) (*Request, error) {
|
||||
return nil, fmt.Errorf("insert time off request: %w", err)
|
||||
}
|
||||
id, _ := res.LastInsertId()
|
||||
return s.GetByID(id)
|
||||
return s.GetByID(ctx, id)
|
||||
}
|
||||
|
||||
func (s *Store) GetByID(id int64) (*Request, error) {
|
||||
func (s *Store) GetByID(ctx context.Context, id int64) (*Request, error) {
|
||||
req := &Request{}
|
||||
var startsAt, endsAt, createdAt, updatedAt string
|
||||
var reason sql.NullString
|
||||
var reviewedBy sql.NullInt64
|
||||
var reviewedAt sql.NullString
|
||||
|
||||
err := s.db.QueryRow(
|
||||
err := s.db.QueryRowContext(ctx,
|
||||
`SELECT id, volunteer_id, starts_at, ends_at, reason, status, reviewed_by, reviewed_at, created_at, updated_at
|
||||
FROM time_off_requests WHERE id = ?`, id,
|
||||
).Scan(&req.ID, &req.VolunteerID, &startsAt, &endsAt, &reason, &req.Status, &reviewedBy, &reviewedAt, &createdAt, &updatedAt)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, nil
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get time off request: %w", err)
|
||||
@@ -84,7 +87,7 @@ func (s *Store) GetByID(id int64) (*Request, error) {
|
||||
return req, nil
|
||||
}
|
||||
|
||||
func (s *Store) List(volunteerID int64) ([]Request, error) {
|
||||
func (s *Store) List(ctx context.Context, volunteerID int64) ([]Request, error) {
|
||||
query := `SELECT id, volunteer_id, starts_at, ends_at, reason, status, reviewed_by, reviewed_at, created_at, updated_at FROM time_off_requests`
|
||||
args := []any{}
|
||||
if volunteerID > 0 {
|
||||
@@ -93,7 +96,7 @@ func (s *Store) List(volunteerID int64) ([]Request, error) {
|
||||
}
|
||||
query += ` ORDER BY starts_at DESC`
|
||||
|
||||
rows, err := s.db.Query(query, args...)
|
||||
rows, err := s.db.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list time off requests: %w", err)
|
||||
}
|
||||
@@ -128,13 +131,13 @@ func (s *Store) List(volunteerID int64) ([]Request, error) {
|
||||
return requests, rows.Err()
|
||||
}
|
||||
|
||||
func (s *Store) Review(id, reviewerID int64, status string) (*Request, error) {
|
||||
_, err := s.db.Exec(
|
||||
`UPDATE time_off_requests SET status=?, reviewed_by=?, reviewed_at=datetime('now'), updated_at=datetime('now') WHERE id=?`,
|
||||
func (s *Store) Review(ctx context.Context, id, reviewerID int64, status string) (*Request, error) {
|
||||
_, err := s.db.ExecContext(ctx,
|
||||
`UPDATE time_off_requests SET status=?, reviewed_by=?, reviewed_at=NOW(), updated_at=NOW() WHERE id=?`,
|
||||
status, reviewerID, id,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("review time off request: %w", err)
|
||||
}
|
||||
return s.GetByID(id)
|
||||
return s.GetByID(ctx, id)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user