From 5ce1dff6352b84acca1ce3f296c1b36b5e836e49 Mon Sep 17 00:00:00 2001 From: Hayden <64056131+hay-kot@users.noreply.github.com> Date: Sat, 3 Dec 2022 15:03:19 -0900 Subject: [PATCH] rework statistics endpoints --- backend/app/api/handlers/v1/v1_ctrl_group.go | 20 ------ .../app/api/handlers/v1/v1_ctrl_statistics.go | 62 +++++++++++++++++++ backend/app/api/routes.go | 1 + 3 files changed, 63 insertions(+), 20 deletions(-) create mode 100644 backend/app/api/handlers/v1/v1_ctrl_statistics.go diff --git a/backend/app/api/handlers/v1/v1_ctrl_group.go b/backend/app/api/handlers/v1/v1_ctrl_group.go index b27622d..a3e8992 100644 --- a/backend/app/api/handlers/v1/v1_ctrl_group.go +++ b/backend/app/api/handlers/v1/v1_ctrl_group.go @@ -24,26 +24,6 @@ type ( } ) -// HandleGroupGet godoc -// @Summary Get the current user's group -// @Tags Group -// @Produce json -// @Success 200 {object} repo.GroupStatistics -// @Router /v1/groups/statistics [Get] -// @Security Bearer -func (ctrl *V1Controller) HandleGroupStatistics() server.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) error { - ctx := services.NewContext(r.Context()) - - stats, err := ctrl.repo.Groups.GroupStatistics(ctx, ctx.GID) - if err != nil { - return validate.NewRequestError(err, http.StatusInternalServerError) - } - - return server.Respond(w, http.StatusOK, stats) - } -} - // HandleGroupGet godoc // @Summary Get the current user's group // @Tags Group diff --git a/backend/app/api/handlers/v1/v1_ctrl_statistics.go b/backend/app/api/handlers/v1/v1_ctrl_statistics.go new file mode 100644 index 0000000..fdab4fc --- /dev/null +++ b/backend/app/api/handlers/v1/v1_ctrl_statistics.go @@ -0,0 +1,62 @@ +package v1 + +import ( + "net/http" + "time" + + "github.com/hay-kot/homebox/backend/internal/core/services" + "github.com/hay-kot/homebox/backend/internal/sys/validate" + "github.com/hay-kot/homebox/backend/pkgs/server" +) + +// HandleGroupGet godoc +// @Summary Get the current user's group statistics +// @Tags Statistics +// @Produce json +// @Success 200 {object} repo.GroupStatistics +// @Router /v1/groups/statistics [GET] +// @Security Bearer +func (ctrl *V1Controller) HandleGroupStatistics() server.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) error { + ctx := services.NewContext(r.Context()) + + stats, err := ctrl.repo.Groups.GroupStatistics(ctx, ctx.GID) + if err != nil { + return validate.NewRequestError(err, http.StatusInternalServerError) + } + + return server.Respond(w, http.StatusOK, stats) + } +} + +// HandleGroupGet godoc +// @Summary Queries the changes overtime of the purchase price over time +// @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() server.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) error { + ctx := services.NewContext(r.Context()) + + startDate, err := time.Parse("2006-01-02", r.URL.Query().Get("start")) + if err != nil { + return validate.NewRequestError(err, http.StatusBadRequest) + } + + endDate, err := time.Parse("2006-01-02", r.URL.Query().Get("end")) + if err != nil { + endDate = time.Now() + } + + stats, err := ctrl.repo.Groups.GroupStatisticsPriceOverTime(ctx, ctx.GID, startDate, endDate) + if err != nil { + return validate.NewRequestError(err, http.StatusInternalServerError) + } + + return server.Respond(w, http.StatusOK, stats) + } +} diff --git a/backend/app/api/routes.go b/backend/app/api/routes.go index e5a7948..3bf27ab 100644 --- a/backend/app/api/routes.go +++ b/backend/app/api/routes.go @@ -79,6 +79,7 @@ func (a *app) mountRoutes(repos *repo.AllRepos) { a.server.Post(v1Base("/groups/invitations"), v1Ctrl.HandleGroupInvitationsCreate(), userMW...) a.server.Get(v1Base("/groups/statistics"), v1Ctrl.HandleGroupStatistics(), userMW...) + a.server.Get(v1Base("/groups/statistics/purchase-price"), v1Ctrl.HandleGroupStatisticsPriceOverTime(), userMW...) // TODO: I don't like /groups being the URL for users a.server.Get(v1Base("/groups"), v1Ctrl.HandleGroupGet(), userMW...)