Echo errors, middleware and handlers used three different formats (message object, error object, bare string). One error handler now writes all of them the same way; status codes are unchanged.
189 lines
7.3 KiB
Go
189 lines
7.3 KiB
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"music-server/internal/backend"
|
|
"music-server/internal/logging"
|
|
|
|
"github.com/labstack/echo/v5"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
const (
|
|
defaultStatsLimit = 10
|
|
maxStatsLimit = 100
|
|
)
|
|
|
|
// StatisticsHandler handles statistics-related HTTP requests
|
|
type StatisticsHandler struct {
|
|
stats *backend.StatisticsService
|
|
}
|
|
|
|
// NewStatisticsHandler creates a new StatisticsHandler
|
|
func NewStatisticsHandler(stats *backend.StatisticsService) *StatisticsHandler {
|
|
return &StatisticsHandler{stats: stats}
|
|
}
|
|
|
|
// parseLimit reads the "limit" query parameter, defaulting to 10 and capping at 100.
|
|
func parseLimit(ctx *echo.Context) (int32, bool) {
|
|
limitStr := ctx.QueryParam("limit")
|
|
if limitStr == "" {
|
|
return defaultStatsLimit, true
|
|
}
|
|
limit, err := strconv.Atoi(limitStr)
|
|
if err != nil || limit <= 0 {
|
|
return 0, false
|
|
}
|
|
return int32(min(limit, maxStatsLimit)), true
|
|
}
|
|
|
|
// withLimit runs a statistics query that takes a limit and writes the result as JSON.
|
|
func withLimit[T any](ctx *echo.Context, name string, query func(*echo.Context, int32) (T, error)) error {
|
|
limit, ok := parseLimit(ctx)
|
|
if !ok {
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Invalid limit parameter")
|
|
}
|
|
result, err := query(ctx, limit)
|
|
return statsResult(ctx, name, result, err)
|
|
}
|
|
|
|
func statsResult[T any](ctx *echo.Context, name string, result T, err error) error {
|
|
if err != nil {
|
|
logging.GetLogger().Error("Failed to get statistics", zap.String("statistic", name), zap.Error(err))
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to get statistics")
|
|
}
|
|
return ctx.JSON(http.StatusOK, result)
|
|
}
|
|
|
|
// GetMostPlayedSoundtracks returns top N most played soundtracks with songs
|
|
//
|
|
// @Summary Get most played soundtracks
|
|
// @Description Returns the top N most played soundtracks with their songs
|
|
// @Tags statistics
|
|
// @Produce json
|
|
// @Param limit query int false "Number of results (default: 10, max: 100)"
|
|
// @Success 200 {array} backend.SoundtrackWithSongs
|
|
// @Failure 400 {object} map[string]string
|
|
// @Failure 500 {object} map[string]string
|
|
// @Router /api/v1/statistics/soundtracks/most-played [get]
|
|
func (h *StatisticsHandler) GetMostPlayedSoundtracks(ctx *echo.Context) error {
|
|
return withLimit(ctx, "most played soundtracks", func(c *echo.Context, limit int32) ([]backend.SoundtrackWithSongs, error) {
|
|
return h.stats.MostPlayedSoundtracks(c.Request().Context(), limit)
|
|
})
|
|
}
|
|
|
|
// GetLeastPlayedSoundtracks returns top N least played soundtracks with songs
|
|
//
|
|
// @Summary Get least played soundtracks
|
|
// @Description Returns the top N least played soundtracks with their songs
|
|
// @Tags statistics
|
|
// @Produce json
|
|
// @Param limit query int false "Number of results (default: 10, max: 100)"
|
|
// @Success 200 {array} backend.SoundtrackWithSongs
|
|
// @Failure 400 {object} map[string]string
|
|
// @Failure 500 {object} map[string]string
|
|
// @Router /api/v1/statistics/soundtracks/least-played [get]
|
|
func (h *StatisticsHandler) GetLeastPlayedSoundtracks(ctx *echo.Context) error {
|
|
return withLimit(ctx, "least played soundtracks", func(c *echo.Context, limit int32) ([]backend.SoundtrackWithSongs, error) {
|
|
return h.stats.LeastPlayedSoundtracks(c.Request().Context(), limit)
|
|
})
|
|
}
|
|
|
|
// GetMostPlayedSongs returns top N most played songs
|
|
//
|
|
// @Summary Get most played songs
|
|
// @Description Returns the top N most played songs with their soundtrack info
|
|
// @Tags statistics
|
|
// @Produce json
|
|
// @Param limit query int false "Number of results (default: 10, max: 100)"
|
|
// @Success 200 {array} backend.SongInfoForStats
|
|
// @Failure 400 {object} map[string]string
|
|
// @Failure 500 {object} map[string]string
|
|
// @Router /api/v1/statistics/songs/most-played [get]
|
|
func (h *StatisticsHandler) GetMostPlayedSongs(ctx *echo.Context) error {
|
|
return withLimit(ctx, "most played songs", func(c *echo.Context, limit int32) ([]backend.SongInfoForStats, error) {
|
|
return h.stats.MostPlayedSongs(c.Request().Context(), limit)
|
|
})
|
|
}
|
|
|
|
// GetLeastPlayedSongs returns top N least played songs
|
|
//
|
|
// @Summary Get least played songs
|
|
// @Description Returns the top N least played songs with their soundtrack info
|
|
// @Tags statistics
|
|
// @Produce json
|
|
// @Param limit query int false "Number of results (default: 10, max: 100)"
|
|
// @Success 200 {array} backend.SongInfoForStats
|
|
// @Failure 400 {object} map[string]string
|
|
// @Failure 500 {object} map[string]string
|
|
// @Router /api/v1/statistics/songs/least-played [get]
|
|
func (h *StatisticsHandler) GetLeastPlayedSongs(ctx *echo.Context) error {
|
|
return withLimit(ctx, "least played songs", func(c *echo.Context, limit int32) ([]backend.SongInfoForStats, error) {
|
|
return h.stats.LeastPlayedSongs(c.Request().Context(), limit)
|
|
})
|
|
}
|
|
|
|
// GetLastPlayedSoundtracks returns the most recently played soundtracks
|
|
//
|
|
// @Summary Get last played soundtracks
|
|
// @Description Returns the most recently played soundtracks with their songs
|
|
// @Tags statistics
|
|
// @Produce json
|
|
// @Param limit query int false "Number of results (default: 10, max: 100)"
|
|
// @Success 200 {array} backend.SoundtrackWithSongs
|
|
// @Failure 400 {object} map[string]string
|
|
// @Failure 500 {object} map[string]string
|
|
// @Router /api/v1/statistics/soundtracks/last-played [get]
|
|
func (h *StatisticsHandler) GetLastPlayedSoundtracks(ctx *echo.Context) error {
|
|
return withLimit(ctx, "last played soundtracks", func(c *echo.Context, limit int32) ([]backend.SoundtrackWithSongs, error) {
|
|
return h.stats.LastPlayedSoundtracks(c.Request().Context(), limit)
|
|
})
|
|
}
|
|
|
|
// GetOldestPlayedSoundtracks returns the least recently played soundtracks
|
|
//
|
|
// @Summary Get oldest played soundtracks
|
|
// @Description Returns the least recently played soundtracks with their songs
|
|
// @Tags statistics
|
|
// @Produce json
|
|
// @Param limit query int false "Number of results (default: 10, max: 100)"
|
|
// @Success 200 {array} backend.SoundtrackWithSongs
|
|
// @Failure 400 {object} map[string]string
|
|
// @Failure 500 {object} map[string]string
|
|
// @Router /api/v1/statistics/soundtracks/oldest-played [get]
|
|
func (h *StatisticsHandler) GetOldestPlayedSoundtracks(ctx *echo.Context) error {
|
|
return withLimit(ctx, "oldest played soundtracks", func(c *echo.Context, limit int32) ([]backend.SoundtrackWithSongs, error) {
|
|
return h.stats.OldestPlayedSoundtracks(c.Request().Context(), limit)
|
|
})
|
|
}
|
|
|
|
// GetNeverPlayedSoundtracks returns soundtracks that have never been played
|
|
//
|
|
// @Summary Get never played soundtracks
|
|
// @Description Returns all soundtracks that have never been played
|
|
// @Tags statistics
|
|
// @Produce json
|
|
// @Success 200 {array} backend.SoundtrackWithSongs
|
|
// @Failure 500 {object} map[string]string
|
|
// @Router /api/v1/statistics/soundtracks/never-played [get]
|
|
func (h *StatisticsHandler) GetNeverPlayedSoundtracks(ctx *echo.Context) error {
|
|
result, err := h.stats.NeverPlayedSoundtracks(ctx.Request().Context())
|
|
return statsResult(ctx, "never played soundtracks", result, err)
|
|
}
|
|
|
|
// GetStatisticsSummary returns overall statistics
|
|
//
|
|
// @Summary Get statistics summary
|
|
// @Description Returns overall play statistics
|
|
// @Tags statistics
|
|
// @Produce json
|
|
// @Success 200 {object} backend.StatisticsSummary
|
|
// @Failure 500 {object} map[string]string
|
|
// @Router /api/v1/statistics/summary [get]
|
|
func (h *StatisticsHandler) GetStatisticsSummary(ctx *echo.Context) error {
|
|
result, err := h.stats.Summary(ctx.Request().Context())
|
|
return statsResult(ctx, "summary", result, err)
|
|
}
|