Files
walkies/internal/timeoff/handler.go
James Griffin 4989ff1061 Scaffold full-stack volunteer scheduling application
Go backend with domain-based packages (volunteer, schedule, timeoff,
checkin, notification), SQLite storage, JWT auth, and chi router.
React TypeScript frontend with routing, auth context, and pages for
all core features. Multi-stage Dockerfile and docker-compose included.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-05 11:25:02 -04:00

87 lines
2.3 KiB
Go

package timeoff
import (
"encoding/json"
"net/http"
"strconv"
"github.com/go-chi/chi/v5"
"git.unsupervised.ca/walkies/internal/respond"
"git.unsupervised.ca/walkies/internal/server/middleware"
)
type Handler struct {
store *Store
}
func NewHandler(store *Store) *Handler {
return &Handler{store: store}
}
// GET /api/v1/timeoff
func (h *Handler) List(w http.ResponseWriter, r *http.Request) {
claims := middleware.ClaimsFromContext(r.Context())
volunteerID := int64(0)
if claims.Role != "admin" {
volunteerID = claims.VolunteerID
}
requests, err := h.store.List(volunteerID)
if err != nil {
respond.Error(w, http.StatusInternalServerError, "could not list time off requests")
return
}
if requests == nil {
requests = []Request{}
}
respond.JSON(w, http.StatusOK, requests)
}
// POST /api/v1/timeoff
func (h *Handler) Create(w http.ResponseWriter, r *http.Request) {
claims := middleware.ClaimsFromContext(r.Context())
var in CreateInput
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
respond.Error(w, http.StatusBadRequest, "invalid request body")
return
}
if in.StartsAt == "" || in.EndsAt == "" {
respond.Error(w, http.StatusBadRequest, "starts_at and ends_at are required")
return
}
req, err := h.store.Create(claims.VolunteerID, in)
if err != nil {
respond.Error(w, http.StatusInternalServerError, "could not create time off request")
return
}
respond.JSON(w, http.StatusCreated, req)
}
// PUT /api/v1/timeoff/{id}/review
func (h *Handler) Review(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
}
var in ReviewInput
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
respond.Error(w, http.StatusBadRequest, "invalid request body")
return
}
if in.Status != "approved" && in.Status != "rejected" {
respond.Error(w, http.StatusBadRequest, "status must be 'approved' or 'rejected'")
return
}
req, err := h.store.Review(id, claims.VolunteerID, in.Status)
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)
}