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>
65 lines
1.6 KiB
Go
65 lines
1.6 KiB
Go
package checkin
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"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}
|
|
}
|
|
|
|
// POST /api/v1/checkin
|
|
func (h *Handler) CheckIn(w http.ResponseWriter, r *http.Request) {
|
|
claims := middleware.ClaimsFromContext(r.Context())
|
|
var in CheckInInput
|
|
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
|
|
respond.Error(w, http.StatusBadRequest, "invalid request body")
|
|
return
|
|
}
|
|
ci, err := h.store.CheckIn(claims.VolunteerID, in)
|
|
if err != nil {
|
|
respond.Error(w, http.StatusConflict, err.Error())
|
|
return
|
|
}
|
|
respond.JSON(w, http.StatusCreated, ci)
|
|
}
|
|
|
|
// POST /api/v1/checkout
|
|
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 {
|
|
respond.Error(w, http.StatusConflict, err.Error())
|
|
return
|
|
}
|
|
respond.JSON(w, http.StatusOK, ci)
|
|
}
|
|
|
|
// GET /api/v1/checkin/history
|
|
func (h *Handler) History(w http.ResponseWriter, r *http.Request) {
|
|
claims := middleware.ClaimsFromContext(r.Context())
|
|
volunteerID := int64(0)
|
|
if claims.Role != "admin" {
|
|
volunteerID = claims.VolunteerID
|
|
}
|
|
history, err := h.store.History(volunteerID)
|
|
if err != nil {
|
|
respond.Error(w, http.StatusInternalServerError, "could not get history")
|
|
return
|
|
}
|
|
if history == nil {
|
|
history = []CheckIn{}
|
|
}
|
|
respond.JSON(w, http.StatusOK, history)
|
|
}
|