Compare commits

..

No commits in common. "73d829f3f8d2fff261ca7cdc47b8cecd419ba275" and "5245cf02e0c0142b2cea05572919c32c435919f6" have entirely different histories.

9 changed files with 74 additions and 249 deletions

View file

@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
"query": "UPDATE\n cqrs_inventory_category_query\n SET\n version = $1,\n name = $2,\n description = $3,\n store_id = $4,\n deleted = $5;",
"query": "UPDATE\n cqrs_inventory_category_query\n SET\n version = $1,\n name = $2,\n description = $3,\n category_id = $4,\n store_id = $5,\n deleted = $6;",
"describe": {
"columns": [],
"parameters": {
@ -9,10 +9,11 @@
"Text",
"Text",
"Uuid",
"Uuid",
"Bool"
]
},
"nullable": []
},
"hash": "c9bee14e15dae80b7af9b0fdca43a97b964024252bed2d0caeebee175acd55d9"
"hash": "14d111d7453b89f2346966a9fdd725d269ef36288ed5e7fe1f7ad452deaab0e6"
}

View file

@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
"query": "UPDATE\n cqrs_inventory_product_customizations_query\n SET\n version = $1,\n name = $2,\n product_id = $3,\n deleted = $4;",
"query": "UPDATE\n cqrs_inventory_product_customizations_query\n SET\n version = $1,\n name = $2,\n customization_id = $3,\n product_id = $4,\n deleted = $5;",
"describe": {
"columns": [],
"parameters": {
@ -8,10 +8,11 @@
"Int8",
"Text",
"Uuid",
"Uuid",
"Bool"
]
},
"nullable": []
},
"hash": "d46bb69f4e2afbae01ab08beb48a67b2cade64d9ebd47f3fc6143b057671cc1b"
"hash": "e88a5dae732c3f8180664f306b4bb1d21f97a2f1391860eb8714a52ef4439d81"
}

View file

