From c119687853282bf0d069df41428c2359d5768ead Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Mon, 15 Jul 2024 20:23:00 +0530 Subject: [PATCH] feat: add deleted flag to Store aggregate --- .../output/db/postgres/store_id_exists.rs | 33 +++++++++++-------- .../output/db/postgres/store_name_exists.rs | 15 ++------- .../adapters/output/db/postgres/store_view.rs | 15 ++++++--- src/inventory/domain/store_aggregate.rs | 8 ++--- 4 files changed, 34 insertions(+), 37 deletions(-) diff --git a/src/inventory/adapters/output/db/postgres/store_id_exists.rs b/src/inventory/adapters/output/db/postgres/store_id_exists.rs index ab64000..11d8571 100644 --- a/src/inventory/adapters/output/db/postgres/store_id_exists.rs +++ b/src/inventory/adapters/output/db/postgres/store_id_exists.rs @@ -29,13 +29,30 @@ impl StoreIDExistsDBPort for InventoryDBPostgresAdapter { } #[cfg(test)] -mod tests { +pub mod tests { use uuid::Uuid; use crate::utils::uuid::tests::UUID; use super::*; + pub async fn create_dummy_store_record(s: &Store, db: &InventoryDBPostgresAdapter) { + sqlx::query!( + "INSERT INTO cqrs_inventory_store_query + (version, name, address, store_id, owner, deleted) + VALUES ($1, $2, $3, $4, $5 ,$6);", + 1, + s.name(), + s.address().as_ref().unwrap(), + s.store_id(), + s.owner(), + false + ) + .execute(&db.pool) + .await + .unwrap(); + } + #[actix_rt::test] async fn test_postgres_store_exists() { let store_id = Uuid::new_v4(); @@ -58,19 +75,7 @@ mod tests { // state doesn't exist assert!(!db.store_id_exists(&store).await.unwrap()); - sqlx::query!( - "INSERT INTO cqrs_inventory_store_query - (version, name, address, store_id, owner) - VALUES ($1, $2, $3, $4, $5);", - 1, - store.name(), - store.address().as_ref().unwrap(), - store.store_id(), - store.owner(), - ) - .execute(&db.pool) - .await - .unwrap(); + create_dummy_store_record(&store, &db).await; // state exists assert!(db.store_id_exists(&store).await.unwrap()); 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 0ac5260..6300cc1 100644 --- a/src/inventory/adapters/output/db/postgres/store_name_exists.rs +++ b/src/inventory/adapters/output/db/postgres/store_name_exists.rs @@ -35,6 +35,7 @@ mod tests { use crate::utils::uuid::tests::UUID; use super::*; + use crate::inventory::adapters::output::db::postgres::store_id_exists::tests::create_dummy_store_record; #[actix_rt::test] async fn test_postgres_store_exists() { @@ -58,19 +59,7 @@ mod tests { // state doesn't exist assert!(!db.store_name_exists(&store).await.unwrap()); - sqlx::query!( - "INSERT INTO cqrs_inventory_store_query - (version, name, address, store_id, owner) - VALUES ($1, $2, $3, $4, $5);", - 1, - store.name(), - store.address().as_ref().unwrap(), - store.store_id(), - store.owner(), - ) - .execute(&db.pool) - .await - .unwrap(); + create_dummy_store_record(&store, &db).await; // state exists assert!(db.store_name_exists(&store).await.unwrap()); diff --git a/src/inventory/adapters/output/db/postgres/store_view.rs b/src/inventory/adapters/output/db/postgres/store_view.rs index 3743734..664af17 100644 --- a/src/inventory/adapters/output/db/postgres/store_view.rs +++ b/src/inventory/adapters/output/db/postgres/store_view.rs @@ -24,6 +24,7 @@ pub struct StoreView { address: Option, store_id: Uuid, owner: Uuid, + deleted: bool, } // This updates the view with events as they are committed. @@ -37,6 +38,7 @@ impl View for StoreView { self.address = val.address().clone(); self.store_id = val.store_id().clone(); self.owner = val.owner().clone(); + self.deleted = false; } _ => (), } @@ -54,7 +56,7 @@ impl ViewRepository for InventoryDBPostgresAdapter { let res = sqlx::query_as!( StoreView, "SELECT - name, address, store_id, owner + name, address, store_id, owner, deleted FROM cqrs_inventory_store_query WHERE @@ -79,7 +81,7 @@ impl ViewRepository for InventoryDBPostgresAdapter { let res = sqlx::query_as!( StoreView, "SELECT - name, address, store_id, owner + name, address, store_id, owner, deleted FROM cqrs_inventory_store_query WHERE @@ -123,15 +125,16 @@ impl ViewRepository for InventoryDBPostgresAdapter { let version = context.version + 1; sqlx::query!( "INSERT INTO cqrs_inventory_store_query ( - version, name, address, store_id, owner + version, name, address, store_id, owner, deleted ) VALUES ( - $1, $2, $3, $4, $5 + $1, $2, $3, $4, $5, $6 );", version, view.name, view.address, view.store_id, view.owner, + view.deleted, ) .execute(&self.pool) .await @@ -147,12 +150,14 @@ impl ViewRepository for InventoryDBPostgresAdapter { name = $2, address = $3, store_id = $4, - owner = $5;", + owner = $5, + deleted = $6;", version, view.name, view.address, view.store_id, view.owner, + view.deleted, ) .execute(&self.pool) .await diff --git a/src/inventory/domain/store_aggregate.rs b/src/inventory/domain/store_aggregate.rs index 7542f3b..ad3ed8a 100644 --- a/src/inventory/domain/store_aggregate.rs +++ b/src/inventory/domain/store_aggregate.rs @@ -22,6 +22,8 @@ pub struct Store { address: Option, owner: Uuid, store_id: Uuid, + #[builder(default = "false")] + deleted: bool, } #[async_trait] @@ -49,11 +51,6 @@ impl Aggregate for Store { Ok(vec![InventoryEvent::StoreAdded(res)]) } _ => Ok(Vec::default()), - // InventoryCommand::AddCategory(cmd) => { - // let res = services.add_category().add_store(cmd).await?; - // Ok(vec![InventoryEvent::CategoryAdded(res)]) - // - // } } } @@ -64,6 +61,7 @@ impl Aggregate for Store { self.address = e.address().as_ref().map(|s| s.to_string()); self.owner = e.owner().clone(); self.store_id = e.store_id().clone(); + self.deleted = false; } _ => (), }