From cb5bb703e1dbd26445987e5a678d7e7c4249fdd6 Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Tue, 16 Jul 2024 11:50:49 +0530 Subject: [PATCH] chore: clippy lints --- .../db/postgres/create_verification_secret.rs | 2 +- .../db/postgres/delete_verification_secret.rs | 4 +- .../db/postgres/get_verification_secret.rs | 2 +- .../adapters/output/db/postgres/user_view.rs | 2 +- .../db/postgres/verification_secret_exists.rs | 4 +- .../services/mark_user_verified/command.rs | 2 +- .../services/mark_user_verified/service.rs | 4 +- .../services/register_user/service.rs | 6 +- .../resend_verification_email/command.rs | 10 +-- .../resend_verification_email/service.rs | 8 +-- .../services/set_user_admin/command.rs | 4 +- .../services/set_user_admin/service.rs | 2 +- .../services/update_email/command.rs | 8 +-- .../services/update_email/service.rs | 6 +- .../services/update_password/command.rs | 2 +- .../services/update_password/service.rs | 2 +- src/identity/domain/aggregate.rs | 8 +-- .../output/db/postgres/category_view.rs | 17 ++--- .../output/db/postgres/product_id_exists.rs | 6 +- .../product_name_exists_for_category.rs | 6 +- .../output/db/postgres/product_view.rs | 39 +++++------ .../output/db/postgres/store_id_exists.rs | 2 +- .../output/db/postgres/store_name_exists.rs | 2 +- .../adapters/output/db/postgres/store_view.rs | 21 +++--- .../services/add_category_service.rs | 34 ++++------ .../services/add_product_service.rs | 26 ++++---- .../application/services/add_store_service.rs | 24 ++++--- src/inventory/domain/add_category_command.rs | 20 ++---- src/inventory/domain/add_product_command.rs | 20 +++--- src/inventory/domain/add_store_command.rs | 8 +-- src/inventory/domain/category_aggregate.rs | 36 ++++------ src/inventory/domain/product_added_event.rs | 8 +-- src/inventory/domain/product_aggregate.rs | 65 +++++++------------ src/inventory/domain/store_aggregate.rs | 21 +++--- src/settings/database.rs | 2 - src/settings/server.rs | 1 - src/utils/parse_aggregate_id.rs | 10 +-- src/utils/uuid.rs | 4 +- 38 files changed, 187 insertions(+), 261 deletions(-) diff --git a/src/identity/adapters/output/db/postgres/create_verification_secret.rs b/src/identity/adapters/output/db/postgres/create_verification_secret.rs index da8e103..acf9847 100644 --- a/src/identity/adapters/output/db/postgres/create_verification_secret.rs +++ b/src/identity/adapters/output/db/postgres/create_verification_secret.rs @@ -42,7 +42,7 @@ mod tests { let msg = CreateSecretMsgBuilder::default() .secret("secret".into()) - .user_id(UUID.clone()) + .user_id(UUID) .build() .unwrap(); diff --git a/src/identity/adapters/output/db/postgres/delete_verification_secret.rs b/src/identity/adapters/output/db/postgres/delete_verification_secret.rs index 8d150f0..76a69ef 100644 --- a/src/identity/adapters/output/db/postgres/delete_verification_secret.rs +++ b/src/identity/adapters/output/db/postgres/delete_verification_secret.rs @@ -49,7 +49,7 @@ mod tests { .unwrap(), ); let msg = VerifySecretExistsMsgBuilder::default() - .user_id(user_id.clone()) + .user_id(user_id) .secret(secret.into()) .build() .unwrap(); @@ -59,7 +59,7 @@ mod tests { let create_msg = CreateSecretMsgBuilder::default() .secret(secret.into()) - .user_id(user_id.clone()) + .user_id(user_id) .build() .unwrap(); db.create_verification_secret(create_msg).await.unwrap(); diff --git a/src/identity/adapters/output/db/postgres/get_verification_secret.rs b/src/identity/adapters/output/db/postgres/get_verification_secret.rs index e97e7aa..3b70782 100644 --- a/src/identity/adapters/output/db/postgres/get_verification_secret.rs +++ b/src/identity/adapters/output/db/postgres/get_verification_secret.rs @@ -60,7 +60,7 @@ mod tests { let create_msg = CreateSecretMsgBuilder::default() .secret(secret.into()) - .user_id(user_id.clone()) + .user_id(user_id) .build() .unwrap(); diff --git a/src/identity/adapters/output/db/postgres/user_view.rs b/src/identity/adapters/output/db/postgres/user_view.rs index a105f00..0f27705 100644 --- a/src/identity/adapters/output/db/postgres/user_view.rs +++ b/src/identity/adapters/output/db/postgres/user_view.rs @@ -45,7 +45,7 @@ impl View for UserView { self.deleted = false; self.first_name = val.first_name().into(); self.last_name = val.last_name().into(); - self.user_id = val.user_id().clone(); + self.user_id = *val.user_id(); } UserEvent::UserDeleted => self.deleted = true, UserEvent::Loggedin(_) => (), diff --git a/src/identity/adapters/output/db/postgres/verification_secret_exists.rs b/src/identity/adapters/output/db/postgres/verification_secret_exists.rs index aa1fe87..ab9a64f 100644 --- a/src/identity/adapters/output/db/postgres/verification_secret_exists.rs +++ b/src/identity/adapters/output/db/postgres/verification_secret_exists.rs @@ -56,7 +56,7 @@ mod tests { .unwrap(), ); let msg = VerifySecretExistsMsgBuilder::default() - .user_id(user_id.clone()) + .user_id(user_id) .secret(secret.into()) .build() .unwrap(); @@ -66,7 +66,7 @@ mod tests { let create_msg = CreateSecretMsgBuilder::default() .secret(secret.into()) - .user_id(user_id.clone()) + .user_id(user_id) .build() .unwrap(); db.create_verification_secret(create_msg).await.unwrap(); diff --git a/src/identity/application/services/mark_user_verified/command.rs b/src/identity/application/services/mark_user_verified/command.rs index 84138df..5179ab8 100644 --- a/src/identity/application/services/mark_user_verified/command.rs +++ b/src/identity/application/services/mark_user_verified/command.rs @@ -29,7 +29,7 @@ mod tests { fn test_cmd() { let user_id = UUID; let secret = "asdfasdf"; - let cmd = MarkUserVerifiedCommand::new(user_id.clone(), secret.into()).unwrap(); + let cmd = MarkUserVerifiedCommand::new(user_id, secret.into()).unwrap(); assert_eq!(cmd.user_id(), &user_id); assert_eq!(cmd.secret(), secret); } diff --git a/src/identity/application/services/mark_user_verified/service.rs b/src/identity/application/services/mark_user_verified/service.rs index 083392e..885d184 100644 --- a/src/identity/application/services/mark_user_verified/service.rs +++ b/src/identity/application/services/mark_user_verified/service.rs @@ -22,7 +22,7 @@ impl MarkUserVerifiedUseCase for MarkUserVerifiedService { cmd: command::MarkUserVerifiedCommand, ) -> IdentityResult<()> { let msg = VerifySecretExistsMsgBuilder::default() - .user_id(cmd.user_id().clone()) + .user_id(*cmd.user_id()) .secret(cmd.secret().into()) .build() .unwrap(); @@ -54,7 +54,7 @@ mod tests { async fn test_service() { let user_id = UUID; let secret = "password"; - let cmd = command::MarkUserVerifiedCommand::new(user_id.clone(), secret.into()).unwrap(); + let cmd = command::MarkUserVerifiedCommand::new(user_id, secret.into()).unwrap(); // happy case { diff --git a/src/identity/application/services/register_user/service.rs b/src/identity/application/services/register_user/service.rs index d13995e..3acb971 100644 --- a/src/identity/application/services/register_user/service.rs +++ b/src/identity/application/services/register_user/service.rs @@ -60,7 +60,7 @@ impl RegisterUserUseCase for RegisterUserService { .create_verification_secret( CreateSecretMsgBuilder::default() .secret(secret.clone()) - .user_id(user_id.clone()) + .user_id(user_id) .build() .unwrap(), ) @@ -103,7 +103,7 @@ mod tests { let cmd = command::UnvalidatedRegisterUserCommandBuilder::default() .first_name(username.into()) .last_name(username.into()) - .email(email.into()) + .email(email) .password(password.into()) .confirm_password(password.into()) .build() @@ -151,7 +151,7 @@ mod tests { let cmd = command::UnvalidatedRegisterUserCommandBuilder::default() .first_name(username.into()) .last_name(username.into()) - .email(email.into()) + .email(email) .password(password.into()) .confirm_password(password.into()) .build() diff --git a/src/identity/application/services/resend_verification_email/command.rs b/src/identity/application/services/resend_verification_email/command.rs index 67ee18b..bf1f0e3 100644 --- a/src/identity/application/services/resend_verification_email/command.rs +++ b/src/identity/application/services/resend_verification_email/command.rs @@ -41,7 +41,7 @@ mod tests { fn test_cmd() { let config = argon2_creds::Config::default(); ResendVerificationEmailCommand::new( - UUID.clone(), + UUID, "john".into(), "realaravinth@example.com".into(), &config, @@ -49,13 +49,7 @@ mod tests { .unwrap(); assert_eq!( - ResendVerificationEmailCommand::new( - UUID.clone(), - "john".into(), - "john".into(), - &config - ) - .err(), + ResendVerificationEmailCommand::new(UUID, "john".into(), "john".into(), &config).err(), Some(IdentityCommandError::BadEmail) ); } diff --git a/src/identity/application/services/resend_verification_email/service.rs b/src/identity/application/services/resend_verification_email/service.rs index 38b8580..57f9259 100644 --- a/src/identity/application/services/resend_verification_email/service.rs +++ b/src/identity/application/services/resend_verification_email/service.rs @@ -57,11 +57,11 @@ mod tests { #[actix_rt::test] async fn test_service() { let user_id = UUID; - let email = format!("john@example.com"); + let email = "john@example.com".to_string(); let secret = "asdfasdf"; let config = argon2_creds::Config::default(); let cmd = command::ResendVerificationEmailCommand::new( - UUID.clone(), + UUID, "john".into(), email.clone(), &config, @@ -88,11 +88,11 @@ mod tests { #[actix_rt::test] async fn test_service_email_exists() { let user_id = UUID; - let email = format!("john@example.com"); + let email = "john@example.com".to_string(); let secret = "asdfasdf"; let config = argon2_creds::Config::default(); let cmd = command::ResendVerificationEmailCommand::new( - UUID.clone(), + UUID, "john".into(), email.clone(), &config, diff --git a/src/identity/application/services/set_user_admin/command.rs b/src/identity/application/services/set_user_admin/command.rs index f2feb19..778f9f5 100644 --- a/src/identity/application/services/set_user_admin/command.rs +++ b/src/identity/application/services/set_user_admin/command.rs @@ -44,7 +44,7 @@ mod tests { .email_verified(false) .is_admin(true) .deleted(false) - .user_id(UUID.clone()) + .user_id(UUID) .build() .unwrap(), ) @@ -61,7 +61,7 @@ mod tests { .is_admin(false) .email_verified(false) .deleted(false) - .user_id(UUID.clone()) + .user_id(UUID) .build() .unwrap(), ) diff --git a/src/identity/application/services/set_user_admin/service.rs b/src/identity/application/services/set_user_admin/service.rs index da9b8f5..a6e34b0 100644 --- a/src/identity/application/services/set_user_admin/service.rs +++ b/src/identity/application/services/set_user_admin/service.rs @@ -38,7 +38,7 @@ mod tests { .email_verified(false) .is_admin(true) .deleted(false) - .user_id(UUID.clone()) + .user_id(UUID) .build() .unwrap(); diff --git a/src/identity/application/services/update_email/command.rs b/src/identity/application/services/update_email/command.rs index 8e56799..e5a911e 100644 --- a/src/identity/application/services/update_email/command.rs +++ b/src/identity/application/services/update_email/command.rs @@ -49,12 +49,12 @@ mod tests { let password = "adsfasdfasd"; let first_name = "john"; let user_id = UUID; - let new_email = format!("newemail@example.com"); + let new_email = "newemail@example.com".to_string(); let hashed_password = config.password(password).unwrap(); assert_eq!( UpdateEmailCommand::new( new_email.clone(), - user_id.clone(), + user_id, first_name.into(), password.into(), &hashed_password, @@ -69,7 +69,7 @@ mod tests { assert_eq!( UpdateEmailCommand::new( user_id.to_string(), - user_id.clone(), + user_id, first_name.into(), password.into(), &hashed_password, @@ -83,7 +83,7 @@ mod tests { assert_eq!( UpdateEmailCommand::new( new_email.to_string(), - user_id.clone(), + user_id, first_name.into(), first_name.into(), &hashed_password, diff --git a/src/identity/application/services/update_email/service.rs b/src/identity/application/services/update_email/service.rs index ce7f621..c6089b8 100644 --- a/src/identity/application/services/update_email/service.rs +++ b/src/identity/application/services/update_email/service.rs @@ -44,7 +44,7 @@ impl UpdateEmailUseCase for UpdateEmailService { .create_verification_secret( CreateSecretMsgBuilder::default() .secret(secret.clone()) - .user_id(cmd.user_id().clone()) + .user_id(*cmd.user_id()) .build() .unwrap(), ) @@ -71,14 +71,14 @@ mod tests { #[actix_rt::test] async fn test_service() { let user_id = UUID; - let new_email = format!("john@example.com"); + let new_email = "john@example.com".to_string(); let password = "password"; let config = argon2_creds::Config::default(); let hashed_password = config.password(password).unwrap(); let cmd = command::UpdateEmailCommand::new( new_email.clone(), - user_id.clone(), + user_id, "john".into(), password.into(), &hashed_password, diff --git a/src/identity/application/services/update_password/command.rs b/src/identity/application/services/update_password/command.rs index f262050..5212806 100644 --- a/src/identity/application/services/update_password/command.rs +++ b/src/identity/application/services/update_password/command.rs @@ -55,7 +55,7 @@ mod tests { ) .unwrap() .hashed_new_passowrd, - new_password.into(), + new_password, ) .unwrap(); diff --git a/src/identity/application/services/update_password/service.rs b/src/identity/application/services/update_password/service.rs index 11714d4..1730c12 100644 --- a/src/identity/application/services/update_password/service.rs +++ b/src/identity/application/services/update_password/service.rs @@ -33,7 +33,7 @@ mod tests { username.into(), password.into(), new_password.clone(), - new_password.into(), + new_password, &hashed_password, &config, ) diff --git a/src/identity/domain/aggregate.rs b/src/identity/domain/aggregate.rs index 4e77a69..f5b1aaa 100644 --- a/src/identity/domain/aggregate.rs +++ b/src/identity/domain/aggregate.rs @@ -142,12 +142,12 @@ impl Aggregate for User { UserEvent::UserRegistered(e) => { self.first_name = e.first_name().into(); self.last_name = e.last_name().into(); - self.user_id = e.user_id().clone(); + self.user_id = *e.user_id(); self.email = e.email().into(); self.hashed_password = e.hashed_password().into(); - self.is_admin = e.is_admin().clone(); - self.email_verified = e.email_verified().clone(); - self.is_verified = e.is_verified().clone(); + self.is_admin = *e.is_admin(); + self.email_verified = *e.email_verified(); + self.is_verified = *e.is_verified(); self.deleted = false; } UserEvent::UserDeleted => { diff --git a/src/inventory/adapters/output/db/postgres/category_view.rs b/src/inventory/adapters/output/db/postgres/category_view.rs index 61f270f..ca135c8 100644 --- a/src/inventory/adapters/output/db/postgres/category_view.rs +++ b/src/inventory/adapters/output/db/postgres/category_view.rs @@ -32,15 +32,12 @@ pub struct CategoryView { // design the events to carry the balance information instead. impl View for CategoryView { fn update(&mut self, event: &EventEnvelope) { - match &event.payload { - InventoryEvent::CategoryAdded(val) => { - self.name = val.name().into(); - self.description = val.description().clone(); - self.category_id = val.category_id().clone(); - self.store_id = val.store_id().clone(); - self.deleted = false; - } - _ => (), + if let InventoryEvent::CategoryAdded(val) = &event.payload { + self.name = val.name().into(); + self.description = val.description().clone(); + self.category_id = *val.category_id(); + self.store_id = *val.store_id(); + self.deleted = false; } } } @@ -187,7 +184,7 @@ impl Query for SimpleLoggingQuery { impl Query for InventoryDBPostgresAdapter { async fn dispatch(&self, category_id: &str, events: &[EventEnvelope]) { let res = self - .load_with_context(&category_id) + .load_with_context(category_id) .await .unwrap_or_else(|_| { Some(( diff --git a/src/inventory/adapters/output/db/postgres/product_id_exists.rs b/src/inventory/adapters/output/db/postgres/product_id_exists.rs index 8f52440..f21e8d5 100644 --- a/src/inventory/adapters/output/db/postgres/product_id_exists.rs +++ b/src/inventory/adapters/output/db/postgres/product_id_exists.rs @@ -63,10 +63,10 @@ pub mod tests { .name(cmd.name().into()) .description(cmd.description().as_ref().map(|s| s.to_string())) .image(cmd.image().as_ref().map(|s| s.to_string())) - .sku_able(cmd.sku_able().clone()) - .category_id(cmd.category_id().clone()) + .sku_able(*cmd.sku_able()) + .category_id(*cmd.category_id()) .quantity(cmd.quantity().clone()) - .product_id(UUID.clone()) + .product_id(UUID) .customizations(customizations) .price(cmd.price().clone()) .build() diff --git a/src/inventory/adapters/output/db/postgres/product_name_exists_for_category.rs b/src/inventory/adapters/output/db/postgres/product_name_exists_for_category.rs index 23f0e7d..dd233e1 100644 --- a/src/inventory/adapters/output/db/postgres/product_name_exists_for_category.rs +++ b/src/inventory/adapters/output/db/postgres/product_name_exists_for_category.rs @@ -60,10 +60,10 @@ mod tests { .name(product_name.into()) .description(cmd.description().as_ref().map(|s| s.to_string())) .image(cmd.image().as_ref().map(|s| s.to_string())) - .sku_able(cmd.sku_able().clone()) - .category_id(cmd.category_id().clone()) + .sku_able(*cmd.sku_able()) + .category_id(*cmd.category_id()) .customizations(Vec::default()) - .product_id(UUID.clone()) + .product_id(UUID) .price(cmd.price().clone()) .quantity(cmd.quantity().clone()) .build() diff --git a/src/inventory/adapters/output/db/postgres/product_view.rs b/src/inventory/adapters/output/db/postgres/product_view.rs index 916164b..958fb4e 100644 --- a/src/inventory/adapters/output/db/postgres/product_view.rs +++ b/src/inventory/adapters/output/db/postgres/product_view.rs @@ -10,8 +10,8 @@ use cqrs_es::{EventEnvelope, Query, View}; use serde::{Deserialize, Serialize}; use uuid::Uuid; +use super::errors::*; use super::InventoryDBPostgresAdapter; -use super::{errors::*, product_id_exists}; use crate::inventory::domain::events::InventoryEvent; use crate::inventory::domain::product_aggregate::{ Currency, Customization, CustomizationBuilder, PriceBuilder, Product, ProductBuilder, @@ -122,27 +122,24 @@ impl From for Product { // design the events to carry the balance information instead. impl View for ProductView { fn update(&mut self, event: &EventEnvelope) { - match &event.payload { - InventoryEvent::ProductAdded(val) => { - self.name = val.name().into(); - self.description = val.description().clone(); - self.image = val.image().clone(); - self.product_id = val.product_id().clone(); - self.category_id = val.category_id().clone(); + if let InventoryEvent::ProductAdded(val) = &event.payload { + self.name = val.name().into(); + self.description = val.description().clone(); + self.image = val.image().clone(); + self.product_id = *val.product_id(); + self.category_id = *val.category_id(); - self.sku_able = val.sku_able().clone(); + self.sku_able = *val.sku_able(); - self.price_minor = val.price().minor().clone() as i32; - self.price_major = val.price().major().clone() as i32; - self.price_currency = val.price().currency().to_string(); + self.price_minor = *val.price().minor() as i32; + self.price_major = *val.price().major() as i32; + self.price_currency = val.price().currency().to_string(); - self.quantity_number = val.quantity().number().clone() as i32; - self.quantity_unit = val.quantity().unit().to_string(); + self.quantity_number = *val.quantity().number() as i32; + self.quantity_unit = val.quantity().unit().to_string(); - self.deleted = false; - self.customizations = val.customizations().clone(); - } - _ => (), + self.deleted = false; + self.customizations = val.customizations().clone(); } } } @@ -234,7 +231,7 @@ impl ViewRepository for InventoryDBPostgresAdapter { .await .map_err(PostgresAggregateError::from)?; - let customizations = res.get_customizations(&self).await?; + let customizations = res.get_customizations(self).await?; Ok(Some(res.to_product_view(customizations))) } @@ -273,7 +270,7 @@ impl ViewRepository for InventoryDBPostgresAdapter { .await .map_err(PostgresAggregateError::from)?; - let customizations = res.get_customizations(&self).await?; + let customizations = res.get_customizations(self).await?; struct Context { version: i64, @@ -436,7 +433,7 @@ impl ViewRepository for InventoryDBPostgresAdapter { impl Query for InventoryDBPostgresAdapter { async fn dispatch(&self, product_id: &str, events: &[EventEnvelope]) { let res = self - .load_with_context(&product_id) + .load_with_context(product_id) .await .unwrap_or_else(|_| { Some(( diff --git a/src/inventory/adapters/output/db/postgres/store_id_exists.rs b/src/inventory/adapters/output/db/postgres/store_id_exists.rs index 11d8571..64cb992 100644 --- a/src/inventory/adapters/output/db/postgres/store_id_exists.rs +++ b/src/inventory/adapters/output/db/postgres/store_id_exists.rs @@ -66,7 +66,7 @@ pub mod tests { let store = StoreBuilder::default() .name("store_name".into()) - .owner(UUID.clone()) + .owner(UUID) .address(Some("store_address".into())) .store_id(store_id) .build() diff --git a/src/inventory/adapters/output/db/postgres/store_name_exists.rs b/src/inventory/adapters/output/db/postgres/store_name_exists.rs index 81da3be..dbcf114 100644 --- a/src/inventory/adapters/output/db/postgres/store_name_exists.rs +++ b/src/inventory/adapters/output/db/postgres/store_name_exists.rs @@ -52,7 +52,7 @@ mod tests { let store = StoreBuilder::default() .name("store_name".into()) - .owner(UUID.clone()) + .owner(UUID) .address(Some("store_address".into())) .store_id(store_id) .build() diff --git a/src/inventory/adapters/output/db/postgres/store_view.rs b/src/inventory/adapters/output/db/postgres/store_view.rs index 664af17..ac8d6e1 100644 --- a/src/inventory/adapters/output/db/postgres/store_view.rs +++ b/src/inventory/adapters/output/db/postgres/store_view.rs @@ -32,15 +32,12 @@ pub struct StoreView { // design the events to carry the balance information instead. impl View for StoreView { fn update(&mut self, event: &EventEnvelope) { - match &event.payload { - InventoryEvent::StoreAdded(val) => { - self.name = val.name().into(); - self.address = val.address().clone(); - self.store_id = val.store_id().clone(); - self.owner = val.owner().clone(); - self.deleted = false; - } - _ => (), + if let InventoryEvent::StoreAdded(val) = &event.payload { + self.name = val.name().into(); + self.address = val.address().clone(); + self.store_id = *val.store_id(); + self.owner = *val.owner(); + self.deleted = false; } } } @@ -187,7 +184,7 @@ impl Query for SimpleLoggingQuery { impl Query for InventoryDBPostgresAdapter { async fn dispatch(&self, store_id: &str, events: &[EventEnvelope]) { let res = self - .load_with_context(&store_id) + .load_with_context(store_id) .await .unwrap_or_else(|_| Some((StoreView::default(), ViewContext::new(store_id.into(), 0)))); let (mut view, view_context): (StoreView, ViewContext) = res.unwrap(); @@ -254,7 +251,7 @@ mod tests { )) .add_category(mock_add_category_service( IS_NEVER_CALLED, - AddCategoryCommand::new("foo".into(), None, UUID.clone(), UUID.clone()).unwrap(), + AddCategoryCommand::new("foo".into(), None, UUID, UUID).unwrap(), )) .add_product(mock_add_product_service(IS_NEVER_CALLED, get_command())) .build() @@ -273,7 +270,7 @@ mod tests { ); let rand = crate::utils::random_string::GenerateRandomString {}; - let cmd = AddStoreCommand::new(rand.get_random(10), None, UUID.clone()).unwrap(); + let cmd = AddStoreCommand::new(rand.get_random(10), None, UUID).unwrap(); cqrs.execute("", InventoryCommand::AddStore(cmd.clone())) .await .unwrap(); diff --git a/src/inventory/application/services/add_category_service.rs b/src/inventory/application/services/add_category_service.rs index 2cf7bd0..5b0e9ef 100644 --- a/src/inventory/application/services/add_category_service.rs +++ b/src/inventory/application/services/add_category_service.rs @@ -41,7 +41,7 @@ impl AddCategoryUseCase for AddCategoryService { let mut category = CategoryBuilder::default() .name(cmd.name().into()) .description(cmd.description().as_ref().map(|s| s.to_string())) - .store_id(cmd.store_id().clone()) + .store_id(*cmd.store_id()) .category_id(category_id) .build() .unwrap(); @@ -64,7 +64,7 @@ impl AddCategoryUseCase for AddCategoryService { category = CategoryBuilder::default() .name(cmd.name().into()) .description(cmd.description().as_ref().map(|s| s.to_string())) - .store_id(cmd.store_id().clone()) + .store_id(*cmd.store_id()) .category_id(category_id) .build() .unwrap(); @@ -78,9 +78,9 @@ impl AddCategoryUseCase for AddCategoryService { Ok(CategoryAddedEventBuilder::default() .name(category.name().into()) .description(category.description().as_ref().map(|s| s.to_string())) - .added_by_user(cmd.adding_by().clone()) - .store_id(category.store_id().clone()) - .category_id(category.category_id().clone()) + .added_by_user(*cmd.adding_by()) + .store_id(*category.store_id()) + .category_id(*category.category_id()) .build() .unwrap()) } @@ -104,9 +104,9 @@ pub mod tests { let res = CategoryAddedEventBuilder::default() .name(cmd.name().into()) .description(cmd.description().as_ref().map(|s| s.to_string())) - .added_by_user(cmd.adding_by().clone()) - .store_id(cmd.store_id().clone()) - .category_id(UUID.clone()) + .added_by_user(*cmd.adding_by()) + .store_id(*cmd.store_id()) + .category_id(UUID) .build() .unwrap(); @@ -129,13 +129,8 @@ pub mod tests { let store_id = Uuid::new_v4(); // description = None - let cmd = AddCategoryCommand::new( - name.into(), - Some(description.into()), - store_id.clone(), - user_id.clone(), - ) - .unwrap(); + let cmd = AddCategoryCommand::new(name.into(), Some(description.into()), store_id, user_id) + .unwrap(); let s = AddCategoryServiceBuilder::default() .db_category_name_exists_for_store(mock_category_name_exists_for_store_db_port_false( @@ -162,13 +157,8 @@ pub mod tests { let store_id = Uuid::new_v4(); // description = None - let cmd = AddCategoryCommand::new( - name.into(), - Some(description.into()), - store_id.clone(), - user_id.clone(), - ) - .unwrap(); + let cmd = AddCategoryCommand::new(name.into(), Some(description.into()), store_id, user_id) + .unwrap(); let s = AddCategoryServiceBuilder::default() .db_category_name_exists_for_store(mock_category_name_exists_for_store_db_port_true( diff --git a/src/inventory/application/services/add_product_service.rs b/src/inventory/application/services/add_product_service.rs index b39e9bd..da23430 100644 --- a/src/inventory/application/services/add_product_service.rs +++ b/src/inventory/application/services/add_product_service.rs @@ -11,9 +11,7 @@ use mockall::*; use super::errors::*; use crate::inventory::{ application::port::output::db::{ - customization_id_exists::{self, *}, - customization_name_exists_for_product::*, - product_id_exists::*, + customization_id_exists::*, customization_name_exists_for_product::*, product_id_exists::*, product_name_exists_for_category::*, }, domain::{ @@ -97,9 +95,9 @@ impl AddProductUseCase for AddProductService { .name(cmd.name().into()) .description(cmd.description().as_ref().map(|s| s.to_string())) .image(cmd.image().clone()) - .sku_able(cmd.sku_able().clone()) + .sku_able(*cmd.sku_able()) .price(cmd.price().clone()) - .category_id(cmd.category_id().clone()) + .category_id(*cmd.category_id()) .quantity(cmd.quantity().clone()) .customizations(customizations) .product_id(product_id) @@ -115,14 +113,14 @@ impl AddProductUseCase for AddProductService { } Ok(ProductAddedEventBuilder::default() - .added_by_user(cmd.adding_by().clone()) + .added_by_user(*cmd.adding_by()) .name(product.name().into()) .description(product.description().as_ref().map(|s| s.to_string())) .image(product.image().clone()) - .sku_able(product.sku_able().clone()) + .sku_able(*product.sku_able()) .price(product.price().clone()) - .category_id(product.category_id().clone()) - .product_id(product.product_id().clone()) + .category_id(*product.category_id()) + .product_id(*product.product_id()) .customizations(product.customizations().clone()) .quantity(product.quantity().clone()) .build() @@ -134,8 +132,6 @@ impl AddProductUseCase for AddProductService { pub mod tests { use super::*; - use uuid::Uuid; - use crate::inventory::domain::add_product_command::tests::get_command; use crate::utils::uuid::tests::UUID; use crate::{tests::bdd::*, utils::uuid::tests::mock_get_uuid}; @@ -162,13 +158,13 @@ pub mod tests { .name(cmd.name().into()) .description(cmd.description().as_ref().map(|s| s.to_string())) .image(cmd.image().as_ref().map(|s| s.to_string())) - .sku_able(cmd.sku_able().clone()) - .category_id(cmd.category_id().clone()) - .product_id(UUID.clone()) + .sku_able(*cmd.sku_able()) + .category_id(*cmd.category_id()) + .product_id(UUID) .price(cmd.price().clone()) .quantity(cmd.quantity().clone()) .customizations(customizations) - .added_by_user(cmd.adding_by().clone()) + .added_by_user(*cmd.adding_by()) .build() .unwrap(); diff --git a/src/inventory/application/services/add_store_service.rs b/src/inventory/application/services/add_store_service.rs index 6c0be72..75d94d8 100644 --- a/src/inventory/application/services/add_store_service.rs +++ b/src/inventory/application/services/add_store_service.rs @@ -10,9 +10,7 @@ use mockall::*; use super::errors::*; use crate::inventory::{ - application::port::output::db::{ - errors::InventoryDBError, store_id_exists::*, store_name_exists::*, - }, + application::port::output::db::{store_id_exists::*, store_name_exists::*}, domain::{ add_store_command::AddStoreCommand, store_added_event::{StoreAddedEvent, StoreAddedEventBuilder}, @@ -43,8 +41,8 @@ impl AddStoreUseCase for AddStoreService { let mut store = StoreBuilder::default() .name(cmd.name().into()) .address(cmd.address().as_ref().map(|s| s.to_string())) - .owner(cmd.owner().clone()) - .store_id(store_id.clone()) + .owner(*cmd.owner()) + .store_id(store_id) .build() .unwrap(); @@ -58,8 +56,8 @@ impl AddStoreUseCase for AddStoreService { store = StoreBuilder::default() .name(cmd.name().into()) .address(cmd.address().as_ref().map(|s| s.to_string())) - .owner(cmd.owner().clone()) - .store_id(store_id.clone()) + .owner(*cmd.owner()) + .store_id(store_id) .build() .unwrap(); continue; @@ -71,8 +69,8 @@ impl AddStoreUseCase for AddStoreService { Ok(StoreAddedEventBuilder::default() .name(store.name().into()) .address(store.address().as_ref().map(|s| s.to_string())) - .owner(cmd.owner().clone()) - .store_id(store_id.clone()) + .owner(*cmd.owner()) + .store_id(store_id) .build() .unwrap()) } @@ -94,8 +92,8 @@ pub mod tests { let res = StoreAddedEventBuilder::default() .name(cmd.name().into()) .address(cmd.address().as_ref().map(|s| s.to_string())) - .owner(cmd.owner().clone()) - .store_id(UUID.clone()) + .owner(*cmd.owner()) + .store_id(UUID) .build() .unwrap(); @@ -117,7 +115,7 @@ pub mod tests { let owner = UUID; // address = None - let cmd = AddStoreCommand::new(name.into(), Some(address.into()), owner.clone()).unwrap(); + let cmd = AddStoreCommand::new(name.into(), Some(address.into()), owner).unwrap(); let s = AddStoreServiceBuilder::default() .db_store_id_exists(mock_store_id_exists_db_port_false(IS_CALLED_ONLY_ONCE)) @@ -140,7 +138,7 @@ pub mod tests { let owner = UUID; // address = None - let cmd = AddStoreCommand::new(name.into(), Some(address.into()), owner.clone()).unwrap(); + let cmd = AddStoreCommand::new(name.into(), Some(address.into()), owner).unwrap(); let s = AddStoreServiceBuilder::default() .db_store_id_exists(mock_store_id_exists_db_port_false(IS_NEVER_CALLED)) diff --git a/src/inventory/domain/add_category_command.rs b/src/inventory/domain/add_category_command.rs index e265c54..9c8d0eb 100644 --- a/src/inventory/domain/add_category_command.rs +++ b/src/inventory/domain/add_category_command.rs @@ -66,21 +66,16 @@ mod tests { let store_id = Uuid::new_v4(); // description = None - let cmd = AddCategoryCommand::new(name.into(), None, store_id.clone(), adding_by.clone()) - .unwrap(); + let cmd = AddCategoryCommand::new(name.into(), None, store_id, adding_by).unwrap(); assert_eq!(cmd.name(), name); assert_eq!(cmd.description(), &None); assert_eq!(cmd.adding_by(), &adding_by); assert_eq!(cmd.store_id(), &store_id); // description = Some - let cmd = AddCategoryCommand::new( - name.into(), - Some(description.into()), - store_id.clone(), - adding_by.clone(), - ) - .unwrap(); + let cmd = + AddCategoryCommand::new(name.into(), Some(description.into()), store_id, adding_by) + .unwrap(); assert_eq!(cmd.name(), name); assert_eq!(cmd.description(), &Some(description.to_owned())); assert_eq!(cmd.adding_by(), &adding_by); @@ -88,12 +83,7 @@ mod tests { // AddCategoryCommandError::NameIsEmpty assert_eq!( - AddCategoryCommand::new( - "".into(), - Some(description.into()), - store_id.clone(), - adding_by.clone(), - ), + AddCategoryCommand::new("".into(), Some(description.into()), store_id, adding_by,), Err(AddCategoryCommandError::NameIsEmpty) ) } diff --git a/src/inventory/domain/add_product_command.rs b/src/inventory/domain/add_product_command.rs index a32f5e5..5b0d22c 100644 --- a/src/inventory/domain/add_product_command.rs +++ b/src/inventory/domain/add_product_command.rs @@ -181,8 +181,8 @@ pub mod tests { .name(name.into()) .description(description.clone()) .image(image.clone()) - .category_id(category_id.clone()) - .adding_by(adding_by.clone()) + .category_id(category_id) + .adding_by(adding_by) .quantity(quantity) .sku_able(sku_able) .price(price.clone()) @@ -218,8 +218,8 @@ pub mod tests { .name(name.into()) .description(None) .image(None) - .category_id(category_id.clone()) - .adding_by(adding_by.clone()) + .category_id(category_id) + .adding_by(adding_by) .quantity(quantity.clone()) .sku_able(sku_able) .price(price.clone()) @@ -265,9 +265,9 @@ pub mod tests { .name(name.into()) .description(description.clone()) .image(image.clone()) - .category_id(category_id.clone()) + .category_id(category_id) .quantity(quantity.clone()) - .adding_by(adding_by.clone()) + .adding_by(adding_by) .sku_able(sku_able) .price(price.clone()) .customizations(customizations.clone()) @@ -312,8 +312,8 @@ pub mod tests { .name("".into()) .description(description.clone()) .image(image.clone()) - .category_id(category_id.clone()) - .adding_by(adding_by.clone()) + .category_id(category_id) + .adding_by(adding_by) .quantity(quantity) .sku_able(sku_able) .customizations(customizations) @@ -365,8 +365,8 @@ pub mod tests { .name(name.into()) .description(description.clone()) .image(image.clone()) - .category_id(category_id.clone()) - .adding_by(adding_by.clone()) + .category_id(category_id) + .adding_by(adding_by) .quantity(quantity) .sku_able(sku_able) .customizations(customizations) diff --git a/src/inventory/domain/add_store_command.rs b/src/inventory/domain/add_store_command.rs index 3277402..3f11af8 100644 --- a/src/inventory/domain/add_store_command.rs +++ b/src/inventory/domain/add_store_command.rs @@ -59,23 +59,23 @@ mod tests { fn test_cmd() { let name = "foo"; let address = "bar"; - let owner = UUID.clone(); + let owner = UUID; // address = None - let cmd = AddStoreCommand::new(name.into(), None, owner.clone()).unwrap(); + let cmd = AddStoreCommand::new(name.into(), None, owner).unwrap(); assert_eq!(cmd.name(), name); assert_eq!(cmd.address(), &None); assert_eq!(cmd.owner(), &owner); // address = Some - let cmd = AddStoreCommand::new(name.into(), Some(address.into()), owner.clone()).unwrap(); + let cmd = AddStoreCommand::new(name.into(), Some(address.into()), owner).unwrap(); assert_eq!(cmd.name(), name); assert_eq!(cmd.address(), &Some(address.to_owned())); assert_eq!(cmd.owner(), &owner); // AddStoreCommandError::NameIsEmpty assert_eq!( - AddStoreCommand::new("".into(), Some(address.into()), owner.clone()), + AddStoreCommand::new("".into(), Some(address.into()), owner), Err(AddStoreCommandError::NameIsEmpty) ) } diff --git a/src/inventory/domain/category_aggregate.rs b/src/inventory/domain/category_aggregate.rs index 440ded3..df286bd 100644 --- a/src/inventory/domain/category_aggregate.rs +++ b/src/inventory/domain/category_aggregate.rs @@ -55,17 +55,14 @@ impl Aggregate for Category { } fn apply(&mut self, event: Self::Event) { - match event { - InventoryEvent::CategoryAdded(e) => { - *self = CategoryBuilder::default() - .name(e.name().into()) - .category_id(e.category_id().clone()) - .description(e.description().clone()) - .store_id(e.store_id().clone()) - .build() - .unwrap(); - } - _ => (), + if let InventoryEvent::CategoryAdded(e) = event { + *self = CategoryBuilder::default() + .name(e.name().into()) + .category_id(*e.category_id()) + .description(e.description().clone()) + .store_id(*e.store_id()) + .build() + .unwrap(); } } } @@ -96,22 +93,17 @@ mod aggregate_tests { let description = Some("category_description".to_string()); let adding_by = UUID; let store_id = Uuid::new_v4(); - let category_id = UUID.clone(); + let category_id = UUID; - let cmd = AddCategoryCommand::new( - name.into(), - description.clone(), - store_id.clone(), - adding_by.clone(), - ) - .unwrap(); + let cmd = + AddCategoryCommand::new(name.into(), description.clone(), store_id, adding_by).unwrap(); let expected = CategoryAddedEventBuilder::default() .name(cmd.name().into()) .description(cmd.description().as_ref().map(|s| s.to_string())) - .added_by_user(cmd.adding_by().clone()) - .store_id(cmd.store_id().clone()) - .category_id(category_id.clone()) + .added_by_user(*cmd.adding_by()) + .store_id(*cmd.store_id()) + .category_id(category_id) .build() .unwrap(); let expected = InventoryEvent::CategoryAdded(expected); diff --git a/src/inventory/domain/product_added_event.rs b/src/inventory/domain/product_added_event.rs index 305aa32..53138a7 100644 --- a/src/inventory/domain/product_added_event.rs +++ b/src/inventory/domain/product_added_event.rs @@ -53,13 +53,13 @@ pub mod tests { .name(cmd.name().into()) .description(cmd.description().as_ref().map(|s| s.to_string())) .image(cmd.image().as_ref().map(|s| s.to_string())) - .sku_able(cmd.sku_able().clone()) - .category_id(cmd.category_id().clone()) - .product_id(UUID.clone()) + .sku_able(*cmd.sku_able()) + .category_id(*cmd.category_id()) + .product_id(UUID) .price(cmd.price().clone()) .customizations(customizations) .quantity(cmd.quantity().clone()) - .added_by_user(cmd.adding_by().clone()) + .added_by_user(*cmd.adding_by()) .build() .unwrap() } diff --git a/src/inventory/domain/product_aggregate.rs b/src/inventory/domain/product_aggregate.rs index 109729a..edf01bd 100644 --- a/src/inventory/domain/product_aggregate.rs +++ b/src/inventory/domain/product_aggregate.rs @@ -5,10 +5,10 @@ use std::str::FromStr; use async_trait::async_trait; -use config::builder; use cqrs_es::Aggregate; use derive_builder::Builder; use derive_getters::Getters; +use derive_more::Display; use serde::{Deserialize, Serialize}; use uuid::Uuid; @@ -16,12 +16,17 @@ use super::{commands::InventoryCommand, events::InventoryEvent}; use crate::inventory::application::services::errors::*; use crate::inventory::application::services::InventoryServicesInterface; -#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd)] +#[derive(Clone, Display, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd)] pub enum QuantityUnit { + #[display(fmt = "{}", KILO_GRAM)] Kilogram, + #[display(fmt = "{}", GRAM)] Gram, + #[display(fmt = "{}", DISCRETE_NUMBER)] DiscreteNumber, // example: 1 sofa, 2 bed, etc. + #[display(fmt = "{}", MILLI_LITER)] MilliLiter, + #[display(fmt = "{}", LITER)] Liter, } @@ -37,19 +42,6 @@ const DISCRETE_NUMBER: &str = "discrete_number"; const MILLI_LITER: &str = "ml"; const LITER: &str = "l"; -impl ToString for QuantityUnit { - fn to_string(&self) -> String { - match self { - Self::Kilogram => KILO_GRAM, - Self::Gram => GRAM, - Self::DiscreteNumber => DISCRETE_NUMBER, - Self::MilliLiter => MILLI_LITER, - Self::Liter => LITER, - } - .into() - } -} - impl FromStr for QuantityUnit { type Err = String; fn from_str(s: &str) -> Result { @@ -72,22 +64,14 @@ pub struct Quantity { unit: QuantityUnit, } -#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd)] +#[derive(Clone, Display, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd)] pub enum Currency { + #[display(fmt = "{}", INR)] INR, } const INR: &str = "INR"; -impl ToString for Currency { - fn to_string(&self) -> String { - match self { - Self::INR => INR, - } - .into() - } -} - impl FromStr for Currency { type Err = String; fn from_str(s: &str) -> Result { @@ -172,23 +156,20 @@ impl Aggregate for Product { } fn apply(&mut self, event: Self::Event) { - match event { - InventoryEvent::ProductAdded(e) => { - *self = ProductBuilder::default() - .name(e.name().into()) - .description(e.description().clone()) - .image(e.image().clone()) - .price(e.price().clone()) - .category_id(e.category_id().clone()) - .sku_able(e.sku_able().clone()) - .product_id(e.product_id().clone()) - .quantity(e.quantity().clone()) - .customizations(e.customizations().clone()) - .deleted(false) - .build() - .unwrap(); - } - _ => (), + if let InventoryEvent::ProductAdded(e) = event { + *self = ProductBuilder::default() + .name(e.name().into()) + .description(e.description().clone()) + .image(e.image().clone()) + .price(e.price().clone()) + .category_id(*e.category_id()) + .sku_able(*e.sku_able()) + .product_id(*e.product_id()) + .quantity(e.quantity().clone()) + .customizations(e.customizations().clone()) + .deleted(false) + .build() + .unwrap(); } } } diff --git a/src/inventory/domain/store_aggregate.rs b/src/inventory/domain/store_aggregate.rs index ad3ed8a..aaffbdd 100644 --- a/src/inventory/domain/store_aggregate.rs +++ b/src/inventory/domain/store_aggregate.rs @@ -55,15 +55,12 @@ impl Aggregate for Store { } fn apply(&mut self, event: Self::Event) { - match event { - InventoryEvent::StoreAdded(e) => { - self.name = e.name().into(); - self.address = e.address().as_ref().map(|s| s.to_string()); - self.owner = e.owner().clone(); - self.store_id = e.store_id().clone(); - self.deleted = false; - } - _ => (), + if let InventoryEvent::StoreAdded(e) = event { + self.name = e.name().into(); + self.address = e.address().as_ref().map(|s| s.to_string()); + self.owner = *e.owner(); + self.store_id = *e.store_id(); + self.deleted = false; } } } @@ -99,13 +96,13 @@ mod tests { let expected = StoreAddedEventBuilder::default() .name(name.into()) .address(address.clone()) - .store_id(store_id.clone()) - .owner(owner.into()) + .store_id(store_id) + .owner(owner) .build() .unwrap(); let expected = InventoryEvent::StoreAdded(expected); - let cmd = AddStoreCommand::new(name.into(), address.clone(), owner.clone()).unwrap(); + let cmd = AddStoreCommand::new(name.into(), address.clone(), owner).unwrap(); let mut services = MockInventoryServicesInterface::new(); services diff --git a/src/settings/database.rs b/src/settings/database.rs index 2b49fee..b325b5f 100644 --- a/src/settings/database.rs +++ b/src/settings/database.rs @@ -75,8 +75,6 @@ mod tests { "postgres://test_db_env_override", database.url ); - - assert!(true); }) .join() .unwrap(); diff --git a/src/settings/server.rs b/src/settings/server.rs index c7f7612..a138849 100644 --- a/src/settings/server.rs +++ b/src/settings/server.rs @@ -65,7 +65,6 @@ mod tests { "asdfasdflkjhasdlkfhalksdf", server.cookie_secret ); - assert!(true); }) .join() .unwrap(); diff --git a/src/utils/parse_aggregate_id.rs b/src/utils/parse_aggregate_id.rs index 85c64dd..f16c61f 100644 --- a/src/utils/parse_aggregate_id.rs +++ b/src/utils/parse_aggregate_id.rs @@ -10,18 +10,18 @@ pub fn parse_aggregate_id( non_id: &str, ) -> Result, PersistenceError> { match Uuid::parse_str(aggregate_id) { - Ok(_) => return Ok(None), + Ok(_) => Ok(None), Err(e) => { if aggregate_id == non_id { // if store_id is unbearable, then store isn't created yet. Use cleaner, robust method // later. - return Ok(Some(( + Ok(Some(( T::default(), ViewContext::new(aggregate_id.into(), 0), - ))); + ))) } else { - return Err(PersistenceError::UnknownError(Box::new(e))); + Err(PersistenceError::UnknownError(Box::new(e))) } } - }; + } } diff --git a/src/utils/uuid.rs b/src/utils/uuid.rs index 477fddd..7e0ccf7 100644 --- a/src/utils/uuid.rs +++ b/src/utils/uuid.rs @@ -50,9 +50,9 @@ pub mod tests { let mut m = MockGetUUID::new(); if let Some(times) = times { - m.expect_get_uuid().times(times).return_const(UUID.clone()); + m.expect_get_uuid().times(times).return_const(UUID); } else { - m.expect_get_uuid().return_const(UUID.clone()); + m.expect_get_uuid().return_const(UUID); } Arc::new(m)