Files
apocapoc-api/internal/infrastructure/http/stats_handlers.go
T
david a0141019b4 Add habit statistics feature
- Implement GetHabitStatsHandler with streak calculations
- Calculate current streak, longest streak, and completion rate
- Track completions this week and this month
- Add stats HTTP handler and route at /api/v1/stats/habits/{id}
- Add Swagger documentation for stats endpoint
2025-11-26 18:45:27 +01:00

67 lines
1.6 KiB
Go

package http
import (
"net/http"
"apocapoc-api/internal/application/queries"
"apocapoc-api/internal/shared/errors"
"github.com/go-chi/chi/v5"
)
type StatsHandlers struct {
getHabitStatsHandler *queries.GetHabitStatsHandler
}
func NewStatsHandlers(
getHabitStatsHandler *queries.GetHabitStatsHandler,
) *StatsHandlers {
return &StatsHandlers{
getHabitStatsHandler: getHabitStatsHandler,
}
}
// GetHabitStats godoc
// @Summary Get habit statistics
// @Description Get statistics for a specific habit including streaks and completion rates
// @Tags stats
// @Produce json
// @Security BearerAuth
// @Param id path string true "Habit ID"
// @Success 200 {object} queries.HabitStatsDTO
// @Failure 401 {object} ErrorResponse
// @Failure 403 {object} ErrorResponse
// @Failure 404 {object} ErrorResponse
// @Failure 500 {object} ErrorResponse
// @Router /stats/habits/{id} [get]
func (h *StatsHandlers) GetHabitStats(w http.ResponseWriter, r *http.Request) {
habitID := chi.URLParam(r, "id")
userID, ok := GetUserIDFromContext(r.Context())
if !ok {
respondError(w, http.StatusUnauthorized, "User not authenticated")
return
}
query := queries.GetHabitStatsQuery{
HabitID: habitID,
UserID: userID,
}
stats, err := h.getHabitStatsHandler.Handle(r.Context(), query)
if err != nil {
if err == errors.ErrNotFound {
respondError(w, http.StatusNotFound, "Habit not found")
return
}
if err == errors.ErrUnauthorized {
respondError(w, http.StatusForbidden, "Access denied")
return
}
respondError(w, http.StatusInternalServerError, "Failed to get habit stats")
return
}
respondJSON(w, http.StatusOK, stats)
}