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:
@@ -1,12 +1,13 @@
|
||||
package notification
|
||||
|
||||
import (
|
||||
"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 {
|
||||
@@ -20,7 +21,7 @@ func NewHandler(store *Store) *Handler {
|
||||
// GET /api/v1/notifications
|
||||
func (h *Handler) List(w http.ResponseWriter, r *http.Request) {
|
||||
claims := middleware.ClaimsFromContext(r.Context())
|
||||
notifications, err := h.store.ListForVolunteer(claims.VolunteerID)
|
||||
notifications, err := h.store.ListForVolunteer(r.Context(), claims.VolunteerID)
|
||||
if err != nil {
|
||||
respond.Error(w, http.StatusInternalServerError, "could not list notifications")
|
||||
return
|
||||
@@ -39,14 +40,14 @@ func (h *Handler) MarkRead(w http.ResponseWriter, r *http.Request) {
|
||||
respond.Error(w, http.StatusBadRequest, "invalid id")
|
||||
return
|
||||
}
|
||||
n, err := h.store.MarkRead(id, claims.VolunteerID)
|
||||
n, err := h.store.MarkRead(r.Context(), id, claims.VolunteerID)
|
||||
if errors.Is(err, ErrNotFound) {
|
||||
respond.Error(w, http.StatusNotFound, "notification not found")
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
respond.Error(w, http.StatusInternalServerError, "could not mark notification as read")
|
||||
return
|
||||
}
|
||||
if n == nil {
|
||||
respond.Error(w, http.StatusNotFound, "notification not found")
|
||||
return
|
||||
}
|
||||
respond.JSON(w, http.StatusOK, n)
|
||||
}
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
package notification
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
var ErrNotFound = fmt.Errorf("notification not found")
|
||||
|
||||
type Notification struct {
|
||||
ID int64 `json:"id"`
|
||||
VolunteerID int64 `json:"volunteer_id"`
|
||||
@@ -23,8 +26,8 @@ func NewStore(db *sql.DB) *Store {
|
||||
return &Store{db: db}
|
||||
}
|
||||
|
||||
func (s *Store) Create(volunteerID int64, message string) (*Notification, error) {
|
||||
res, err := s.db.Exec(
|
||||
func (s *Store) Create(ctx context.Context, volunteerID int64, message string) (*Notification, error) {
|
||||
res, err := s.db.ExecContext(ctx,
|
||||
`INSERT INTO notifications (volunteer_id, message) VALUES (?, ?)`,
|
||||
volunteerID, message,
|
||||
)
|
||||
@@ -32,17 +35,17 @@ func (s *Store) Create(volunteerID int64, message string) (*Notification, error)
|
||||
return nil, fmt.Errorf("insert notification: %w", err)
|
||||
}
|
||||
id, _ := res.LastInsertId()
|
||||
return s.GetByID(id)
|
||||
return s.GetByID(ctx, id)
|
||||
}
|
||||
|
||||
func (s *Store) GetByID(id int64) (*Notification, error) {
|
||||
func (s *Store) GetByID(ctx context.Context, id int64) (*Notification, error) {
|
||||
n := &Notification{}
|
||||
var createdAt string
|
||||
err := s.db.QueryRow(
|
||||
err := s.db.QueryRowContext(ctx,
|
||||
`SELECT id, volunteer_id, message, read, created_at FROM notifications WHERE id = ?`, id,
|
||||
).Scan(&n.ID, &n.VolunteerID, &n.Message, &n.Read, &createdAt)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, nil
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get notification: %w", err)
|
||||
@@ -51,8 +54,8 @@ func (s *Store) GetByID(id int64) (*Notification, error) {
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (s *Store) ListForVolunteer(volunteerID int64) ([]Notification, error) {
|
||||
rows, err := s.db.Query(
|
||||
func (s *Store) ListForVolunteer(ctx context.Context, volunteerID int64) ([]Notification, error) {
|
||||
rows, err := s.db.QueryContext(ctx,
|
||||
`SELECT id, volunteer_id, message, read, created_at FROM notifications WHERE volunteer_id = ? ORDER BY created_at DESC`,
|
||||
volunteerID,
|
||||
)
|
||||
@@ -74,17 +77,17 @@ func (s *Store) ListForVolunteer(volunteerID int64) ([]Notification, error) {
|
||||
return notifications, rows.Err()
|
||||
}
|
||||
|
||||
func (s *Store) MarkRead(id, volunteerID int64) (*Notification, error) {
|
||||
result, err := s.db.Exec(
|
||||
func (s *Store) MarkRead(ctx context.Context, id, volunteerID int64) (*Notification, error) {
|
||||
result, err := s.db.ExecContext(ctx,
|
||||
`UPDATE notifications SET read = 1 WHERE id = ? AND volunteer_id = ?`,
|
||||
id, volunteerID,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("mark read: %w", err)
|
||||
}
|
||||
rows, _ := result.RowsAffected()
|
||||
if rows == 0 {
|
||||
return nil, nil
|
||||
affected, _ := result.RowsAffected()
|
||||
if affected == 0 {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return s.GetByID(id)
|
||||
return s.GetByID(ctx, id)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user