From a780c6fac40dc8d03d9d18051f95151b848d4b56 Mon Sep 17 00:00:00 2001 From: Katos <7927609+katosdev@users.noreply.github.com> Date: Sat, 22 Jun 2024 15:34:18 +0100 Subject: [PATCH] Add Total Price Calculation to ctrl_items --- backend/app/api/handlers/v1/v1_ctrl_items.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/backend/app/api/handlers/v1/v1_ctrl_items.go b/backend/app/api/handlers/v1/v1_ctrl_items.go index 272011f8..ec78c7b0 100644 --- a/backend/app/api/handlers/v1/v1_ctrl_items.go +++ b/backend/app/api/handlers/v1/v1_ctrl_items.go @@ -6,6 +6,7 @@ import ( "errors" "net/http" "strings" + "math/big" "github.com/google/uuid" "github.com/hay-kot/httpkit/errchain" @@ -80,6 +81,15 @@ func (ctrl *V1Controller) HandleItemsGetAll() errchain.HandlerFunc { ctx := services.NewContext(r.Context()) items, err := ctrl.repo.Items.QueryByGroup(ctx, ctx.GID, extractQuery(r)) + totalPrice := new(big.Int) + for _, item := range items.Items { + totalPrice.Add(totalPrice, big.NewInt(int64(item.PurchasePrice * 100))) + } + + totalPriceFloat := new(big.Float).SetInt(totalPrice) + totalPriceFloat.Quo(totalPriceFloat, big.NewFloat(100)) + items.TotalPrice, _ = totalPriceFloat.Float64() + if err != nil { if errors.Is(err, sql.ErrNoRows) { return server.JSON(w, http.StatusOK, repo.PaginationResult[repo.ItemSummary]{