Some checks failed
Deploy to NAS / deploy (push) Failing after 4s
Remove submodule tracking; backend is now a plain directory in the repo. Also update deploy workflow: remove --recurse-submodules. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
103 lines
3.1 KiB
Go
103 lines
3.1 KiB
Go
package api
|
|
|
|
import (
|
|
"log/slog"
|
|
"net/http"
|
|
|
|
"github.com/jacek/pamietnik/backend/internal/db"
|
|
"github.com/jacek/pamietnik/backend/internal/domain"
|
|
)
|
|
|
|
// HandleListDays handles GET /v1/days?from=YYYY-MM-DD&to=YYYY-MM-DD
|
|
func HandleListDays(store *db.TrackpointStore) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
userID := userIDFromContext(r.Context())
|
|
from := r.URL.Query().Get("from")
|
|
to := r.URL.Query().Get("to")
|
|
if from == "" || to == "" {
|
|
writeError(w, http.StatusBadRequest, "BAD_REQUEST", "from and to are required (YYYY-MM-DD)")
|
|
return
|
|
}
|
|
|
|
days, err := store.ListDays(r.Context(), userID, from, to)
|
|
if err != nil {
|
|
slog.Error("list days", "user_id", userID, "err", err)
|
|
writeError(w, http.StatusInternalServerError, "INTERNAL_ERROR", "database error")
|
|
return
|
|
}
|
|
if days == nil {
|
|
days = []domain.DaySummary{}
|
|
}
|
|
writeJSON(w, http.StatusOK, days)
|
|
}
|
|
}
|
|
|
|
// HandleListTrackpoints handles GET /v1/trackpoints?date=YYYY-MM-DD
|
|
func HandleListTrackpoints(store *db.TrackpointStore) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
userID := userIDFromContext(r.Context())
|
|
date := r.URL.Query().Get("date")
|
|
if date == "" {
|
|
writeError(w, http.StatusBadRequest, "BAD_REQUEST", "date is required (YYYY-MM-DD)")
|
|
return
|
|
}
|
|
|
|
points, err := store.ListByDate(r.Context(), userID, date)
|
|
if err != nil {
|
|
slog.Error("list trackpoints", "user_id", userID, "date", date, "err", err)
|
|
writeError(w, http.StatusInternalServerError, "INTERNAL_ERROR", "database error")
|
|
return
|
|
}
|
|
if points == nil {
|
|
points = []domain.Trackpoint{}
|
|
}
|
|
writeJSON(w, http.StatusOK, points)
|
|
}
|
|
}
|
|
|
|
// HandleListStops handles GET /v1/stops?date=YYYY-MM-DD
|
|
func HandleListStops(store *db.StopStore) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
userID := userIDFromContext(r.Context())
|
|
date := r.URL.Query().Get("date")
|
|
if date == "" {
|
|
writeError(w, http.StatusBadRequest, "BAD_REQUEST", "date is required (YYYY-MM-DD)")
|
|
return
|
|
}
|
|
|
|
stops, err := store.ListByDate(r.Context(), userID, date)
|
|
if err != nil {
|
|
slog.Error("list stops", "user_id", userID, "date", date, "err", err)
|
|
writeError(w, http.StatusInternalServerError, "INTERNAL_ERROR", "database error")
|
|
return
|
|
}
|
|
if stops == nil {
|
|
stops = []domain.Stop{}
|
|
}
|
|
writeJSON(w, http.StatusOK, stops)
|
|
}
|
|
}
|
|
|
|
// HandleListSuggestions handles GET /v1/suggestions?date=YYYY-MM-DD
|
|
func HandleListSuggestions(store *db.SuggestionStore) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
userID := userIDFromContext(r.Context())
|
|
date := r.URL.Query().Get("date")
|
|
if date == "" {
|
|
writeError(w, http.StatusBadRequest, "BAD_REQUEST", "date is required (YYYY-MM-DD)")
|
|
return
|
|
}
|
|
|
|
suggestions, err := store.ListByDate(r.Context(), userID, date)
|
|
if err != nil {
|
|
slog.Error("list suggestions", "user_id", userID, "date", date, "err", err)
|
|
writeError(w, http.StatusInternalServerError, "INTERNAL_ERROR", "database error")
|
|
return
|
|
}
|
|
if suggestions == nil {
|
|
suggestions = []domain.Suggestion{}
|
|
}
|
|
writeJSON(w, http.StatusOK, suggestions)
|
|
}
|
|
}
|