fix: rename bililng cqrs types, and use struct to aggregate all execs

This commit is contained in:
Aravinth Manivannan 2025-01-09 01:13:21 +05:30
parent 3e884cac06
commit ae9cbe953d
Signed by: realaravinth
GPG key ID: F8F50389936984FF
3 changed files with 47 additions and 21 deletions

View file

@ -61,10 +61,10 @@ const UUID: Uuid = uuid::uuid!("67e55044-10b1-426f-9247-bb680e5fe0c8");
#[post("/billing/store/add")] #[post("/billing/store/add")]
#[tracing::instrument( #[tracing::instrument(
name = "add store handler", 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( async fn add_store_submit_handler(
billing_store_cqrs_exec: types::WebBillingStoreCqrsExec, billing_cqrs_exec: types::WebBillingCqrsExec,
billing_store_cqrs_view: types::WebBillingStoreCqrsView, billing_store_cqrs_view: types::WebBillingStoreCqrsView,
uuid_generator: WebGetUUIDInterfaceObj, uuid_generator: WebGetUUIDInterfaceObj,
req: HttpRequest, req: HttpRequest,
@ -83,7 +83,7 @@ async fn add_store_submit_handler(
.build() .build()
.unwrap(); .unwrap();
billing_store_cqrs_exec billing_cqrs_exec
.execute(&store_uuid_str, BillingCommand::AddStore(cmd)) .execute(&store_uuid_str, BillingCommand::AddStore(cmd))
.await .await
.unwrap(); .unwrap();
@ -132,10 +132,10 @@ async fn add_bill_page_handler() -> WebJsonRepsonse<impl Responder> {
#[post("/billing/bill/add")] #[post("/billing/bill/add")]
#[tracing::instrument( #[tracing::instrument(
name = "add bill handler", 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( async fn add_bill_submit_handler(
billing_bill_cqrs_exec: types::WebBillingBillCqrsExec, billing_cqrs_exec: types::WebBillingCqrsExec,
billing_bill_cqrs_view: types::WebBillingBillCqrsView, billing_bill_cqrs_view: types::WebBillingBillCqrsView,
req: HttpRequest, req: HttpRequest,
// id: Identity, // id: Identity,
@ -153,7 +153,7 @@ async fn add_bill_submit_handler(
.build() .build()
.unwrap(); .unwrap();
billing_bill_cqrs_exec billing_cqrs_exec
.execute(&bill_uuid_str, BillingCommand::AddBill(cmd)) .execute(&bill_uuid_str, BillingCommand::AddBill(cmd))
.await .await
.unwrap(); .unwrap();

View file

@ -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) = let (line_item_cqrs_exec, line_item_cqrs_query) =
line_item_view::init_cqrs(db.clone(), services.clone()); 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| { let f = move |cfg: &mut web::ServiceConfig| {
cfg.configure(input::web::load_ctx()); 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(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(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(line_item_cqrs_query.clone()));
cfg.app_data(Data::new(Arc::new(billing_cqrs_exec)));
}; };
Box::new(f) Box::new(f)

View file

@ -1,7 +1,3 @@
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net> // SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
// //
// SPDX-License-Identifier: AGPL-3.0-or-later // SPDX-License-Identifier: AGPL-3.0-or-later
@ -10,7 +6,8 @@
use std::sync::Arc; use std::sync::Arc;
use actix_web::web::Data; 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 postgres_es::PostgresCqrs;
use crate::billing::{ use crate::billing::{
@ -21,8 +18,11 @@ use crate::billing::{
BillingDBPostgresAdapter, BillingDBPostgresAdapter,
}, },
}, },
application::services::BillingServicesObj, application::services::{errors::BillingError, BillingServicesObj},
domain::{bill_aggregate::Bill, line_item_aggregate::LineItem, store_aggregate::Store}, domain::{
bill_aggregate::Bill, commands::BillingCommand, line_item_aggregate::LineItem,
store_aggregate::Store,
},
}; };
pub type WebBillingRoutesRepository = Data<Arc<RoutesRepository>>; pub type WebBillingRoutesRepository = Data<Arc<RoutesRepository>>;
@ -30,16 +30,38 @@ pub type WebBillingRoutesRepository = Data<Arc<RoutesRepository>>;
pub type WebBillingServiceObj = Data<BillingServicesObj>; pub type WebBillingServiceObj = Data<BillingServicesObj>;
pub type BillingBillCqrsExec = Arc<PostgresCqrs<Bill>>; pub type BillingBillCqrsExec = Arc<PostgresCqrs<Bill>>;
pub type WebBillingBillCqrsExec = Data<BillingBillCqrsExec>;
pub type BillingBillCqrsView = Arc<dyn ViewRepository<BillView, Bill>>; pub type BillingBillCqrsView = Arc<dyn ViewRepository<BillView, Bill>>;
pub type WebBillingBillCqrsView = Data<BillingBillCqrsView>; pub type WebBillingBillCqrsView = Data<BillingBillCqrsView>;
pub type BillingLineItemCqrsExec = Arc<PostgresCqrs<LineItem>>; pub type BillingLineItemCqrsExec = Arc<PostgresCqrs<LineItem>>;
pub type WebBillingLineItemCqrsExec = Data<BillingLineItemCqrsExec>;
pub type BillingLineItemCqrsView = Arc<dyn ViewRepository<LineItemView, LineItem>>; pub type BillingLineItemCqrsView = Arc<dyn ViewRepository<LineItemView, LineItem>>;
pub type WebBillingLineItemCqrsView = Data<BillingLineItemCqrsView>; pub type WebBillingLineItemCqrsView = Data<BillingLineItemCqrsView>;
pub type BillingStoreCqrsExec = Arc<PostgresCqrs<Store>>; pub type BillingStoreCqrsExec = Arc<PostgresCqrs<Store>>;
pub type WebBillingStoreCqrsExec = Data<BillingStoreCqrsExec>;
pub type BillingStoreCqrsView = Arc<dyn ViewRepository<StoreView, Store>>; pub type BillingStoreCqrsView = Arc<dyn ViewRepository<StoreView, Store>>;
pub type WebBillingStoreCqrsView = Data<BillingStoreCqrsView>; pub type WebBillingStoreCqrsView = Data<BillingStoreCqrsView>;
pub type WebBillingCqrsExec = Data<Arc<BillingCqrsExec>>;
#[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<BillingError>> {
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(())
}
}