From 8557b68d9d88a7279a110b0455f08088d67333e9 Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Mon, 15 Jul 2024 22:33:32 +0530 Subject: [PATCH 1/2] fix: add deleted flag to Product aggregate --- ...3e530373595ce83cd36612ee48d3c36c81f3.json} | 10 ++++++-- ...e4f0682f740778e483a2db91d6c73ac6bc6d.json} | 7 +++--- ...86bbc866218e4146b6d608255c1b929073b4.json} | 7 +++--- .../category_name_exists_for_store.rs | 2 -- .../output/db/postgres/product_id_exists.rs | 6 +++-- .../output/db/postgres/product_view.rs | 23 ++++++++++++++----- src/inventory/domain/product_aggregate.rs | 3 +++ 7 files changed, 40 insertions(+), 18 deletions(-) rename .sqlx/{query-398a5dc0ffc9f892255ebfd67e1583f5cf092d871a9a710ca4f59e14d08aaf8e.json => query-3f83167782a2de1be7d87d35f63a3e530373595ce83cd36612ee48d3c36c81f3.json} (82%) rename .sqlx/{query-727e8a82469f1887a7136900d45cfb4e3b6c0ddeb0c3610ec5a9f80e0b1c227e.json => query-9390910c71001ef77313e594dea0e4f0682f740778e483a2db91d6c73ac6bc6d.json} (69%) rename .sqlx/{query-60fb3bb1d459b64f900cf02d7d2444b2183bd4d1a4af3a93336091f3c2bdcace.json => query-f55fe530202bb369fdea3baaf91f86bbc866218e4146b6d608255c1b929073b4.json} (83%) diff --git a/.sqlx/query-398a5dc0ffc9f892255ebfd67e1583f5cf092d871a9a710ca4f59e14d08aaf8e.json b/.sqlx/query-3f83167782a2de1be7d87d35f63a3e530373595ce83cd36612ee48d3c36c81f3.json similarity index 82% rename from .sqlx/query-398a5dc0ffc9f892255ebfd67e1583f5cf092d871a9a710ca4f59e14d08aaf8e.json rename to .sqlx/query-3f83167782a2de1be7d87d35f63a3e530373595ce83cd36612ee48d3c36c81f3.json index 66962fb..bfc6121 100644 --- a/.sqlx/query-398a5dc0ffc9f892255ebfd67e1583f5cf092d871a9a710ca4f59e14d08aaf8e.json +++ b/.sqlx/query-3f83167782a2de1be7d87d35f63a3e530373595ce83cd36612ee48d3c36c81f3.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "SELECT \n name,\n description,\n image,\n product_id,\n category_id,\n price_major,\n price_minor,\n price_currency,\n sku_able,\n quantity_unit,\n quantity_number\n FROM\n cqrs_inventory_product_query\n WHERE\n product_id = $1;", + "query": "SELECT \n name,\n description,\n image,\n product_id,\n category_id,\n price_major,\n price_minor,\n price_currency,\n sku_able,\n quantity_unit,\n quantity_number,\n deleted\n FROM\n cqrs_inventory_product_query\n WHERE\n product_id = $1;", "describe": { "columns": [ { @@ -57,6 +57,11 @@ "ordinal": 10, "name": "quantity_number", "type_info": "Int4" + }, + { + "ordinal": 11, + "name": "deleted", + "type_info": "Bool" } ], "parameters": { @@ -75,8 +80,9 @@ false, false, false, + false, false ] }, - "hash": "398a5dc0ffc9f892255ebfd67e1583f5cf092d871a9a710ca4f59e14d08aaf8e" + "hash": "3f83167782a2de1be7d87d35f63a3e530373595ce83cd36612ee48d3c36c81f3" } diff --git a/.sqlx/query-727e8a82469f1887a7136900d45cfb4e3b6c0ddeb0c3610ec5a9f80e0b1c227e.json b/.sqlx/query-9390910c71001ef77313e594dea0e4f0682f740778e483a2db91d6c73ac6bc6d.json similarity index 69% rename from .sqlx/query-727e8a82469f1887a7136900d45cfb4e3b6c0ddeb0c3610ec5a9f80e0b1c227e.json rename to .sqlx/query-9390910c71001ef77313e594dea0e4f0682f740778e483a2db91d6c73ac6bc6d.json index b26993b..d70d210 100644 --- a/.sqlx/query-727e8a82469f1887a7136900d45cfb4e3b6c0ddeb0c3610ec5a9f80e0b1c227e.json +++ b/.sqlx/query-9390910c71001ef77313e594dea0e4f0682f740778e483a2db91d6c73ac6bc6d.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "INSERT INTO cqrs_inventory_product_query (\n version,\n name,\n description,\n image,\n product_id,\n category_id,\n price_major,\n price_minor,\n price_currency,\n sku_able,\n quantity_unit,\n quantity_number\n ) VALUES (\n $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12\n );", + "query": "INSERT INTO cqrs_inventory_product_query (\n version,\n name,\n description,\n image,\n product_id,\n category_id,\n price_major,\n price_minor,\n price_currency,\n sku_able,\n quantity_unit,\n quantity_number,\n deleted\n ) VALUES (\n $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13\n );", "describe": { "columns": [], "parameters": { @@ -16,10 +16,11 @@ "Text", "Bool", "Text", - "Int4" + "Int4", + "Bool" ] }, "nullable": [] }, - "hash": "727e8a82469f1887a7136900d45cfb4e3b6c0ddeb0c3610ec5a9f80e0b1c227e" + "hash": "9390910c71001ef77313e594dea0e4f0682f740778e483a2db91d6c73ac6bc6d" } diff --git a/.sqlx/query-60fb3bb1d459b64f900cf02d7d2444b2183bd4d1a4af3a93336091f3c2bdcace.json b/.sqlx/query-f55fe530202bb369fdea3baaf91f86bbc866218e4146b6d608255c1b929073b4.json similarity index 83% rename from .sqlx/query-60fb3bb1d459b64f900cf02d7d2444b2183bd4d1a4af3a93336091f3c2bdcace.json rename to .sqlx/query-f55fe530202bb369fdea3baaf91f86bbc866218e4146b6d608255c1b929073b4.json index 1a5cf00..b95a37b 100644 --- a/.sqlx/query-60fb3bb1d459b64f900cf02d7d2444b2183bd4d1a4af3a93336091f3c2bdcace.json +++ b/.sqlx/query-f55fe530202bb369fdea3baaf91f86bbc866218e4146b6d608255c1b929073b4.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "UPDATE\n cqrs_inventory_product_query\n SET\n version = $1,\n name = $2,\n description = $3,\n image = $4,\n product_id = $5,\n category_id = $6,\n price_major = $7,\n price_minor = $8,\n price_currency = $9,\n sku_able = $10,\n quantity_unit = $11,\n quantity_number = $12;", + "query": "UPDATE\n cqrs_inventory_product_query\n SET\n version = $1,\n name = $2,\n description = $3,\n image = $4,\n product_id = $5,\n category_id = $6,\n price_major = $7,\n price_minor = $8,\n price_currency = $9,\n sku_able = $10,\n quantity_unit = $11,\n quantity_number = $12,\n deleted = $13;", "describe": { "columns": [], "parameters": { @@ -16,10 +16,11 @@ "Text", "Bool", "Text", - "Int4" + "Int4", + "Bool" ] }, "nullable": [] }, - "hash": "60fb3bb1d459b64f900cf02d7d2444b2183bd4d1a4af3a93336091f3c2bdcace" + "hash": "f55fe530202bb369fdea3baaf91f86bbc866218e4146b6d608255c1b929073b4" } diff --git a/src/inventory/adapters/output/db/postgres/category_name_exists_for_store.rs b/src/inventory/adapters/output/db/postgres/category_name_exists_for_store.rs index 194d395..cc30580 100644 --- a/src/inventory/adapters/output/db/postgres/category_name_exists_for_store.rs +++ b/src/inventory/adapters/output/db/postgres/category_name_exists_for_store.rs @@ -87,7 +87,6 @@ pub mod tests { // state exists assert!(db.category_name_exists_for_store(&category).await.unwrap()); - // Set category.deleted = true; now db.category_name_exists_for_store must return false sqlx::query!( "UPDATE cqrs_inventory_category_query SET deleted = true WHERE category_id = $1;", @@ -98,7 +97,6 @@ pub mod tests { .unwrap(); assert!(!db.category_name_exists_for_store(&category).await.unwrap()); - settings.drop_db().await; } } diff --git a/src/inventory/adapters/output/db/postgres/product_id_exists.rs b/src/inventory/adapters/output/db/postgres/product_id_exists.rs index b799bff..34d714f 100644 --- a/src/inventory/adapters/output/db/postgres/product_id_exists.rs +++ b/src/inventory/adapters/output/db/postgres/product_id_exists.rs @@ -84,9 +84,10 @@ pub mod tests { price_currency, sku_able, quantity_unit, - quantity_number + quantity_number, + deleted ) VALUES ( - $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12 + $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13 );", 1, p.name(), @@ -100,6 +101,7 @@ pub mod tests { p.sku_able().clone(), p.quantity().unit().to_string(), p.quantity().number().clone() as i32, + p.deleted().clone() ) .execute(&db.pool) .await diff --git a/src/inventory/adapters/output/db/postgres/product_view.rs b/src/inventory/adapters/output/db/postgres/product_view.rs index e4c0579..68562ab 100644 --- a/src/inventory/adapters/output/db/postgres/product_view.rs +++ b/src/inventory/adapters/output/db/postgres/product_view.rs @@ -38,6 +38,8 @@ pub struct ProductView { quantity_number: i32, category_id: Uuid, + + deleted: bool, } impl From for Product { @@ -64,6 +66,7 @@ impl From for Product { .category_id(v.category_id) .quantity(quantity) .product_id(v.product_id) + .deleted(v.deleted) .build() .unwrap() } @@ -90,6 +93,8 @@ impl View for ProductView { self.quantity_number = val.quantity().number().clone() as i32; self.quantity_unit = val.quantity().unit().to_string(); + + self.deleted = false; } _ => (), } @@ -117,7 +122,8 @@ impl ViewRepository for InventoryDBPostgresAdapter { price_currency, sku_able, quantity_unit, - quantity_number + quantity_number, + deleted FROM cqrs_inventory_product_query WHERE @@ -152,7 +158,8 @@ impl ViewRepository for InventoryDBPostgresAdapter { price_currency, sku_able, quantity_unit, - quantity_number + quantity_number, + deleted FROM cqrs_inventory_product_query WHERE @@ -207,9 +214,10 @@ impl ViewRepository for InventoryDBPostgresAdapter { price_currency, sku_able, quantity_unit, - quantity_number + quantity_number, + deleted ) VALUES ( - $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12 + $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13 );", version, view.name, @@ -223,6 +231,7 @@ impl ViewRepository for InventoryDBPostgresAdapter { view.sku_able, view.quantity_unit, view.quantity_number, + view.deleted ) .execute(&self.pool) .await @@ -245,7 +254,8 @@ impl ViewRepository for InventoryDBPostgresAdapter { price_currency = $9, sku_able = $10, quantity_unit = $11, - quantity_number = $12;", + quantity_number = $12, + deleted = $13;", version, view.name, view.description, @@ -257,7 +267,8 @@ impl ViewRepository for InventoryDBPostgresAdapter { view.price_currency, view.sku_able, view.quantity_unit, - view.quantity_number + view.quantity_number, + view.deleted ) .execute(&self.pool) .await diff --git a/src/inventory/domain/product_aggregate.rs b/src/inventory/domain/product_aggregate.rs index d6b1a2e..c518a7f 100644 --- a/src/inventory/domain/product_aggregate.rs +++ b/src/inventory/domain/product_aggregate.rs @@ -127,6 +127,8 @@ pub struct Product { category_id: Uuid, sku_able: bool, product_id: Uuid, + #[builder(default = "false")] + deleted: bool, } #[async_trait] @@ -169,6 +171,7 @@ impl Aggregate for Product { .sku_able(e.sku_able().clone()) .product_id(e.product_id().clone()) .quantity(e.quantity().clone()) + .deleted(false) .build() .unwrap(); } -- 2.39.5 From fb50c66ffb846352cd8eba3c489cf91eca215f02 Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Mon, 15 Jul 2024 22:36:02 +0530 Subject: [PATCH 2/2] fix: ignore deleted products while checking for product names --- ...f8bb4b13c3e090dc7d6455464b656ff44e0c2c5ebe.json} | 4 ++-- .../db/postgres/product_name_exists_for_category.rs | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) rename .sqlx/{query-53a5e7e87387ffc14013067a24c80b9d14c9864875177af5320c926d68cfb4ae.json => query-ff4bf8b635cd7144238c2df8bb4b13c3e090dc7d6455464b656ff44e0c2c5ebe.json} (72%) diff --git a/.sqlx/query-53a5e7e87387ffc14013067a24c80b9d14c9864875177af5320c926d68cfb4ae.json b/.sqlx/query-ff4bf8b635cd7144238c2df8bb4b13c3e090dc7d6455464b656ff44e0c2c5ebe.json similarity index 72% rename from .sqlx/query-53a5e7e87387ffc14013067a24c80b9d14c9864875177af5320c926d68cfb4ae.json rename to .sqlx/query-ff4bf8b635cd7144238c2df8bb4b13c3e090dc7d6455464b656ff44e0c2c5ebe.json index 5050200..6ceac82 100644 --- a/.sqlx/query-53a5e7e87387ffc14013067a24c80b9d14c9864875177af5320c926d68cfb4ae.json +++ b/.sqlx/query-ff4bf8b635cd7144238c2df8bb4b13c3e090dc7d6455464b656ff44e0c2c5ebe.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "SELECT EXISTS (\n SELECT 1\n FROM cqrs_inventory_product_query\n WHERE\n name = $1\n AND\n category_id = $2\n );", + "query": "SELECT EXISTS (\n SELECT 1\n FROM cqrs_inventory_product_query\n WHERE\n name = $1\n AND\n category_id = $2\n AND\n deleted = false\n );", "describe": { "columns": [ { @@ -19,5 +19,5 @@ null ] }, - "hash": "53a5e7e87387ffc14013067a24c80b9d14c9864875177af5320c926d68cfb4ae" + "hash": "ff4bf8b635cd7144238c2df8bb4b13c3e090dc7d6455464b656ff44e0c2c5ebe" } diff --git a/src/inventory/adapters/output/db/postgres/product_name_exists_for_category.rs b/src/inventory/adapters/output/db/postgres/product_name_exists_for_category.rs index 4763f73..5fb5e84 100644 --- a/src/inventory/adapters/output/db/postgres/product_name_exists_for_category.rs +++ b/src/inventory/adapters/output/db/postgres/product_name_exists_for_category.rs @@ -19,6 +19,8 @@ impl ProductNameExistsForCategoryDBPort for InventoryDBPostgresAdapter { name = $1 AND category_id = $2 + AND + deleted = false );", s.name(), s.category_id(), @@ -74,6 +76,17 @@ mod tests { // state exists assert!(db.product_name_exists_for_category(&product).await.unwrap()); + // Set product.deleted = true; now db.product_name_exists_for_category must return false + sqlx::query!( + "UPDATE cqrs_inventory_product_query SET deleted = true WHERE product_id = $1;", + product.product_id(), + ) + .execute(&db.pool) + .await + .unwrap(); + assert!(!db.product_name_exists_for_category(&product).await.unwrap()); + + settings.drop_db().await; } } -- 2.39.5