Skip to content

A continuation of HomeBox the inventory and organization system built for the Home User

Add Total Price Calculation to ctrl_items

Katosa780c6fac40d
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]{