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 01/12] 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]{ From aaeac8ca9d2e257abb9baf33126e6e57e27b77f7 Mon Sep 17 00:00:00 2001 From: Katos <7927609+katosdev@users.noreply.github.com> Date: Sat, 22 Jun 2024 15:35:47 +0100 Subject: [PATCH 02/12] Add Total Price Calculation to locations --- .../app/api/handlers/v1/v1_ctrl_locations.go | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/backend/app/api/handlers/v1/v1_ctrl_locations.go b/backend/app/api/handlers/v1/v1_ctrl_locations.go index 2686b5c0..a3397561 100644 --- a/backend/app/api/handlers/v1/v1_ctrl_locations.go +++ b/backend/app/api/handlers/v1/v1_ctrl_locations.go @@ -2,6 +2,9 @@ package v1 import ( "net/http" + "fmt" + "context" + "math/big" "github.com/google/uuid" "github.com/hay-kot/httpkit/errchain" @@ -83,6 +86,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 { + fmt.Println(err) + } + location.TotalPrice += childLocation.TotalPrice + } + + return location, err +} + // HandleLocationGet godoc // // @Summary Get Location @@ -95,7 +124,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) From 53d542413b01151aec12729043f3868f0cb9baf0 Mon Sep 17 00:00:00 2001 From: Katos <7927609+katosdev@users.noreply.github.com> Date: Sat, 22 Jun 2024 15:37:48 +0100 Subject: [PATCH 03/12] Update swagger to include total location cost --- backend/app/api/static/docs/swagger.json | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) 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 +} From a8d21f046570eb8513ccaa537eb0b47be8137bbd Mon Sep 17 00:00:00 2001 From: Katos <7927609+katosdev@users.noreply.github.com> Date: Sat, 22 Jun 2024 15:38:27 +0100 Subject: [PATCH 04/12] Add totalprice to pagination --- backend/internal/data/repo/pagination.go | 1 + 1 file changed, 1 insertion(+) diff --git a/backend/internal/data/repo/pagination.go b/backend/internal/data/repo/pagination.go index d8878d02..5f540ee4 100644 --- a/backend/internal/data/repo/pagination.go +++ b/backend/internal/data/repo/pagination.go @@ -5,6 +5,7 @@ type PaginationResult[T any] struct { PageSize int `json:"pageSize"` Total int `json:"total"` Items []T `json:"items"` + TotalPrice float64 `json:"totalPrice"` } func calculateOffset(page, pageSize int) int { From 2292d728025201939c2e63bb293361b0bdc58cc6 Mon Sep 17 00:00:00 2001 From: Katos <7927609+katosdev@users.noreply.github.com> Date: Sat, 22 Jun 2024 15:39:00 +0100 Subject: [PATCH 05/12] Update repo_locations --- backend/internal/data/repo/repo_locations.go | 1 + 1 file changed, 1 insertion(+) 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"` } ) From 70966164145b25a199d57ccb5884d3221b11ef2a Mon Sep 17 00:00:00 2001 From: Katos <7927609+katosdev@users.noreply.github.com> Date: Sat, 22 Jun 2024 15:41:12 +0100 Subject: [PATCH 06/12] Update docs for new location pricing --- docs/docs/api/openapi-2.0.json | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/docs/docs/api/openapi-2.0.json b/docs/docs/api/openapi-2.0.json index b10c93ad..dab7ef77 100644 --- a/docs/docs/api/openapi-2.0.json +++ b/docs/docs/api/openapi-2.0.json @@ -2469,6 +2469,9 @@ "parent": { "$ref": "#/definitions/repo.LocationSummary" }, + "totalPrice": { + "type": "number" + }, "updatedAt": { "type": "string" } @@ -2707,6 +2710,9 @@ }, "total": { "type": "integer" + }, + "updatedAt": { + "type": "string" } } }, @@ -2989,4 +2995,4 @@ "in": "header" } } -} \ No newline at end of file +} From 4d916a69af7c26f8586c7ef56a25d62f7dbd4cca Mon Sep 17 00:00:00 2001 From: Katos <7927609+katosdev@users.noreply.github.com> Date: Sat, 22 Jun 2024 15:42:08 +0100 Subject: [PATCH 07/12] Update to add total price --- frontend/lib/api/types/data-contracts.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/frontend/lib/api/types/data-contracts.ts b/frontend/lib/api/types/data-contracts.ts index 384ffb61..e7fb4fe9 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; } From 7e150aa8d77d0dcd001b7a1a4762367c47acd619 Mon Sep 17 00:00:00 2001 From: Katos <7927609+katosdev@users.noreply.github.com> Date: Sat, 22 Jun 2024 15:42:41 +0100 Subject: [PATCH 08/12] Update to a --- frontend/lib/api/types/data-contracts.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/frontend/lib/api/types/data-contracts.ts b/frontend/lib/api/types/data-contracts.ts index e7fb4fe9..3b4674dd 100644 --- a/frontend/lib/api/types/data-contracts.ts +++ b/frontend/lib/api/types/data-contracts.ts @@ -330,6 +330,7 @@ export interface PaginationResultItemSummary { page: number; pageSize: number; total: number; + totalPrice: number; } export interface TotalsByOrganizer { From d7bf64742ee44e7ef1423d728355293e80fc04ba Mon Sep 17 00:00:00 2001 From: Katos <7927609+katosdev@users.noreply.github.com> Date: Sat, 22 Jun 2024 15:45:38 +0100 Subject: [PATCH 09/12] Update label to include total pricing --- frontend/pages/label/[id].vue | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) 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 @@
- +
From b57efa02fff0046aa0330007849ded9bc32c8086 Mon Sep 17 00:00:00 2001 From: Katos <7927609+katosdev@users.noreply.github.com> Date: Sat, 22 Jun 2024 15:46:39 +0100 Subject: [PATCH 10/12] Update location to include total pricing --- frontend/pages/location/[id].vue | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) 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 : "" }} + +
    +
    + +
    +

    From 8a57ca41bf6220acea6e265ce8f487cec5db74b8 Mon Sep 17 00:00:00 2001 From: Katos <7927609+katosdev@users.noreply.github.com> Date: Sat, 22 Jun 2024 17:26:39 +0100 Subject: [PATCH 11/12] Update error handling Code review suggestion Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- backend/app/api/handlers/v1/v1_ctrl_locations.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/app/api/handlers/v1/v1_ctrl_locations.go b/backend/app/api/handlers/v1/v1_ctrl_locations.go index a3397561..632e9d36 100644 --- a/backend/app/api/handlers/v1/v1_ctrl_locations.go +++ b/backend/app/api/handlers/v1/v1_ctrl_locations.go @@ -104,7 +104,7 @@ func (ctrl *V1Controller) GetLocationWithPrice(auth context.Context, GID uuid.UU for _, childLocation := range location.Children { var childLocation, err = ctrl.GetLocationWithPrice(auth, GID, childLocation.ID) if err != nil { - fmt.Println(err) + return repo.LocationOut{}, err } location.TotalPrice += childLocation.TotalPrice } From 30abdd4d367e3128b38ae2d7ea318eee4c132081 Mon Sep 17 00:00:00 2001 From: Matt Kilgore Date: Sat, 22 Jun 2024 16:15:22 -0400 Subject: [PATCH 12/12] chore(backend): removed extra unneeded property to pagination --- backend/app/api/handlers/v1/v1_ctrl_items.go | 7 +++---- backend/app/api/handlers/v1/v1_ctrl_locations.go | 5 ++--- backend/internal/data/repo/pagination.go | 1 - docs/docs/api/openapi-2.0.json | 6 ++---- 4 files changed, 7 insertions(+), 12 deletions(-) diff --git a/backend/app/api/handlers/v1/v1_ctrl_items.go b/backend/app/api/handlers/v1/v1_ctrl_items.go index ec78c7b0..857f49f6 100644 --- a/backend/app/api/handlers/v1/v1_ctrl_items.go +++ b/backend/app/api/handlers/v1/v1_ctrl_items.go @@ -4,9 +4,9 @@ import ( "database/sql" "encoding/csv" "errors" + "math/big" "net/http" "strings" - "math/big" "github.com/google/uuid" "github.com/hay-kot/httpkit/errchain" @@ -83,13 +83,12 @@ func (ctrl *V1Controller) HandleItemsGetAll() errchain.HandlerFunc { 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))) + 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]{ diff --git a/backend/app/api/handlers/v1/v1_ctrl_locations.go b/backend/app/api/handlers/v1/v1_ctrl_locations.go index 632e9d36..eff6e9c8 100644 --- a/backend/app/api/handlers/v1/v1_ctrl_locations.go +++ b/backend/app/api/handlers/v1/v1_ctrl_locations.go @@ -1,10 +1,9 @@ package v1 import ( - "net/http" - "fmt" "context" "math/big" + "net/http" "github.com/google/uuid" "github.com/hay-kot/httpkit/errchain" @@ -93,7 +92,7 @@ func (ctrl *V1Controller) GetLocationWithPrice(auth context.Context, GID uuid.UU 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))) + totalPrice.Add(totalPrice, big.NewInt(int64(item.PurchasePrice*100))) } totalPriceFloat := new(big.Float).SetInt(totalPrice) diff --git a/backend/internal/data/repo/pagination.go b/backend/internal/data/repo/pagination.go index 5f540ee4..d8878d02 100644 --- a/backend/internal/data/repo/pagination.go +++ b/backend/internal/data/repo/pagination.go @@ -5,7 +5,6 @@ type PaginationResult[T any] struct { PageSize int `json:"pageSize"` Total int `json:"total"` Items []T `json:"items"` - TotalPrice float64 `json:"totalPrice"` } func calculateOffset(page, pageSize int) int { diff --git a/docs/docs/api/openapi-2.0.json b/docs/docs/api/openapi-2.0.json index dab7ef77..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" @@ -2711,8 +2709,8 @@ "total": { "type": "integer" }, - "updatedAt": { - "type": "string" + "totalPrice": { + "type": "number" } } },