Add Total Price Calculation to ctrl_items

This commit is contained in:
Katos
2024-06-22 15:34:18 +01:00
parent 2a54933cef
commit a780c6fac4

View File

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