2022-12-05 21:36:32 +00:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hay-kot/homebox/backend/internal/core/services"
|
2023-03-21 04:32:10 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/repo"
|
2022-12-05 21:36:32 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/sys/validate"
|
2023-03-21 04:32:10 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/web/adapters"
|
2023-04-09 18:39:43 +00:00
|
|
|
"github.com/hay-kot/httpkit/errchain"
|
|
|
|
"github.com/hay-kot/httpkit/server"
|
2022-12-05 21:36:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// HandleGroupGet godoc
|
2023-03-07 06:18:58 +00:00
|
|
|
//
|
|
|
|
// @Summary Get Location Statistics
|
|
|
|
// @Tags Statistics
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} []repo.TotalsByOrganizer
|
|
|
|
// @Router /v1/groups/statistics/locations [GET]
|
|
|
|
// @Security Bearer
|
2023-03-21 04:32:10 +00:00
|
|
|
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)
|
2022-12-05 21:36:32 +00:00
|
|
|
}
|
2023-03-21 04:32:10 +00:00
|
|
|
|
|
|
|
return adapters.Command(fn, http.StatusOK)
|
2022-12-05 21:36:32 +00:00
|
|
|
}
|
|
|
|
|
2023-03-07 06:18:58 +00:00
|
|
|
// HandleGroupStatisticsLabels godoc
|
|
|
|
//
|
|
|
|
// @Summary Get Label Statistics
|
|
|
|
// @Tags Statistics
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} []repo.TotalsByOrganizer
|
|
|
|
// @Router /v1/groups/statistics/labels [GET]
|
|
|
|
// @Security Bearer
|
2023-03-21 04:32:10 +00:00
|
|
|
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)
|
2022-12-05 21:36:32 +00:00
|
|
|
}
|
2023-03-21 04:32:10 +00:00
|
|
|
|
|
|
|
return adapters.Command(fn, http.StatusOK)
|
2022-12-05 21:36:32 +00:00
|
|
|
}
|
|
|
|
|
2023-03-07 06:18:58 +00:00
|
|
|
// HandleGroupStatistics godoc
|
|
|
|
//
|
|
|
|
// @Summary Get Group Statistics
|
|
|
|
// @Tags Statistics
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} repo.GroupStatistics
|
|
|
|
// @Router /v1/groups/statistics [GET]
|
|
|
|
// @Security Bearer
|
2023-03-21 04:32:10 +00:00
|
|
|
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)
|
2022-12-05 21:36:32 +00:00
|
|
|
}
|
2023-03-21 04:32:10 +00:00
|
|
|
|
|
|
|
return adapters.Command(fn, http.StatusOK)
|
2022-12-05 21:36:32 +00:00
|
|
|
}
|
|
|
|
|
2023-03-07 06:18:58 +00:00
|
|
|
// 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
|
2023-03-21 04:32:10 +00:00
|
|
|
func (ctrl *V1Controller) HandleGroupStatisticsPriceOverTime() errchain.HandlerFunc {
|
2022-12-05 21:36:32 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-03-21 04:32:10 +00:00
|
|
|
return server.JSON(w, http.StatusOK, stats)
|
2022-12-05 21:36:32 +00:00
|
|
|
}
|
|
|
|
}
|