feat: add deleted flag to Store aggregate

This commit is contained in:
Aravinth Manivannan 2024-07-15 20:23:00 +05:30
parent eae59bd641
commit c119687853
Signed by: realaravinth
GPG key ID: F8F50389936984FF
4 changed files with 34 additions and 37 deletions

View file

@ -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());

View file

@ -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());

View file

@ -24,6 +24,7 @@ pub struct StoreView {
address: Option<String>,
store_id: Uuid,
owner: Uuid,
deleted: bool,
}
// This updates the view with events as they are committed.
@ -37,6 +38,7 @@ impl View<Store> 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<StoreView, Store> 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<StoreView, Store> 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<StoreView, Store> 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<StoreView, Store> 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

View file

@ -22,6 +22,8 @@ pub struct Store {
address: Option<String>,
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;
}
_ => (),
}