diff --git a/src/billing/adapters/input/web/bill.rs b/src/billing/adapters/input/web/bill.rs index c9e58df..54af990 100644 --- a/src/billing/adapters/input/web/bill.rs +++ b/src/billing/adapters/input/web/bill.rs @@ -61,10 +61,10 @@ const UUID: Uuid = uuid::uuid!("67e55044-10b1-426f-9247-bb680e5fe0c8"); #[post("/billing/store/add")] #[tracing::instrument( name = "add store handler", - skip(billing_store_cqrs_exec, billing_store_cqrs_view, uuid_generator) + skip(billing_cqrs_exec, billing_store_cqrs_view, uuid_generator) )] async fn add_store_submit_handler( - billing_store_cqrs_exec: types::WebBillingStoreCqrsExec, + billing_cqrs_exec: types::WebBillingCqrsExec, billing_store_cqrs_view: types::WebBillingStoreCqrsView, uuid_generator: WebGetUUIDInterfaceObj, req: HttpRequest, @@ -83,7 +83,7 @@ async fn add_store_submit_handler( .build() .unwrap(); - billing_store_cqrs_exec + billing_cqrs_exec .execute(&store_uuid_str, BillingCommand::AddStore(cmd)) .await .unwrap(); @@ -132,10 +132,10 @@ async fn add_bill_page_handler() -> WebJsonRepsonse { #[post("/billing/bill/add")] #[tracing::instrument( name = "add bill handler", - skip(billing_bill_cqrs_exec, billing_bill_cqrs_view,) + skip(billing_cqrs_exec, billing_bill_cqrs_view,) )] async fn add_bill_submit_handler( - billing_bill_cqrs_exec: types::WebBillingBillCqrsExec, + billing_cqrs_exec: types::WebBillingCqrsExec, billing_bill_cqrs_view: types::WebBillingBillCqrsView, req: HttpRequest, // id: Identity, @@ -153,7 +153,7 @@ async fn add_bill_submit_handler( .build() .unwrap(); - billing_bill_cqrs_exec + billing_cqrs_exec .execute(&bill_uuid_str, BillingCommand::AddBill(cmd)) .await .unwrap(); diff --git a/src/billing/adapters/mod.rs b/src/billing/adapters/mod.rs index e453b2d..94d2c89 100644 --- a/src/billing/adapters/mod.rs +++ b/src/billing/adapters/mod.rs @@ -53,17 +53,21 @@ pub fn load_adapters(pool: PgPool, settings: Settings) -> impl FnOnce(&mut web:: let (line_item_cqrs_exec, line_item_cqrs_query) = line_item_view::init_cqrs(db.clone(), services.clone()); + let billing_cqrs_exec = types::BillingCqrsExecBuilder::default() + .bill(bill_cqrs_exec) + .line_item(line_item_cqrs_exec) + .store(store_cqrs_exec) + .build() + .unwrap(); + let f = move |cfg: &mut web::ServiceConfig| { cfg.configure(input::web::load_ctx()); - cfg.app_data(Data::new(bill_cqrs_exec.clone())); cfg.app_data(Data::new(bill_cqrs_query.clone())); - - cfg.app_data(Data::new(store_cqrs_exec.clone())); cfg.app_data(Data::new(store_cqrs_query.clone())); - - cfg.app_data(Data::new(line_item_cqrs_exec.clone())); cfg.app_data(Data::new(line_item_cqrs_query.clone())); + + cfg.app_data(Data::new(Arc::new(billing_cqrs_exec))); }; Box::new(f) diff --git a/src/billing/adapters/types.rs b/src/billing/adapters/types.rs index 316a085..f5c3d59 100644 --- a/src/billing/adapters/types.rs +++ b/src/billing/adapters/types.rs @@ -1,7 +1,3 @@ -// SPDX-FileCopyrightText: 2024 Aravinth Manivannan -// -// SPDX-License-Identifier: AGPL-3.0-or-later - // SPDX-FileCopyrightText: 2024 Aravinth Manivannan // // SPDX-License-Identifier: AGPL-3.0-or-later @@ -10,7 +6,8 @@ use std::sync::Arc; use actix_web::web::Data; -use cqrs_es::persist::ViewRepository; +use cqrs_es::{persist::ViewRepository, AggregateError}; +use derive_builder::Builder; use postgres_es::PostgresCqrs; use crate::billing::{ @@ -21,8 +18,11 @@ use crate::billing::{ BillingDBPostgresAdapter, }, }, - application::services::BillingServicesObj, - domain::{bill_aggregate::Bill, line_item_aggregate::LineItem, store_aggregate::Store}, + application::services::{errors::BillingError, BillingServicesObj}, + domain::{ + bill_aggregate::Bill, commands::BillingCommand, line_item_aggregate::LineItem, + store_aggregate::Store, + }, }; pub type WebBillingRoutesRepository = Data>; @@ -30,16 +30,38 @@ pub type WebBillingRoutesRepository = Data>; pub type WebBillingServiceObj = Data; pub type BillingBillCqrsExec = Arc>; -pub type WebBillingBillCqrsExec = Data; pub type BillingBillCqrsView = Arc>; pub type WebBillingBillCqrsView = Data; pub type BillingLineItemCqrsExec = Arc>; -pub type WebBillingLineItemCqrsExec = Data; pub type BillingLineItemCqrsView = Arc>; pub type WebBillingLineItemCqrsView = Data; pub type BillingStoreCqrsExec = Arc>; -pub type WebBillingStoreCqrsExec = Data; pub type BillingStoreCqrsView = Arc>; pub type WebBillingStoreCqrsView = Data; + +pub type WebBillingCqrsExec = Data>; + +#[derive(Clone, Builder)] +pub struct BillingCqrsExec { + bill: BillingBillCqrsExec, + line_item: BillingLineItemCqrsExec, + store: BillingStoreCqrsExec, +} + +impl BillingCqrsExec { + pub async fn execute( + &self, + aggregate_id: &str, + command: BillingCommand, + ) -> Result<(), AggregateError> { + self.bill.execute(aggregate_id, command.clone()).await?; + self.line_item + .execute(aggregate_id, command.clone()) + .await?; + self.store.execute(aggregate_id, command).await?; + + Ok(()) + } +}