From afda7a6ec80434933a620720d678be87494d4bff Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Fri, 10 Jan 2025 20:11:17 +0530 Subject: [PATCH] feat: utils to init cqrs framework --- .../meili/add_product_to_store.rs | 4 +++- .../adapters/output/db/category_view.rs | 16 +++++++++++++++ .../adapters/output/db/customization_view.rs | 20 ++++++++++++++++++- src/ordering/adapters/output/db/kot_view.rs | 16 +++++++++++++++ .../adapters/output/db/line_item_view.rs | 16 +++++++++++++++ src/ordering/adapters/output/db/mod.rs | 14 ++++++------- src/ordering/adapters/output/db/order_view.rs | 16 +++++++++++++++ .../adapters/output/db/product_view.rs | 16 +++++++++++++++ src/ordering/adapters/output/db/store_view.rs | 16 +++++++++++++++ .../meili/add_product_to_store.rs | 4 +++- 10 files changed, 128 insertions(+), 10 deletions(-) diff --git a/src/inventory/adapters/output/full_text_search/meili/add_product_to_store.rs b/src/inventory/adapters/output/full_text_search/meili/add_product_to_store.rs index 4f83945..e56034b 100644 --- a/src/inventory/adapters/output/full_text_search/meili/add_product_to_store.rs +++ b/src/inventory/adapters/output/full_text_search/meili/add_product_to_store.rs @@ -47,7 +47,9 @@ impl AddProductToStoreFTSPort for InventoryFTSMeili { product: &Product, category: &Category, ) -> InventoryFTSResult<()> { - let store_index = self.client.index(format!("inventory-{}",category.store_id())); + let store_index = self + .client + .index(format!("inventory-{}", category.store_id())); let meili_product = MeiliProduct::new(product, category); store_index .add_documents(&[meili_product], Some("product_id")) diff --git a/src/ordering/adapters/output/db/category_view.rs b/src/ordering/adapters/output/db/category_view.rs index 247799d..503d9c8 100644 --- a/src/ordering/adapters/output/db/category_view.rs +++ b/src/ordering/adapters/output/db/category_view.rs @@ -10,6 +10,8 @@ use uuid::Uuid; use super::errors::*; use super::OrderingDBPostgresAdapter; +use crate::ordering::adapters::types::{OrderingCategoryCqrsExec, OrderingCategoryCqrsView}; +use crate::ordering::application::services::OrderingServicesObj; use crate::ordering::domain::category_aggregate::*; use crate::ordering::domain::events::OrderingEvent; use crate::utils::parse_aggregate_id::parse_aggregate_id; @@ -222,6 +224,20 @@ impl Query for OrderingDBPostgresAdapter { } } +pub fn init_cqrs( + db: OrderingDBPostgresAdapter, + services: OrderingServicesObj, +) -> (OrderingCategoryCqrsExec, OrderingCategoryCqrsView) { + let queries: Vec>> = vec![Box::new(db.clone())]; + + let pool = db.pool.clone(); + + ( + std::sync::Arc::new(postgres_es::postgres_cqrs(pool.clone(), queries, services)), + std::sync::Arc::new(db.clone()), + ) +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/ordering/adapters/output/db/customization_view.rs b/src/ordering/adapters/output/db/customization_view.rs index c31a26b..b6fb775 100644 --- a/src/ordering/adapters/output/db/customization_view.rs +++ b/src/ordering/adapters/output/db/customization_view.rs @@ -12,13 +12,17 @@ use uuid::Uuid; use super::errors::*; use super::OrderingDBPostgresAdapter; +use crate::ordering::adapters::types::{ + OrderingCustomizationCqrsExec, OrderingCustomizationCqrsView, +}; +use crate::ordering::application::services::OrderingServicesObj; use crate::ordering::domain::{customization_aggregate::*, events::OrderingEvent}; use crate::utils::parse_aggregate_id::parse_aggregate_id; pub const NEW_CUSTOMIZATION_NON_UUID: &str = "ordering_new_customization_non_uuid-asdfa"; #[derive(Debug, Default, Serialize, Deserialize)] -struct CustomizationView { +pub struct CustomizationView { name: String, product_id: Uuid, customization_id: Uuid, @@ -219,6 +223,20 @@ impl Query for OrderingDBPostgresAdapter { } } +pub fn init_cqrs( + db: OrderingDBPostgresAdapter, + services: OrderingServicesObj, +) -> (OrderingCustomizationCqrsExec, OrderingCustomizationCqrsView) { + let queries: Vec>> = vec![Box::new(db.clone())]; + + let pool = db.pool.clone(); + + ( + std::sync::Arc::new(postgres_es::postgres_cqrs(pool.clone(), queries, services)), + std::sync::Arc::new(db.clone()), + ) +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/ordering/adapters/output/db/kot_view.rs b/src/ordering/adapters/output/db/kot_view.rs index 2adccd8..58e67a2 100644 --- a/src/ordering/adapters/output/db/kot_view.rs +++ b/src/ordering/adapters/output/db/kot_view.rs @@ -13,6 +13,8 @@ use uuid::Uuid; use super::errors::*; use super::OrderingDBPostgresAdapter; +use crate::ordering::adapters::types::{OrderingKotCqrsExec, OrderingKotCqrsView}; +use crate::ordering::application::services::OrderingServicesObj; use crate::ordering::domain::events::OrderingEvent; use crate::ordering::domain::kot_aggregate::*; use crate::types::quantity::*; @@ -229,6 +231,20 @@ impl Query for OrderingDBPostgresAdapter { } } +pub fn init_cqrs( + db: OrderingDBPostgresAdapter, + services: OrderingServicesObj, +) -> (OrderingKotCqrsExec, OrderingKotCqrsView) { + let queries: Vec>> = vec![Box::new(db.clone())]; + + let pool = db.pool.clone(); + + ( + std::sync::Arc::new(postgres_es::postgres_cqrs(pool.clone(), queries, services)), + std::sync::Arc::new(db.clone()), + ) +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/ordering/adapters/output/db/line_item_view.rs b/src/ordering/adapters/output/db/line_item_view.rs index 3e3fa33..ee61acc 100644 --- a/src/ordering/adapters/output/db/line_item_view.rs +++ b/src/ordering/adapters/output/db/line_item_view.rs @@ -13,6 +13,8 @@ use uuid::Uuid; use super::errors::*; use super::OrderingDBPostgresAdapter; +use crate::ordering::adapters::types::{OrderingLineItemCqrsExec, OrderingLineItemCqrsView}; +use crate::ordering::application::services::OrderingServicesObj; use crate::ordering::domain::events::OrderingEvent; use crate::ordering::domain::line_item_aggregate::*; use crate::types::quantity::*; @@ -319,6 +321,20 @@ impl Query for OrderingDBPostgresAdapter { } } +pub fn init_cqrs( + db: OrderingDBPostgresAdapter, + services: OrderingServicesObj, +) -> (OrderingLineItemCqrsExec, OrderingLineItemCqrsView) { + let queries: Vec>> = vec![Box::new(db.clone())]; + + let pool = db.pool.clone(); + + ( + std::sync::Arc::new(postgres_es::postgres_cqrs(pool.clone(), queries, services)), + std::sync::Arc::new(db.clone()), + ) +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/ordering/adapters/output/db/mod.rs b/src/ordering/adapters/output/db/mod.rs index d02dffb..1e704ec 100644 --- a/src/ordering/adapters/output/db/mod.rs +++ b/src/ordering/adapters/output/db/mod.rs @@ -10,24 +10,24 @@ use crate::db::{migrate::RunMigrations, sqlx_postgres::Postgres}; mod category_id_exists; mod category_name_exists_for_store; -mod category_view; +pub mod category_view; mod customization_id_exists; mod customization_name_exists_for_product; -mod customization_view; +pub mod customization_view; mod errors; mod get_category; mod kot_id_exists; -mod kot_view; +pub mod kot_view; mod line_item_id_exists; -mod line_item_view; +pub mod line_item_view; mod order_id_exists; -mod order_view; +pub mod order_view; mod product_id_exists; mod product_name_exists_for_category; -mod product_view; +pub mod product_view; mod store_id_exists; mod store_name_exists; -mod store_view; +pub mod store_view; #[derive(Clone)] pub struct OrderingDBPostgresAdapter { diff --git a/src/ordering/adapters/output/db/order_view.rs b/src/ordering/adapters/output/db/order_view.rs index b075b57..00e4bb0 100644 --- a/src/ordering/adapters/output/db/order_view.rs +++ b/src/ordering/adapters/output/db/order_view.rs @@ -13,6 +13,8 @@ use uuid::Uuid; use super::errors::*; use super::OrderingDBPostgresAdapter; +use crate::ordering::adapters::types::{OrderingOrderCqrsExec, OrderingOrderCqrsView}; +use crate::ordering::application::services::OrderingServicesObj; use crate::ordering::domain::events::OrderingEvent; use crate::ordering::domain::order_aggregate::*; use crate::utils::parse_aggregate_id::parse_aggregate_id; @@ -237,6 +239,20 @@ impl Query for OrderingDBPostgresAdapter { } } +pub fn init_cqrs( + db: OrderingDBPostgresAdapter, + services: OrderingServicesObj, +) -> (OrderingOrderCqrsExec, OrderingOrderCqrsView) { + let queries: Vec>> = vec![Box::new(db.clone())]; + + let pool = db.pool.clone(); + + ( + std::sync::Arc::new(postgres_es::postgres_cqrs(pool.clone(), queries, services)), + std::sync::Arc::new(db.clone()), + ) +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/ordering/adapters/output/db/product_view.rs b/src/ordering/adapters/output/db/product_view.rs index c192122..7e09910 100644 --- a/src/ordering/adapters/output/db/product_view.rs +++ b/src/ordering/adapters/output/db/product_view.rs @@ -12,6 +12,8 @@ use uuid::Uuid; use super::errors::*; use super::OrderingDBPostgresAdapter; +use crate::ordering::adapters::types::{OrderingProductCqrsExec, OrderingProductCqrsView}; +use crate::ordering::application::services::OrderingServicesObj; use crate::ordering::domain::events::OrderingEvent; use crate::ordering::domain::product_aggregate::{Product, ProductBuilder}; use crate::types::currency::*; @@ -348,6 +350,20 @@ impl Query for OrderingDBPostgresAdapter { } } +pub fn init_cqrs( + db: OrderingDBPostgresAdapter, + services: OrderingServicesObj, +) -> (OrderingProductCqrsExec, OrderingProductCqrsView) { + let queries: Vec>> = vec![Box::new(db.clone())]; + + let pool = db.pool.clone(); + + ( + std::sync::Arc::new(postgres_es::postgres_cqrs(pool.clone(), queries, services)), + std::sync::Arc::new(db.clone()), + ) +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/ordering/adapters/output/db/store_view.rs b/src/ordering/adapters/output/db/store_view.rs index 7c3a29c..e99de17 100644 --- a/src/ordering/adapters/output/db/store_view.rs +++ b/src/ordering/adapters/output/db/store_view.rs @@ -10,6 +10,8 @@ use uuid::Uuid; use super::errors::*; use super::OrderingDBPostgresAdapter; +use crate::ordering::adapters::types::{OrderingStoreCqrsExec, OrderingStoreCqrsView}; +use crate::ordering::application::services::OrderingServicesObj; use crate::ordering::domain::events::OrderingEvent; use crate::ordering::domain::store_aggregate::*; use crate::utils::parse_aggregate_id::parse_aggregate_id; @@ -216,6 +218,20 @@ impl Query for OrderingDBPostgresAdapter { } } +pub fn init_cqrs( + db: OrderingDBPostgresAdapter, + services: OrderingServicesObj, +) -> (OrderingStoreCqrsExec, OrderingStoreCqrsView) { + let queries: Vec>> = vec![Box::new(db.clone())]; + + let pool = db.pool.clone(); + + ( + std::sync::Arc::new(postgres_es::postgres_cqrs(pool.clone(), queries, services)), + std::sync::Arc::new(db.clone()), + ) +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/ordering/adapters/output/full_text_search/meili/add_product_to_store.rs b/src/ordering/adapters/output/full_text_search/meili/add_product_to_store.rs index 2928eb0..e12a6db 100644 --- a/src/ordering/adapters/output/full_text_search/meili/add_product_to_store.rs +++ b/src/ordering/adapters/output/full_text_search/meili/add_product_to_store.rs @@ -47,7 +47,9 @@ impl AddProductToStoreFTSPort for OrderingFTSMeili { product: &Product, category: &Category, ) -> OrderingFTSResult<()> { - let store_index = self.client.index(format!("ordering-{}",category.store_id())); + let store_index = self + .client + .index(format!("ordering-{}", category.store_id())); let meili_product = MeiliProduct::new(product, category); store_index .add_documents(&[meili_product], Some("product_id"))