@ -10,7 +10,7 @@ use uuid::Uuid;
use super::errors::*;
use super::InventoryDBPostgresAdapter;
use crate::inventory::domain::category_aggregate::{Category, CategoryBuilder};
use crate::inventory::domain::category_aggregate::Category;
use crate::inventory::domain::events::InventoryEvent;
use crate::utils::parse_aggregate_id::parse_aggregate_id;
@ -27,19 +27,6 @@ pub struct CategoryView {
deleted: bool,
}
impl From<CategoryView> for Category {
fn from(v: CategoryView) -> Self {
CategoryBuilder::default()
.name(v.name)
.description(v.description)
.category_id(v.category_id)
.store_id(v.store_id)
.deleted(v.deleted)
.build()
.unwrap()
}
}
// This updates the view with events as they are committed.
// The logic should be minimal here, e.g., don't calculate the account balance,
// design the events to carry the balance information instead.
@ -177,11 +164,13 @@ impl ViewRepository<CategoryView, Category> for InventoryDBPostgresAdapter {
version = $1,
name = $2,
description = $3,
store_id = $4,
deleted = $5;",
category_id = $4,
store_id = $5,
deleted = $6;",
version,
view.name,
view.description,
view.category_id,
view.store_id,
view.deleted
)
@ -228,143 +217,3 @@ impl Query<Category> for InventoryDBPostgresAdapter {
self.update_view(view, view_context).await.unwrap();
}
}
#[cfg(test)]
mod tests {
use super::*;
use postgres_es::PostgresCqrs;
use crate::{
db::migrate::*,
inventory::{
application::services::{
add_category_service::*, update_category_service::*, MockInventoryServicesInterface,
},
domain::{
add_category_command::*, category_aggregate::*, commands::*, events::*,
store_aggregate::*, update_category_command::*,
},
},
tests::bdd::*,
utils::{random_string::GenerateRandomStringInterface, uuid::tests::UUID},
};
use std::sync::Arc;
#[actix_rt::test]
async fn pg_query_inventory_category_view() {
let settings = crate::settings::tests::get_settings().await;
//let settings = crate::settings::Settings::new().unwrap();
settings.create_db().await;
let db = crate::db::sqlx_postgres::Postgres::init(&settings.database.url).await;
db.migrate().await;
let db = InventoryDBPostgresAdapter::new(db.pool.clone());
let store = Store::default();
crate::inventory::adapters::output::db::postgres::store_id_exists::tests::create_dummy_store_record(&store, &db).await;
let queries: Vec<Box<dyn Query<Category>>> = vec![Box::new(db.clone())];
let mut mock_services = MockInventoryServicesInterface::new();
let db2 = Arc::new(db.clone());
mock_services
.expect_add_category()
.times(IS_CALLED_ONLY_ONCE.unwrap())
.returning(move || {
Arc::new(
AddCategoryServiceBuilder::default()
.db_store_id_exists(db2.clone())
.db_category_name_exists_for_store(db2.clone())
.db_category_id_exists(db2.clone())
.build()
.unwrap(),
)
});
let db2 = Arc::new(db.clone());
mock_services
.expect_update_category()
.times(IS_CALLED_ONLY_ONCE.unwrap())
.returning(move || {
Arc::new(
UpdateCategoryServiceBuilder::default()
.db_store_id_exists(db2.clone())
.db_category_name_exists_for_store(db2.clone())
.db_category_id_exists(db2.clone())
.build()
.unwrap(),
)
});
let (cqrs, category_query): (
Arc<PostgresCqrs<Category>>,
Arc<dyn ViewRepository<CategoryView, Category>>,
) = (
Arc::new(postgres_es::postgres_cqrs(
db.pool.clone(),
queries,
Arc::new(mock_services),
)),
Arc::new(db.clone()),
);
let rand = crate::utils::random_string::GenerateRandomString {};
let cmd = AddCategoryCommandBuilder::default()
.name(rand.get_random(10))
.description(None)
.store_id(*store.store_id())
.adding_by(UUID)
.category_id(UUID)
.build()
.unwrap();
cqrs.execute(
&cmd.category_id().to_string(),
InventoryCommand::AddCategory(cmd.clone()),
)
.await
.unwrap();
let category = category_query
.load(&(*cmd.category_id()).to_string())
.await
.unwrap()
.unwrap();
let category: Category = category.into();
assert_eq!(category.name(), cmd.name());
assert_eq!(category.description(), cmd.description());
assert_eq!(category.category_id(), cmd.category_id());
assert_eq!(category.store_id(), cmd.store_id());
assert!(!store.deleted());
let update_category_cmd = UpdateCategoryCommand::new(
rand.get_random(10),
Some(rand.get_random(10)),
category,
UUID,
)
.unwrap();
cqrs.execute(
&cmd.category_id().to_string(),
InventoryCommand::UpdateCategory(update_category_cmd.clone()),
)
.await
.unwrap();
let category = category_query
.load(&(*cmd.category_id()).to_string())
.await
.unwrap()
.unwrap();
let category: Category = category.into();
assert_eq!(category.name(), update_category_cmd.name());
assert_eq!(category.description(), update_category_cmd.description());
assert_eq!(category.category_id(), cmd.category_id());
assert_eq!(category.store_id(), cmd.store_id());
assert!(!category.deleted());
settings.drop_db().await;
}
}

View file

