- 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>
54 lines
1.4 KiB
Go
54 lines
1.4 KiB
Go
package notification
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"git.unsupervised.ca/walkies/internal/respond"
|
|
"git.unsupervised.ca/walkies/internal/server/middleware"
|
|
"github.com/go-chi/chi/v5"
|
|
)
|
|
|
|
type Handler struct {
|
|
store *Store
|
|
}
|
|
|
|
func NewHandler(store *Store) *Handler {
|
|
return &Handler{store: store}
|
|
}
|
|
|
|
// GET /api/v1/notifications
|
|
func (h *Handler) List(w http.ResponseWriter, r *http.Request) {
|
|
claims := middleware.ClaimsFromContext(r.Context())
|
|
notifications, err := h.store.ListForVolunteer(r.Context(), claims.VolunteerID)
|
|
if err != nil {
|
|
respond.Error(w, http.StatusInternalServerError, "could not list notifications")
|
|
return
|
|
}
|
|
if notifications == nil {
|
|
notifications = []Notification{}
|
|
}
|
|
respond.JSON(w, http.StatusOK, notifications)
|
|
}
|
|
|
|
// PUT /api/v1/notifications/{id}/read
|
|
func (h *Handler) MarkRead(w http.ResponseWriter, r *http.Request) {
|
|
claims := middleware.ClaimsFromContext(r.Context())
|
|
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
|
if err != nil {
|
|
respond.Error(w, http.StatusBadRequest, "invalid id")
|
|
return
|
|
}
|
|
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
|
|
}
|
|
respond.JSON(w, http.StatusOK, n)
|
|
}
|