From cdb055fb42f608c4c586573d80dcfcdc05a67030 Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Mon, 15 Jul 2024 20:31:16 +0530 Subject: [PATCH 1/2] fix: add deleted flag to Category aggregate --- ...d725d269ef36288ed5e7fe1f7ad452deaab0e6.json} | 7 ++++--- ...d4031a7e77b47054d971997c0aa6284826f533.json} | 10 ++++++++-- ...24188bc1a0f21d1f3699e765b1b42fc7d23fde.json} | 7 ++++--- .../postgres/category_name_exists_for_store.rs | 5 +++-- .../output/db/postgres/category_view.rs | 17 +++++++++++------ src/inventory/domain/category_aggregate.rs | 2 ++ 6 files changed, 32 insertions(+), 16 deletions(-) rename .sqlx/{query-fed365b25499b05fbeb73416310b4c5ee1d3beb6dda94290c80c5ae038894116.json => query-14d111d7453b89f2346966a9fdd725d269ef36288ed5e7fe1f7ad452deaab0e6.json} (70%) rename .sqlx/{query-d396a3ccbe58a02acc0710700274eb3d28b2a1fe005190cd3599c2772dd01f7d.json => query-86ac358a97c56c0afcea72c5abd4031a7e77b47054d971997c0aa6284826f533.json} (66%) rename .sqlx/{query-2ca5f8ca1b3ac04175346c1d7e57a37ac4ccf493ebced9cf690f4030e78bd439.json => query-ff12c5b1a675a8c7de57a4538424188bc1a0f21d1f3699e765b1b42fc7d23fde.json} (59%) diff --git a/.sqlx/query-fed365b25499b05fbeb73416310b4c5ee1d3beb6dda94290c80c5ae038894116.json b/.sqlx/query-14d111d7453b89f2346966a9fdd725d269ef36288ed5e7fe1f7ad452deaab0e6.json similarity index 70% rename from .sqlx/query-fed365b25499b05fbeb73416310b4c5ee1d3beb6dda94290c80c5ae038894116.json rename to .sqlx/query-14d111d7453b89f2346966a9fdd725d269ef36288ed5e7fe1f7ad452deaab0e6.json index 8419d01..eb6f222 100644 --- a/.sqlx/query-fed365b25499b05fbeb73416310b4c5ee1d3beb6dda94290c80c5ae038894116.json +++ b/.sqlx/query-14d111d7453b89f2346966a9fdd725d269ef36288ed5e7fe1f7ad452deaab0e6.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "UPDATE\n cqrs_inventory_category_query\n SET\n version = $1,\n name = $2,\n description = $3,\n category_id = $4,\n store_id = $5;", + "query": "UPDATE\n cqrs_inventory_category_query\n SET\n version = $1,\n name = $2,\n description = $3,\n category_id = $4,\n store_id = $5,\n deleted = $6;", "describe": { "columns": [], "parameters": { @@ -9,10 +9,11 @@ "Text", "Text", "Uuid", - "Uuid" + "Uuid", + "Bool" ] }, "nullable": [] }, - "hash": "fed365b25499b05fbeb73416310b4c5ee1d3beb6dda94290c80c5ae038894116" + "hash": "14d111d7453b89f2346966a9fdd725d269ef36288ed5e7fe1f7ad452deaab0e6" } diff --git a/.sqlx/query-d396a3ccbe58a02acc0710700274eb3d28b2a1fe005190cd3599c2772dd01f7d.json b/.sqlx/query-86ac358a97c56c0afcea72c5abd4031a7e77b47054d971997c0aa6284826f533.json similarity index 66% rename from .sqlx/query-d396a3ccbe58a02acc0710700274eb3d28b2a1fe005190cd3599c2772dd01f7d.json rename to .sqlx/query-86ac358a97c56c0afcea72c5abd4031a7e77b47054d971997c0aa6284826f533.json index 26e15b0..31f555c 100644 --- a/.sqlx/query-d396a3ccbe58a02acc0710700274eb3d28b2a1fe005190cd3599c2772dd01f7d.json +++ b/.sqlx/query-86ac358a97c56c0afcea72c5abd4031a7e77b47054d971997c0aa6284826f533.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "SELECT \n name, description, category_id, store_id\n FROM\n cqrs_inventory_category_query\n WHERE\n category_id = $1;", + "query": "SELECT \n name, description, category_id, store_id, deleted\n FROM\n cqrs_inventory_category_query\n WHERE\n category_id = $1;", "describe": { "columns": [ { @@ -22,6 +22,11 @@ "ordinal": 3, "name": "store_id", "type_info": "Uuid" + }, + { + "ordinal": 4, + "name": "deleted", + "type_info": "Bool" } ], "parameters": { @@ -33,8 +38,9 @@ false, true, false, + false, false ] }, - "hash": "d396a3ccbe58a02acc0710700274eb3d28b2a1fe005190cd3599c2772dd01f7d" + "hash": "86ac358a97c56c0afcea72c5abd4031a7e77b47054d971997c0aa6284826f533" } diff --git a/.sqlx/query-2ca5f8ca1b3ac04175346c1d7e57a37ac4ccf493ebced9cf690f4030e78bd439.json b/.sqlx/query-ff12c5b1a675a8c7de57a4538424188bc1a0f21d1f3699e765b1b42fc7d23fde.json similarity index 59% rename from .sqlx/query-2ca5f8ca1b3ac04175346c1d7e57a37ac4ccf493ebced9cf690f4030e78bd439.json rename to .sqlx/query-ff12c5b1a675a8c7de57a4538424188bc1a0f21d1f3699e765b1b42fc7d23fde.json index afbefda..d965e81 100644 --- a/.sqlx/query-2ca5f8ca1b3ac04175346c1d7e57a37ac4ccf493ebced9cf690f4030e78bd439.json +++ b/.sqlx/query-ff12c5b1a675a8c7de57a4538424188bc1a0f21d1f3699e765b1b42fc7d23fde.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "INSERT INTO cqrs_inventory_category_query (\n version, name, description, category_id, store_id\n ) VALUES (\n $1, $2, $3, $4, $5\n );", + "query": "INSERT INTO cqrs_inventory_category_query (\n version, name, description, category_id, store_id, deleted\n ) VALUES (\n $1, $2, $3, $4, $5, $6\n );", "describe": { "columns": [], "parameters": { @@ -9,10 +9,11 @@ "Text", "Text", "Uuid", - "Uuid" + "Uuid", + "Bool" ] }, "nullable": [] }, - "hash": "2ca5f8ca1b3ac04175346c1d7e57a37ac4ccf493ebced9cf690f4030e78bd439" + "hash": "ff12c5b1a675a8c7de57a4538424188bc1a0f21d1f3699e765b1b42fc7d23fde" } 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 44cce82..3dab26a 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 @@ -43,13 +43,14 @@ pub mod tests { pub async fn create_dummy_category_record(c: &Category, db: &InventoryDBPostgresAdapter) { sqlx::query!( "INSERT INTO cqrs_inventory_category_query - (version, name, description, category_id, store_id) - VALUES ($1, $2, $3, $4, $5);", + (version, name, description, category_id, store_id, deleted) + VALUES ($1, $2, $3, $4, $5, $6);", 1, c.name(), c.description().as_ref().unwrap(), c.category_id(), c.store_id(), + c.deleted().clone(), ) .execute(&db.pool) .await diff --git a/src/inventory/adapters/output/db/postgres/category_view.rs b/src/inventory/adapters/output/db/postgres/category_view.rs index 95cc996..61f270f 100644 --- a/src/inventory/adapters/output/db/postgres/category_view.rs +++ b/src/inventory/adapters/output/db/postgres/category_view.rs @@ -24,6 +24,7 @@ pub struct CategoryView { description: Option, category_id: Uuid, store_id: Uuid, + deleted: bool, } // This updates the view with events as they are committed. @@ -37,6 +38,7 @@ impl View for CategoryView { self.description = val.description().clone(); self.category_id = val.category_id().clone(); self.store_id = val.store_id().clone(); + self.deleted = false; } _ => (), } @@ -54,7 +56,7 @@ impl ViewRepository for InventoryDBPostgresAdapter { let res = sqlx::query_as!( CategoryView, "SELECT - name, description, category_id, store_id + name, description, category_id, store_id, deleted FROM cqrs_inventory_category_query WHERE @@ -79,7 +81,7 @@ impl ViewRepository for InventoryDBPostgresAdapter { let res = sqlx::query_as!( CategoryView, "SELECT - name, description, category_id, store_id + name, description, category_id, store_id, deleted FROM cqrs_inventory_category_query WHERE @@ -123,15 +125,16 @@ impl ViewRepository for InventoryDBPostgresAdapter { let version = context.version + 1; sqlx::query!( "INSERT INTO cqrs_inventory_category_query ( - version, name, description, category_id, store_id + version, name, description, category_id, store_id, deleted ) VALUES ( - $1, $2, $3, $4, $5 + $1, $2, $3, $4, $5, $6 );", version, view.name, view.description, view.category_id, view.store_id, + view.deleted ) .execute(&self.pool) .await @@ -147,12 +150,14 @@ impl ViewRepository for InventoryDBPostgresAdapter { name = $2, description = $3, category_id = $4, - store_id = $5;", + store_id = $5, + deleted = $6;", version, view.name, view.description, view.category_id, - view.store_id + view.store_id, + view.deleted ) .execute(&self.pool) .await diff --git a/src/inventory/domain/category_aggregate.rs b/src/inventory/domain/category_aggregate.rs index 6998715..440ded3 100644 --- a/src/inventory/domain/category_aggregate.rs +++ b/src/inventory/domain/category_aggregate.rs @@ -22,6 +22,8 @@ pub struct Category { description: Option, store_id: Uuid, category_id: Uuid, + #[builder(default = "false")] + deleted: bool, } #[async_trait] From 92219cd4f36b9a598d6ab151a427e23f479e52a7 Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Mon, 15 Jul 2024 20:33:17 +0530 Subject: [PATCH 2/2] fix: ignore deleted categories while checking for category names --- ...24ade45836ccb6745ff2096f1d4f733b702142ca0.json} | 4 ++-- .../db/postgres/category_name_exists_for_store.rs | 14 ++++++++++++++ .../output/db/postgres/store_name_exists.rs | 2 +- 3 files changed, 17 insertions(+), 3 deletions(-) rename .sqlx/{query-5944442c15d28d47654afae92815ccefea89cc9aee705e0e1e54a3bf884bb194.json => query-7acab15a75b02104059e95c24ade45836ccb6745ff2096f1d4f733b702142ca0.json} (72%) diff --git a/.sqlx/query-5944442c15d28d47654afae92815ccefea89cc9aee705e0e1e54a3bf884bb194.json b/.sqlx/query-7acab15a75b02104059e95c24ade45836ccb6745ff2096f1d4f733b702142ca0.json similarity index 72% rename from .sqlx/query-5944442c15d28d47654afae92815ccefea89cc9aee705e0e1e54a3bf884bb194.json rename to .sqlx/query-7acab15a75b02104059e95c24ade45836ccb6745ff2096f1d4f733b702142ca0.json index 8ff850c..42c3b14 100644 --- a/.sqlx/query-5944442c15d28d47654afae92815ccefea89cc9aee705e0e1e54a3bf884bb194.json +++ b/.sqlx/query-7acab15a75b02104059e95c24ade45836ccb6745ff2096f1d4f733b702142ca0.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "SELECT EXISTS (\n SELECT 1\n FROM cqrs_inventory_category_query\n WHERE\n name = $1\n AND\n store_id = $2\n );", + "query": "SELECT EXISTS (\n SELECT 1\n FROM cqrs_inventory_category_query\n WHERE\n name = $1\n AND\n store_id = $2\n AND\n deleted = false\n );", "describe": { "columns": [ { @@ -19,5 +19,5 @@ null ] }, - "hash": "5944442c15d28d47654afae92815ccefea89cc9aee705e0e1e54a3bf884bb194" + "hash": "7acab15a75b02104059e95c24ade45836ccb6745ff2096f1d4f733b702142ca0" } 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 3dab26a..194d395 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 @@ -19,6 +19,8 @@ impl CategoryNameExistsForStoreDBPort for InventoryDBPostgresAdapter { name = $1 AND store_id = $2 + AND + deleted = false );", s.name(), s.store_id(), @@ -85,6 +87,18 @@ 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;", + category.category_id(), + ) + .execute(&db.pool) + .await + .unwrap(); + assert!(!db.category_name_exists_for_store(&category).await.unwrap()); + + settings.drop_db().await; } } diff --git a/src/inventory/adapters/output/db/postgres/store_name_exists.rs b/src/inventory/adapters/output/db/postgres/store_name_exists.rs index 279c44c..81da3be 100644 --- a/src/inventory/adapters/output/db/postgres/store_name_exists.rs +++ b/src/inventory/adapters/output/db/postgres/store_name_exists.rs @@ -66,7 +66,7 @@ mod tests { // state exists assert!(db.store_name_exists(&store).await.unwrap()); - // Set store.deleted = true; now db.store_id_exists must return false + // Set store.deleted = true; now db.store_name_exists must return false sqlx::query!( "UPDATE cqrs_inventory_store_query SET deleted = true WHERE store_id = $1;", store.store_id()