feat: InventoryServices constructor

This commit is contained in:
Aravinth Manivannan 2025-01-10 14:06:06 +05:30
parent 46973ba2e0
commit bf2b3c3440
Signed by: realaravinth
GPG key ID: F8F50389936984FF

View file

@ -1,6 +1,7 @@
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
use std::sync::Arc;
use derive_builder::Builder;
use mockall::predicate::*;
@ -18,58 +19,209 @@ pub mod update_customization_service;
pub mod update_product_service;
pub mod update_store_service;
use add_category_service::*;
use add_customization_service::*;
use add_product_service::*;
use add_store_service::*;
use update_category_service::*;
use update_customization_service::*;
use update_product_service::*;
use update_store_service::*;
use super::port::output::{
db::{
category_id_exists::*, category_name_exists_for_store::*, customization_id_exists::*,
customization_name_exists_for_product::*, get_category::*, product_id_exists::*,
product_name_exists_for_category::*, store_id_exists::*, store_name_exists::*,
},
full_text_search::{add_product_to_store::*, update_product::*},
};
#[automock]
pub trait InventoryServicesInterface: Send + Sync {
fn add_store(&self) -> add_store_service::AddStoreServiceObj;
fn add_category(&self) -> add_category_service::AddCategoryServiceObj;
fn add_product(&self) -> add_product_service::AddProductServiceObj;
fn add_customization(&self) -> add_customization_service::AddCustomizationServiceObj;
fn update_product(&self) -> update_product_service::UpdateProductServiceObj;
fn update_customization(&self) -> update_customization_service::UpdateCustomizationServiceObj;
fn update_category(&self) -> update_category_service::UpdateCategoryServiceObj;
fn update_store(&self) -> update_store_service::UpdateStoreServiceObj;
fn add_store(&self) -> AddStoreServiceObj;
fn add_category(&self) -> AddCategoryServiceObj;
fn add_product(&self) -> AddProductServiceObj;
fn add_customization(&self) -> AddCustomizationServiceObj;
fn update_product(&self) -> UpdateProductServiceObj;
fn update_customization(&self) -> UpdateCustomizationServiceObj;
fn update_category(&self) -> UpdateCategoryServiceObj;
fn update_store(&self) -> UpdateStoreServiceObj;
}
pub type InventoryServicesObj = Arc<dyn InventoryServicesInterface>;
#[derive(Clone, Builder)]
pub struct InventoryServices {
add_store: add_store_service::AddStoreServiceObj,
add_category: add_category_service::AddCategoryServiceObj,
add_product: add_product_service::AddProductServiceObj,
add_customization: add_customization_service::AddCustomizationServiceObj,
update_product: update_product_service::UpdateProductServiceObj,
update_customization: update_customization_service::UpdateCustomizationServiceObj,
update_category: update_category_service::UpdateCategoryServiceObj,
update_store: update_store_service::UpdateStoreServiceObj,
add_store: AddStoreServiceObj,
add_category: AddCategoryServiceObj,
add_product: AddProductServiceObj,
add_customization: AddCustomizationServiceObj,
update_product: UpdateProductServiceObj,
update_customization: UpdateCustomizationServiceObj,
update_category: UpdateCategoryServiceObj,
update_store: UpdateStoreServiceObj,
}
impl InventoryServicesInterface for InventoryServices {
fn add_store(&self) -> add_store_service::AddStoreServiceObj {
fn add_store(&self) -> AddStoreServiceObj {
self.add_store.clone()
}
fn add_category(&self) -> add_category_service::AddCategoryServiceObj {
fn add_category(&self) -> AddCategoryServiceObj {
self.add_category.clone()
}
fn add_product(&self) -> add_product_service::AddProductServiceObj {
fn add_product(&self) -> AddProductServiceObj {
self.add_product.clone()
}
fn add_customization(&self) -> add_customization_service::AddCustomizationServiceObj {
fn add_customization(&self) -> AddCustomizationServiceObj {
self.add_customization.clone()
}
fn update_product(&self) -> update_product_service::UpdateProductServiceObj {
fn update_product(&self) -> UpdateProductServiceObj {
self.update_product.clone()
}
fn update_customization(&self) -> update_customization_service::UpdateCustomizationServiceObj {
fn update_customization(&self) -> UpdateCustomizationServiceObj {
self.update_customization.clone()
}
fn update_category(&self) -> update_category_service::UpdateCategoryServiceObj {
fn update_category(&self) -> UpdateCategoryServiceObj {
self.update_category.clone()
}
fn update_store(&self) -> update_store_service::UpdateStoreServiceObj {
fn update_store(&self) -> UpdateStoreServiceObj {
self.update_store.clone()
}
}
impl InventoryServices {
pub fn new(
out_db_category_id_exists: CategoryIDExistsDBPortObj,
out_db_category_name_exists_for_store: CategoryNameExistsForStoreDBPortObj,
out_db_customization_id_exists: CustomizationIDExistsDBPortObj,
out_db_customization_name_exists_for_product: CustomizationNameExistsForProductDBPortObj,
out_db_get_category: GetCategoryDBPortObj,
out_db_product_id_exists: ProductIDExistsDBPortObj,
out_db_product_name_exists_for_category: ProductNameExistsForCategoryDBPortObj,
out_db_store_id_exists: StoreIDExistsDBPortObj,
out_db_store_name_exists: StoreNameExistsDBPortObj,
out_fts_add_product_to_store: AddProductToStoreFTSPortObj,
// out_fts_update_product: updateproduct
) -> InventoryServicesObj {
let add_store = Arc::new(
AddStoreServiceBuilder::default()
.db_store_id_exists(out_db_store_id_exists.clone())
.db_store_name_exists(out_db_store_name_exists.clone())
.build()
.unwrap(),
);
let add_category = Arc::new(
AddCategoryServiceBuilder::default()
.db_store_id_exists(out_db_store_id_exists.clone())
.db_category_name_exists_for_store(out_db_category_name_exists_for_store.clone())
.db_category_id_exists(out_db_category_id_exists.clone())
.build()
.unwrap(),
);
let add_product = Arc::new(
AddProductServiceBuilder::default()
.db_category_id_exists(out_db_category_id_exists.clone())
.db_product_name_exists_for_category(
out_db_product_name_exists_for_category.clone(),
)
.db_product_id_exists(out_db_product_id_exists.clone())
.db_get_category(out_db_get_category.clone())
.fts_add_product(out_fts_add_product_to_store.clone())
.build()
.unwrap(),
);
let add_customization = Arc::new(
AddCustomizationServiceBuilder::default()
.db_product_id_exists(out_db_product_id_exists.clone())
.db_customization_id_exists(out_db_customization_id_exists.clone())
.db_customization_name_exists_for_product(
out_db_customization_name_exists_for_product.clone(),
)
.build()
.unwrap(),
);
let update_product = Arc::new(
UpdateProductServiceBuilder::default()
.db_category_id_exists(out_db_category_id_exists.clone())
.db_product_name_exists_for_category(
out_db_product_name_exists_for_category.clone(),
)
.db_product_id_exists(out_db_product_id_exists.clone())
.build()
.unwrap(),
);
let update_customization = Arc::new(
UpdateCustomizationServiceBuilder::default()
.db_product_id_exists(out_db_product_id_exists.clone())
.db_customization_id_exists(out_db_customization_id_exists.clone())
.db_customization_name_exists_for_product(
out_db_customization_name_exists_for_product.clone(),
)
.build()
.unwrap(),
);
let update_category = Arc::new(
UpdateCategoryServiceBuilder::default()
.db_store_id_exists(out_db_store_id_exists.clone())
.db_category_name_exists_for_store(out_db_category_name_exists_for_store.clone())
.db_category_id_exists(out_db_category_id_exists.clone())
.build()
.unwrap(),
);
let update_store = Arc::new(
UpdateStoreServiceBuilder::default()
.db_store_id_exists(out_db_store_id_exists.clone())
.db_store_name_exists(out_db_store_name_exists.clone())
.build()
.unwrap(),
);
Arc::new(Self {
add_store,
add_category,
add_product,
add_customization,
update_product,
update_customization,
update_category,
update_store,
})
}
}
#[cfg(test)]
mod tests {
use crate::tests::bdd::IS_NEVER_CALLED;
use super::*;
#[test]
fn inventory_services_work() {
let s = InventoryServices::new(
mock_category_id_exists_db_port_true(IS_NEVER_CALLED),
mock_category_name_exists_for_store_db_port_true(IS_NEVER_CALLED),
mock_customization_id_exists_db_port_true(IS_NEVER_CALLED),
mock_customization_name_exists_for_product_db_port_true(IS_NEVER_CALLED),
mock_get_category_db_port(IS_NEVER_CALLED),
mock_product_id_exists_db_port_true(IS_NEVER_CALLED),
mock_product_name_exists_for_category_db_port_true(IS_NEVER_CALLED),
mock_store_id_exists_db_port_true(IS_NEVER_CALLED),
mock_store_name_exists_db_port_true(IS_NEVER_CALLED),
mock_add_product_to_store_fts_port(IS_NEVER_CALLED),
);
}
}