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:
2026-03-05 16:20:23 -04:00
parent 55f68c571e
commit 87caf478df
14 changed files with 180 additions and 145 deletions

View File

@@ -1,19 +1,26 @@
package checkin
import (
"context"
"database/sql"
"errors"
"fmt"
"time"
)
var (
ErrNotFound = fmt.Errorf("check-in not found")
ErrAlreadyCheckedIn = fmt.Errorf("already checked in")
ErrNotCheckedIn = fmt.Errorf("not checked in")
)
type CheckIn struct {
ID int64 `json:"id"`
VolunteerID int64 `json:"volunteer_id"`
ScheduleID *int64 `json:"schedule_id,omitempty"`
CheckedInAt time.Time `json:"checked_in_at"`
CheckedOutAt *time.Time `json:"checked_out_at,omitempty"`
Notes string `json:"notes,omitempty"`
ID int64 `json:"id"`
VolunteerID int64 `json:"volunteer_id"`
ScheduleID *int64 `json:"schedule_id,omitempty"`
CheckedInAt time.Time `json:"checked_in_at"`
CheckedOutAt *time.Time `json:"checked_out_at,omitempty"`
Notes string `json:"notes,omitempty"`
}
type CheckInInput struct {
@@ -33,17 +40,17 @@ func NewStore(db *sql.DB) *Store {
return &Store{db: db}
}
func (s *Store) CheckIn(volunteerID int64, in CheckInInput) (*CheckIn, error) {
func (s *Store) CheckIn(ctx context.Context, volunteerID int64, in CheckInInput) (*CheckIn, error) {
// Ensure no active check-in exists
var count int
s.db.QueryRow(
s.db.QueryRowContext(ctx,
`SELECT COUNT(*) FROM checkins WHERE volunteer_id = ? AND checked_out_at IS NULL`, volunteerID,
).Scan(&count)
if count > 0 {
return nil, fmt.Errorf("already checked in")
return nil, ErrAlreadyCheckedIn
}
res, err := s.db.Exec(
res, err := s.db.ExecContext(ctx,
`INSERT INTO checkins (volunteer_id, schedule_id, notes) VALUES (?, ?, ?)`,
volunteerID, in.ScheduleID, in.Notes,
)
@@ -51,43 +58,43 @@ func (s *Store) CheckIn(volunteerID int64, in CheckInInput) (*CheckIn, error) {
return nil, fmt.Errorf("insert checkin: %w", err)
}
id, _ := res.LastInsertId()
return s.GetByID(id)
return s.GetByID(ctx, id)
}
func (s *Store) CheckOut(volunteerID int64, in CheckOutInput) (*CheckIn, error) {
func (s *Store) CheckOut(ctx context.Context, volunteerID int64, in CheckOutInput) (*CheckIn, error) {
var id int64
err := s.db.QueryRow(
err := s.db.QueryRowContext(ctx,
`SELECT id FROM checkins WHERE volunteer_id = ? AND checked_out_at IS NULL ORDER BY checked_in_at DESC LIMIT 1`,
volunteerID,
).Scan(&id)
if errors.Is(err, sql.ErrNoRows) {
return nil, fmt.Errorf("not checked in")
return nil, ErrNotCheckedIn
}
if err != nil {
return nil, fmt.Errorf("find active checkin: %w", err)
}
_, err = s.db.Exec(
`UPDATE checkins SET checked_out_at=datetime('now'), notes=COALESCE(NULLIF(?, ''), notes) WHERE id=?`,
_, err = s.db.ExecContext(ctx,
`UPDATE checkins SET checked_out_at=NOW(), notes=COALESCE(NULLIF(?, ''), notes) WHERE id=?`,
in.Notes, id,
)
if err != nil {
return nil, fmt.Errorf("checkout: %w", err)
}
return s.GetByID(id)
return s.GetByID(ctx, id)
}
func (s *Store) GetByID(id int64) (*CheckIn, error) {
func (s *Store) GetByID(ctx context.Context, id int64) (*CheckIn, error) {
ci := &CheckIn{}
var checkedInAt string
var checkedOutAt sql.NullString
var scheduleID sql.NullInt64
var notes sql.NullString
err := s.db.QueryRow(
err := s.db.QueryRowContext(ctx,
`SELECT id, volunteer_id, schedule_id, checked_in_at, checked_out_at, notes FROM checkins WHERE id = ?`, id,
).Scan(&ci.ID, &ci.VolunteerID, &scheduleID, &checkedInAt, &checkedOutAt, &notes)
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
return nil, ErrNotFound
}
if err != nil {
return nil, fmt.Errorf("get checkin: %w", err)
@@ -106,7 +113,7 @@ func (s *Store) GetByID(id int64) (*CheckIn, error) {
return ci, nil
}
func (s *Store) History(volunteerID int64) ([]CheckIn, error) {
func (s *Store) History(ctx context.Context, volunteerID int64) ([]CheckIn, error) {
query := `SELECT id, volunteer_id, schedule_id, checked_in_at, checked_out_at, notes FROM checkins`
args := []any{}
if volunteerID > 0 {
@@ -115,7 +122,7 @@ func (s *Store) History(volunteerID int64) ([]CheckIn, error) {
}
query += ` ORDER BY checked_in_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 checkins: %w", err)
}

View File

@@ -2,6 +2,7 @@ package checkin
import (
"encoding/json"
"errors"
"net/http"
"git.unsupervised.ca/walkies/internal/respond"
@@ -24,11 +25,15 @@ func (h *Handler) CheckIn(w http.ResponseWriter, r *http.Request) {
respond.Error(w, http.StatusBadRequest, "invalid request body")
return
}
ci, err := h.store.CheckIn(claims.VolunteerID, in)
if err != nil {
ci, err := h.store.CheckIn(r.Context(), claims.VolunteerID, in)
if errors.Is(err, ErrAlreadyCheckedIn) {
respond.Error(w, http.StatusConflict, err.Error())
return
}
if err != nil {
respond.Error(w, http.StatusInternalServerError, "could not check in")
return
}
respond.JSON(w, http.StatusCreated, ci)
}
@@ -37,11 +42,15 @@ func (h *Handler) CheckOut(w http.ResponseWriter, r *http.Request) {
claims := middleware.ClaimsFromContext(r.Context())
var in CheckOutInput
json.NewDecoder(r.Body).Decode(&in)
ci, err := h.store.CheckOut(claims.VolunteerID, in)
if err != nil {
ci, err := h.store.CheckOut(r.Context(), claims.VolunteerID, in)
if errors.Is(err, ErrNotCheckedIn) {
respond.Error(w, http.StatusConflict, err.Error())
return
}
if err != nil {
respond.Error(w, http.StatusInternalServerError, "could not check out")
return
}
respond.JSON(w, http.StatusOK, ci)
}
@@ -52,7 +61,7 @@ func (h *Handler) History(w http.ResponseWriter, r *http.Request) {
if claims.Role != "admin" {
volunteerID = claims.VolunteerID
}
history, err := h.store.History(volunteerID)
history, err := h.store.History(r.Context(), volunteerID)
if err != nil {
respond.Error(w, http.StatusInternalServerError, "could not get history")
return