forked from mirrors/homebox
db80f8a159
* move typegen code * update taskfile to fix code-gen caches and use 'dir' attribute * enable dumping stack traces for errors * log request start and stop * set zerolog stack handler * fix routes function * refactor context adapters to use requests directly * change some method signatures to support GID * start requiring validation tags * first pass on updating handlers to use adapters * add errs package * code gen * tidy * rework API to use external server package
104 lines
3.1 KiB
Go
104 lines
3.1 KiB
Go
package v1
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/hay-kot/homebox/backend/internal/core/services"
|
|
"github.com/hay-kot/homebox/backend/internal/data/repo"
|
|
"github.com/hay-kot/homebox/backend/internal/sys/validate"
|
|
"github.com/hay-kot/homebox/backend/internal/web/adapters"
|
|
"github.com/hay-kot/safeserve/errchain"
|
|
"github.com/hay-kot/safeserve/server"
|
|
)
|
|
|
|
// HandleGroupGet godoc
|
|
//
|
|
// @Summary Get Location Statistics
|
|
// @Tags Statistics
|
|
// @Produce json
|
|
// @Success 200 {object} []repo.TotalsByOrganizer
|
|
// @Router /v1/groups/statistics/locations [GET]
|
|
// @Security Bearer
|
|
func (ctrl *V1Controller) HandleGroupStatisticsLocations() errchain.HandlerFunc {
|
|
fn := func(r *http.Request) ([]repo.TotalsByOrganizer, error) {
|
|
auth := services.NewContext(r.Context())
|
|
return ctrl.repo.Groups.StatsLocationsByPurchasePrice(auth, auth.GID)
|
|
}
|
|
|
|
return adapters.Command(fn, http.StatusOK)
|
|
}
|
|
|
|
// HandleGroupStatisticsLabels godoc
|
|
//
|
|
// @Summary Get Label Statistics
|
|
// @Tags Statistics
|
|
// @Produce json
|
|
// @Success 200 {object} []repo.TotalsByOrganizer
|
|
// @Router /v1/groups/statistics/labels [GET]
|
|
// @Security Bearer
|
|
func (ctrl *V1Controller) HandleGroupStatisticsLabels() errchain.HandlerFunc {
|
|
fn := func(r *http.Request) ([]repo.TotalsByOrganizer, error) {
|
|
auth := services.NewContext(r.Context())
|
|
return ctrl.repo.Groups.StatsLabelsByPurchasePrice(auth, auth.GID)
|
|
}
|
|
|
|
return adapters.Command(fn, http.StatusOK)
|
|
}
|
|
|
|
// HandleGroupStatistics godoc
|
|
//
|
|
// @Summary Get Group Statistics
|
|
// @Tags Statistics
|
|
// @Produce json
|
|
// @Success 200 {object} repo.GroupStatistics
|
|
// @Router /v1/groups/statistics [GET]
|
|
// @Security Bearer
|
|
func (ctrl *V1Controller) HandleGroupStatistics() errchain.HandlerFunc {
|
|
fn := func(r *http.Request) (repo.GroupStatistics, error) {
|
|
auth := services.NewContext(r.Context())
|
|
return ctrl.repo.Groups.StatsGroup(auth, auth.GID)
|
|
}
|
|
|
|
return adapters.Command(fn, http.StatusOK)
|
|
}
|
|
|
|
// HandleGroupStatisticsPriceOverTime godoc
|
|
//
|
|
// @Summary Get Purchase Price Statistics
|
|
// @Tags Statistics
|
|
// @Produce json
|
|
// @Success 200 {object} repo.ValueOverTime
|
|
// @Param start query string false "start date"
|
|
// @Param end query string false "end date"
|
|
// @Router /v1/groups/statistics/purchase-price [GET]
|
|
// @Security Bearer
|
|
func (ctrl *V1Controller) HandleGroupStatisticsPriceOverTime() errchain.HandlerFunc {
|
|
parseDate := func(datestr string, defaultDate time.Time) (time.Time, error) {
|
|
if datestr == "" {
|
|
return defaultDate, nil
|
|
}
|
|
return time.Parse("2006-01-02", datestr)
|
|
}
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) error {
|
|
ctx := services.NewContext(r.Context())
|
|
|
|
startDate, err := parseDate(r.URL.Query().Get("start"), time.Now().AddDate(0, -1, 0))
|
|
if err != nil {
|
|
return validate.NewRequestError(err, http.StatusBadRequest)
|
|
}
|
|
|
|
endDate, err := parseDate(r.URL.Query().Get("end"), time.Now())
|
|
if err != nil {
|
|
return validate.NewRequestError(err, http.StatusBadRequest)
|
|
}
|
|
|
|
stats, err := ctrl.repo.Groups.StatsPurchasePrice(ctx, ctx.GID, startDate, endDate)
|
|
if err != nil {
|
|
return validate.NewRequestError(err, http.StatusInternalServerError)
|
|
}
|
|
|
|
return server.JSON(w, http.StatusOK, stats)
|
|
}
|
|
}
|