From ac6da029a58df821601149234cfad30225e3c400 Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Fri, 20 Sep 2024 18:09:08 +0530 Subject: [PATCH] feat: inventory: customization ID is provided by caller --- .../services/add_customization_service.rs | 34 +++++------- src/inventory/application/services/errors.rs | 6 +- .../domain/add_customization_command.rs | 55 ++++++++----------- src/inventory/domain/add_store_command.rs | 4 -- 4 files changed, 39 insertions(+), 60 deletions(-) diff --git a/src/inventory/application/services/add_customization_service.rs b/src/inventory/application/services/add_customization_service.rs index 9dcedcf..acf4905 100644 --- a/src/inventory/application/services/add_customization_service.rs +++ b/src/inventory/application/services/add_customization_service.rs @@ -24,7 +24,6 @@ use crate::inventory::{ product_aggregate::*, }, }; -use crate::utils::uuid::*; #[automock] #[async_trait::async_trait] @@ -42,7 +41,6 @@ pub struct AddCustomizationService { db_product_id_exists: ProductIDExistsDBPortObj, db_customization_id_exists: CustomizationIDExistsDBPortObj, db_customization_name_exists_for_product: CustomizationNameExistsForProductDBPortObj, - get_uuid: GetUUIDInterfaceObj, } #[async_trait::async_trait] @@ -59,25 +57,19 @@ impl AddCustomizationUseCase for AddCustomizationService { return Err(InventoryError::ProductIDNotFound); } - let mut customization_id = self.get_uuid.get_uuid(); - loop { - if self - .db_customization_id_exists - .customization_id_exists(&customization_id) - .await? - { - customization_id = self.get_uuid.get_uuid(); - continue; - } else { - break; - } + if self + .db_customization_id_exists + .customization_id_exists(cmd.customization_id()) + .await? + { + return Err(InventoryError::DuplicateCustomizationID); } let customization = CustomizationBuilder::default() .name(cmd.name().into()) .deleted(false) .product_id(*cmd.product_id()) - .customization_id(customization_id) + .customization_id(*cmd.customization_id()) .build() .unwrap(); @@ -101,11 +93,9 @@ pub mod tests { use super::*; use customization_added_event::tests::get_customization_added_event_from_cmd; - use uuid::Uuid; use crate::inventory::domain::add_customization_command::tests::get_command; - use crate::utils::uuid::tests::UUID; - use crate::{tests::bdd::*, utils::uuid::tests::mock_get_uuid}; + use crate::tests::bdd::*; pub fn mock_add_customization_service( times: Option, @@ -138,12 +128,16 @@ pub mod tests { .db_customization_name_exists_for_product( mock_customization_name_exists_for_product_db_port_false(IS_CALLED_ONLY_ONCE), ) - .get_uuid(mock_get_uuid(IS_CALLED_ONLY_ONCE)) .build() .unwrap(); let res = s.add_customization(cmd.clone()).await.unwrap(); assert_eq!(res.customization().name(), cmd.name()); + assert_eq!(res.customization().product_id(), cmd.product_id()); + assert_eq!( + res.customization().customization_id(), + cmd.customization_id() + ); // assert_eq!(customization_added_events.len(), cmd.customizations().len()); } @@ -159,7 +153,6 @@ pub mod tests { .db_customization_name_exists_for_product( mock_customization_name_exists_for_product_db_port_true(IS_CALLED_ONLY_ONCE), ) - .get_uuid(mock_get_uuid(IS_CALLED_ONLY_ONCE)) .build() .unwrap(); @@ -179,7 +172,6 @@ pub mod tests { .db_customization_name_exists_for_product( mock_customization_name_exists_for_product_db_port_false(IS_NEVER_CALLED), ) - .get_uuid(mock_get_uuid(IS_NEVER_CALLED)) .build() .unwrap(); diff --git a/src/inventory/application/services/errors.rs b/src/inventory/application/services/errors.rs index c913f34..8d256f5 100644 --- a/src/inventory/application/services/errors.rs +++ b/src/inventory/application/services/errors.rs @@ -18,6 +18,7 @@ pub enum InventoryError { DuplicateStoreName, DuplicateProductName, DuplicateCustomizationName, + DuplicateCustomizationID, DuplicateStoreID, ProductIDNotFound, CategoryIDNotFound, @@ -42,10 +43,7 @@ impl From for InventoryError { error!("DuplicateCategoryID"); Self::InternalError } - InventoryDBError::DuplicateCustomizationID => { - error!("DuplicateCustomizationID"); - Self::InternalError - } + InventoryDBError::DuplicateCustomizationID => Self::DuplicateCustomizationID, InventoryDBError::InternalError => Self::InternalError, InventoryDBError::ProductIDNotFound => InventoryError::ProductIDNotFound, InventoryDBError::CategoryIDNotFound => InventoryError::CategoryIDNotFound, diff --git a/src/inventory/domain/add_customization_command.rs b/src/inventory/domain/add_customization_command.rs index c79a64e..507412c 100644 --- a/src/inventory/domain/add_customization_command.rs +++ b/src/inventory/domain/add_customization_command.rs @@ -16,28 +16,26 @@ pub enum AddCustomizationCommandError { #[derive( Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Getters, Builder, )] -pub struct UnvalidatedAddCustomizationCommand { - name: String, - product_id: Uuid, -} - -#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Getters)] +#[builder(build_fn(validate = "Self::validate"))] pub struct AddCustomizationCommand { + #[builder(setter(custom))] name: String, product_id: Uuid, + customization_id: Uuid, } -impl UnvalidatedAddCustomizationCommand { - pub fn validate(self) -> Result { - let name = self.name.trim().to_owned(); - if name.is_empty() { - return Err(AddCustomizationCommandError::NameIsEmpty); - } +impl AddCustomizationCommandBuilder { + pub fn name(&mut self, name: String) -> &mut Self { + self.name = Some(name.trim().to_owned()); + self + } - Ok(AddCustomizationCommand { - name, - product_id: self.product_id, - }) + fn validate(&self) -> Result<(), String> { + let name = self.name.as_ref().unwrap().trim().to_owned(); + if name.is_empty() { + return Err(AddCustomizationCommandError::NameIsEmpty.to_string()); + } + Ok(()) } } @@ -48,13 +46,12 @@ pub mod tests { use crate::utils::uuid::tests::UUID; pub fn get_command() -> AddCustomizationCommand { - UnvalidatedAddCustomizationCommandBuilder::default() + AddCustomizationCommandBuilder::default() .name("foo".into()) .product_id(UUID.clone()) + .customization_id(UUID.clone()) .build() .unwrap() - .validate() - .unwrap() } #[test] @@ -62,12 +59,11 @@ pub mod tests { let name = "foo"; let product_id = UUID; - let cmd = UnvalidatedAddCustomizationCommandBuilder::default() + let cmd = AddCustomizationCommandBuilder::default() .name(name.into()) .product_id(product_id.clone()) + .customization_id(UUID.clone()) .build() - .unwrap() - .validate() .unwrap(); assert_eq!(cmd.name(), name); @@ -78,14 +74,11 @@ pub mod tests { fn test_cmd_name_is_empty() { let product_id = UUID; - assert_eq!( - UnvalidatedAddCustomizationCommandBuilder::default() - .name("".into()) - .product_id(product_id.clone()) - .build() - .unwrap() - .validate(), - Err(AddCustomizationCommandError::NameIsEmpty) - ); + assert!(AddCustomizationCommandBuilder::default() + .name("".into()) + .product_id(product_id.clone()) + .customization_id(UUID.clone()) + .build() + .is_err(),); } } diff --git a/src/inventory/domain/add_store_command.rs b/src/inventory/domain/add_store_command.rs index 0ad67f8..68cb21e 100644 --- a/src/inventory/domain/add_store_command.rs +++ b/src/inventory/domain/add_store_command.rs @@ -27,8 +27,6 @@ pub struct AddStoreCommand { } impl AddStoreCommandBuilder { - // pub fn custom_address(&mut self, address: Option) -> &mut Self { - // fn address(&mut self, address: Option) { pub fn address(&mut self, address: Option) -> &mut Self { self.address = if let Some(address) = address { let address = address.trim(); @@ -43,8 +41,6 @@ impl AddStoreCommandBuilder { self } - //pub fn custom_name(&mut self, name: String) -> &mut Self { - //fn name(&mut self, name: String) { pub fn name(&mut self, name: String) -> &mut Self { self.name = Some(name.trim().to_owned()); self