@ -45,7 +45,7 @@ pub mod tests {
VALUES ($1, $2, $3, $4, $5 ,$6);",
1,
s.name(),
s.address().as_ref().map(|s| s.as_str()),
s.address().as_ref().unwrap(),
s.store_id(),
s.owner(),
false

View file

@ -14,11 +14,12 @@ use crate::inventory::{
category_id_exists::*, category_name_exists_for_store::*, store_id_exists::*,
},
domain::{
add_category_command::*,
add_category_command::AddCategoryCommand,
category_added_event::{CategoryAddedEvent, CategoryAddedEventBuilder},
category_aggregate::*,
},
};
use crate::utils::uuid::*;
#[automock]
#[async_trait::async_trait]
@ -33,6 +34,7 @@ pub struct AddCategoryService {
db_store_id_exists: StoreIDExistsDBPortObj,
db_category_name_exists_for_store: CategoryNameExistsForStoreDBPortObj,
db_category_id_exists: CategoryIDExistsDBPortObj,
get_uuid: GetUUIDInterfaceObj,
}
#[async_trait::async_trait]
@ -46,19 +48,27 @@ impl AddCategoryUseCase for AddCategoryService {
return Err(InventoryError::StoreIDNotFound);
}
let mut category_id = self.get_uuid.get_uuid();
loop {
if self
.db_category_id_exists
.category_id_exists(cmd.category_id())
.category_id_exists(&category_id)
.await?
{
return Err(InventoryError::DuplicateCategoryID);
category_id = self.get_uuid.get_uuid();
continue;
} else {
break;
}
}
let category = CategoryBuilder::default()
.name(cmd.name().into())
.description(cmd.description().as_ref().map(|s| s.to_string()))
.store_id(*cmd.store_id())
.category_id(*cmd.category_id())
.category_id(category_id)
.build()
.unwrap();
@ -87,8 +97,8 @@ pub mod tests {
use uuid::Uuid;
use crate::tests::bdd::*;
use crate::utils::uuid::tests::UUID;
use crate::{tests::bdd::*, utils::uuid::tests::mock_get_uuid};
pub fn mock_add_category_service(
times: Option<usize>,
@ -101,7 +111,7 @@ pub mod tests {
.description(cmd.description().as_ref().map(|s| s.to_string()))
.added_by_user(*cmd.adding_by())
.store_id(*cmd.store_id())
.category_id(*cmd.category_id())
.category_id(UUID)
.build()
.unwrap();
@ -123,13 +133,8 @@ pub mod tests {
let user_id = UUID;
let store_id = Uuid::new_v4();
let cmd = AddCategoryCommandBuilder::default()
.name(name.into())
.description(Some(description.into()))
.store_id(store_id)
.adding_by(user_id)
.category_id(UUID)
.build()
// description = None
let cmd = AddCategoryCommand::new(name.into(), Some(description.into()), store_id, user_id)
.unwrap();
let s = AddCategoryServiceBuilder::default()
@ -138,6 +143,7 @@ pub mod tests {
IS_CALLED_ONLY_ONCE,
))
.db_category_id_exists(mock_category_id_exists_db_port_false(IS_CALLED_ONLY_ONCE))
.get_uuid(mock_get_uuid(IS_CALLED_ONLY_ONCE))
.build()
.unwrap();
@ -156,13 +162,8 @@ pub mod tests {
let user_id = UUID;
let store_id = Uuid::new_v4();
let cmd = AddCategoryCommandBuilder::default()
.name(name.into())
.description(Some(description.into()))
.store_id(store_id)
.adding_by(user_id)
.category_id(UUID)
.build()
// description = None
let cmd = AddCategoryCommand::new(name.into(), Some(description.into()), store_id, user_id)
.unwrap();
let s = AddCategoryServiceBuilder::default()
@ -171,6 +172,7 @@ pub mod tests {
IS_CALLED_ONLY_ONCE,
))
.db_category_id_exists(mock_category_id_exists_db_port_false(IS_CALLED_ONLY_ONCE))
.get_uuid(mock_get_uuid(IS_CALLED_ONLY_ONCE))
.build()
.unwrap();
@ -187,13 +189,8 @@ pub mod tests {
let user_id = UUID;
let store_id = Uuid::new_v4();
let cmd = AddCategoryCommandBuilder::default()
.name(name.into())
.description(Some(description.into()))
.store_id(store_id)
.adding_by(user_id)
.category_id(UUID)
.build()
// description = None
let cmd = AddCategoryCommand::new(name.into(), Some(description.into()), store_id, user_id)
.unwrap();
let s = AddCategoryServiceBuilder::default()
@ -202,6 +199,7 @@ pub mod tests {
IS_NEVER_CALLED,
))
.db_category_id_exists(mock_category_id_exists_db_port_false(IS_NEVER_CALLED))
.get_uuid(mock_get_uuid(IS_NEVER_CALLED))
.build()
.unwrap();

View file

@ -20,7 +20,6 @@ pub enum InventoryError {
DuplicateCustomizationName,
DuplicateCustomizationID,
DuplicateStoreID,
DuplicateCategoryID,
ProductIDNotFound,
CategoryIDNotFound,
CustomizationIDNotFound,
@ -40,7 +39,10 @@ impl From<InventoryDBError> for InventoryError {
error!("DuplicateProductID");
Self::InternalError
}
InventoryDBError::DuplicateCategoryID => Self::DuplicateCategoryID,
InventoryDBError::DuplicateCategoryID => {
error!("DuplicateCategoryID");
Self::InternalError
}
InventoryDBError::DuplicateCustomizationID => Self::DuplicateCustomizationID,
InventoryDBError::InternalError => Self::InternalError,
InventoryDBError::ProductIDNotFound => InventoryError::ProductIDNotFound,

View file

@ -15,6 +15,7 @@ use crate::inventory::{
},
domain::{category_aggregate::*, category_updated_event::*, update_category_command::*},
};
use crate::utils::uuid::*;
#[automock]
#[async_trait::async_trait]

View file

@ -2,7 +2,6 @@
//
// SPDX-License-Identifier: AGPL-3.0-or-later
use derive_builder::Builder;
use derive_getters::Getters;
use derive_more::{Display, Error};
use serde::{Deserialize, Serialize};
@ -13,22 +12,21 @@ pub enum AddCategoryCommandError {
NameIsEmpty,
}
#[derive(
Clone, Debug, Builder, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Getters,
)]
#[builder(build_fn(validate = "Self::validate"))]
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Getters)]
pub struct AddCategoryCommand {
#[builder(setter(custom))]
name: String,
#[builder(setter(custom))]
description: Option<String>,
store_id: Uuid,
category_id: Uuid,
adding_by: Uuid,
}
impl AddCategoryCommandBuilder {
pub fn description(&mut self, description: Option<String>) -> &mut Self {
impl AddCategoryCommand {
pub fn new(
name: String,
description: Option<String>,
store_id: Uuid,
adding_by: Uuid,
) -> Result<Self, AddCategoryCommandError> {
let description: Option<String> = if let Some(description) = description {
let description = description.trim();
if description.is_empty() {
@ -39,22 +37,18 @@ impl AddCategoryCommandBuilder {
} else {
None
};
self.description = Some(description);
self
}
pub fn name(&mut self, name: String) -> &mut Self {
let name = name.trim().to_owned();
self.name = Some(name);
self
if name.is_empty() {
return Err(AddCategoryCommandError::NameIsEmpty);
}
pub fn validate(&self) -> Result<(), String> {
if self.name.as_ref().unwrap().is_empty() {
return Err(AddCategoryCommandError::NameIsEmpty.to_string());
}
Ok(())
Ok(Self {
name,
store_id,
description,
adding_by,
})
}
}
@ -71,41 +65,26 @@ mod tests {
let adding_by = UUID;
let store_id = Uuid::new_v4();
let cmd = AddCategoryCommandBuilder::default()
.name(name.into())
.description(None)
.store_id(store_id)
.adding_by(adding_by)
.category_id(UUID)
.build()
.unwrap();
// description = None
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);
let cmd = AddCategoryCommandBuilder::default()
.name(name.into())
.description(Some(description.into()))
.store_id(store_id)
.adding_by(adding_by)
.category_id(UUID)
.build()
// description = Some
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);
assert_eq!(cmd.store_id(), &store_id);
// AddCategoryCommandError::NameIsEmpty
assert!(AddCategoryCommandBuilder::default()
.name("".into())
.description(Some(description.into()))
.store_id(store_id)
.adding_by(adding_by)
.category_id(UUID)
.build()
.is_err())
assert_eq!(
AddCategoryCommand::new("".into(), Some(description.into()), store_id, adding_by,),
Err(AddCategoryCommandError::NameIsEmpty)
)
}
}

View file

@ -107,14 +107,8 @@ mod aggregate_tests {
let store_id = Uuid::new_v4();
let category_id = UUID;
let cmd = AddCategoryCommandBuilder::default()
.name(name.into())
.description(description.clone())
.store_id(store_id)
.adding_by(adding_by)
.category_id(category_id)
.build()
.unwrap();
let cmd =
AddCategoryCommand::new(name.into(), description.clone(), store_id, adding_by).unwrap();
let expected = CategoryAddedEventBuilder::default()
.name(cmd.name().into())