diff --git a/backend/app/api/handlers/v1/v1_ctrl_items.go b/backend/app/api/handlers/v1/v1_ctrl_items.go index 2619f4d1..f53377db 100644 --- a/backend/app/api/handlers/v1/v1_ctrl_items.go +++ b/backend/app/api/handlers/v1/v1_ctrl_items.go @@ -4,6 +4,7 @@ import ( "database/sql" "encoding/csv" "errors" + "math/big" "net/http" "strings" @@ -80,6 +81,14 @@ 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)) + if err != nil { if errors.Is(err, sql.ErrNoRows) { return server.JSON(w, http.StatusOK, repo.PaginationResult[repo.ItemSummary]{ diff --git a/backend/app/api/handlers/v1/v1_ctrl_locations.go b/backend/app/api/handlers/v1/v1_ctrl_locations.go index 2686b5c0..eff6e9c8 100644 --- a/backend/app/api/handlers/v1/v1_ctrl_locations.go +++ b/backend/app/api/handlers/v1/v1_ctrl_locations.go @@ -1,6 +1,8 @@ package v1 import ( + "context" + "math/big" "net/http" "github.com/google/uuid" @@ -83,6 +85,32 @@ func (ctrl *V1Controller) HandleLocationDelete() errchain.HandlerFunc { return adapters.CommandID("id", fn, http.StatusNoContent) } +func (ctrl *V1Controller) GetLocationWithPrice(auth context.Context, GID uuid.UUID, ID uuid.UUID) (repo.LocationOut, error) { + var location, err = ctrl.repo.Locations.GetOneByGroup(auth, GID, ID) + + // Add direct child items price + totalPrice := new(big.Int) + items, err := ctrl.repo.Items.QueryByGroup(auth, GID, repo.ItemQuery{LocationIDs: []uuid.UUID{ID}}) + 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)) + location.TotalPrice, _ = totalPriceFloat.Float64() + + // Add price from child locations + for _, childLocation := range location.Children { + var childLocation, err = ctrl.GetLocationWithPrice(auth, GID, childLocation.ID) + if err != nil { + return repo.LocationOut{}, err + } + location.TotalPrice += childLocation.TotalPrice + } + + return location, err +} + // HandleLocationGet godoc // // @Summary Get Location @@ -95,7 +123,9 @@ func (ctrl *V1Controller) HandleLocationDelete() errchain.HandlerFunc { func (ctrl *V1Controller) HandleLocationGet() errchain.HandlerFunc { fn := func(r *http.Request, ID uuid.UUID) (repo.LocationOut, error) { auth := services.NewContext(r.Context()) - return ctrl.repo.Locations.GetOneByGroup(auth, auth.GID, ID) + var location, err = ctrl.GetLocationWithPrice(auth, auth.GID, ID) + + return location, err } return adapters.CommandID("id", fn, http.StatusOK) diff --git a/backend/app/api/static/docs/swagger.json b/backend/app/api/static/docs/swagger.json index b10c93ad..2b695bc5 100644 --- a/backend/app/api/static/docs/swagger.json +++ b/backend/app/api/static/docs/swagger.json @@ -2469,6 +2469,9 @@ "parent": { "$ref": "#/definitions/repo.LocationSummary" }, + "totalPrice": { + "type": "number" + }, "updatedAt": { "type": "string" } @@ -2707,6 +2710,9 @@ }, "total": { "type": "integer" + }, + "totalPrice": { + "type": "number" } } }, @@ -2989,4 +2995,4 @@ "in": "header" } } -} \ No newline at end of file +} diff --git a/backend/internal/data/repo/repo_locations.go b/backend/internal/data/repo/repo_locations.go index 4998b5ba..f5796d50 100644 --- a/backend/internal/data/repo/repo_locations.go +++ b/backend/internal/data/repo/repo_locations.go @@ -49,6 +49,7 @@ type ( Parent *LocationSummary `json:"parent,omitempty"` LocationSummary Children []LocationSummary `json:"children"` + TotalPrice float64 `json:"totalPrice"` } ) diff --git a/docs/docs/api/openapi-2.0.json b/docs/docs/api/openapi-2.0.json index b10c93ad..d517beac 100644 --- a/docs/docs/api/openapi-2.0.json +++ b/docs/docs/api/openapi-2.0.json @@ -1683,14 +1683,12 @@ "parameters": [ { "type": "string", - "example": "admin@admin.com", "description": "string", "name": "username", "in": "formData" }, { "type": "string", - "example": "admin", "description": "string", "name": "password", "in": "formData" @@ -2469,6 +2467,9 @@ "parent": { "$ref": "#/definitions/repo.LocationSummary" }, + "totalPrice": { + "type": "number" + }, "updatedAt": { "type": "string" } @@ -2707,6 +2708,9 @@ }, "total": { "type": "integer" + }, + "totalPrice": { + "type": "number" } } }, @@ -2989,4 +2993,4 @@ "in": "header" } } -} \ No newline at end of file +} diff --git a/frontend/lib/api/types/data-contracts.ts b/frontend/lib/api/types/data-contracts.ts index 384ffb61..3b4674dd 100644 --- a/frontend/lib/api/types/data-contracts.ts +++ b/frontend/lib/api/types/data-contracts.ts @@ -232,6 +232,7 @@ export interface LocationOut { id: string; name: string; parent: LocationSummary; + totalPrice: number; updatedAt: Date | string; } @@ -329,6 +330,7 @@ export interface PaginationResultItemSummary { page: number; pageSize: number; total: number; + totalPrice: number; } export interface TotalsByOrganizer { diff --git a/frontend/pages/label/[id].vue b/frontend/pages/label/[id].vue index 5824888b..ce8c2d9f 100644 --- a/frontend/pages/label/[id].vue +++ b/frontend/pages/label/[id].vue @@ -88,7 +88,7 @@ return []; } - return resp.data.items; + return resp.data; }); @@ -115,8 +115,17 @@
-

+

{{ label ? label.name : "" }} + +
+
+ +
+

@@ -144,7 +153,7 @@
- +
diff --git a/frontend/pages/location/[id].vue b/frontend/pages/location/[id].vue index 5f7f1a13..47338807 100644 --- a/frontend/pages/location/[id].vue +++ b/frontend/pages/location/[id].vue @@ -138,8 +138,17 @@
  • {{ location.name }}
  • -

    +

    {{ location ? location.name : "" }} + +
    +
    + +
    +