fix: use UUID instead of Store obj to check constraint violations w DB port
This commit is contained in:
parent
b146b13bc4
commit
ae8615b8ee
3 changed files with 19 additions and 23 deletions
|
@ -1,6 +1,7 @@
|
|||
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||
//
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::InventoryDBPostgresAdapter;
|
||||
use crate::inventory::application::port::output::db::{errors::*, store_id_exists::*};
|
||||
|
@ -8,7 +9,7 @@ use crate::inventory::domain::store_aggregate::*;
|
|||
|
||||
#[async_trait::async_trait]
|
||||
impl StoreIDExistsDBPort for InventoryDBPostgresAdapter {
|
||||
async fn store_id_exists(&self, s: &Store) -> InventoryDBResult<bool> {
|
||||
async fn store_id_exists(&self, store_id: &Uuid) -> InventoryDBResult<bool> {
|
||||
let res = sqlx::query!(
|
||||
"SELECT EXISTS (
|
||||
SELECT 1
|
||||
|
@ -16,7 +17,7 @@ impl StoreIDExistsDBPort for InventoryDBPostgresAdapter {
|
|||
WHERE
|
||||
store_id = $1
|
||||
);",
|
||||
s.store_id(),
|
||||
store_id
|
||||
)
|
||||
.fetch_one(&self.pool)
|
||||
.await?;
|
||||
|
@ -73,12 +74,12 @@ pub mod tests {
|
|||
.unwrap();
|
||||
|
||||
// state doesn't exist
|
||||
assert!(!db.store_id_exists(&store).await.unwrap());
|
||||
assert!(!db.store_id_exists(store.store_id()).await.unwrap());
|
||||
|
||||
create_dummy_store_record(&store, &db).await;
|
||||
|
||||
// state exists
|
||||
assert!(db.store_id_exists(&store).await.unwrap());
|
||||
assert!(db.store_id_exists(store.store_id()).await.unwrap());
|
||||
|
||||
settings.drop_db().await;
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
use mockall::predicate::*;
|
||||
use mockall::*;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::inventory::domain::store_aggregate::Store;
|
||||
|
||||
|
@ -15,7 +16,7 @@ pub use tests::*;
|
|||
#[automock]
|
||||
#[async_trait::async_trait]
|
||||
pub trait StoreIDExistsDBPort: Send + Sync {
|
||||
async fn store_id_exists(&self, s: &Store) -> InventoryDBResult<bool>;
|
||||
async fn store_id_exists(&self, store_id: &Uuid) -> InventoryDBResult<bool>;
|
||||
}
|
||||
|
||||
pub type StoreIDExistsDBPortObj = std::sync::Arc<dyn StoreIDExistsDBPort>;
|
||||
|
|
|
@ -38,7 +38,17 @@ pub struct AddStoreService {
|
|||
impl AddStoreUseCase for AddStoreService {
|
||||
async fn add_store(&self, cmd: AddStoreCommand) -> InventoryResult<StoreAddedEvent> {
|
||||
let mut store_id = self.get_uuid.get_uuid();
|
||||
let mut store = StoreBuilder::default()
|
||||
|
||||
loop {
|
||||
if self.db_store_id_exists.store_id_exists(&store_id).await? {
|
||||
store_id = self.get_uuid.get_uuid();
|
||||
continue;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
let store = StoreBuilder::default()
|
||||
.name(cmd.name().into())
|
||||
.address(cmd.address().as_ref().map(|s| s.to_string()))
|
||||
.owner(*cmd.owner())
|
||||
|
@ -50,22 +60,6 @@ impl AddStoreUseCase for AddStoreService {
|
|||
return Err(InventoryError::DuplicateStoreName);
|
||||
}
|
||||
|
||||
loop {
|
||||
if self.db_store_id_exists.store_id_exists(&store).await? {
|
||||
store_id = self.get_uuid.get_uuid();
|
||||
store = StoreBuilder::default()
|
||||
.name(cmd.name().into())
|
||||
.address(cmd.address().as_ref().map(|s| s.to_string()))
|
||||
.owner(*cmd.owner())
|
||||
.store_id(store_id)
|
||||
.build()
|
||||
.unwrap();
|
||||
continue;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(StoreAddedEventBuilder::default()
|
||||
.name(store.name().into())
|
||||
.address(store.address().as_ref().map(|s| s.to_string()))
|
||||
|
@ -141,7 +135,7 @@ pub mod tests {
|
|||
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))
|
||||
.db_store_id_exists(mock_store_id_exists_db_port_false(IS_CALLED_ONLY_ONCE))
|
||||
.db_store_name_exists(mock_store_name_exists_db_port_true(IS_CALLED_ONLY_ONCE))
|
||||
.get_uuid(mock_get_uuid(IS_CALLED_ONLY_ONCE))
|
||||
.build()
|
||||
|
|
Loading…
Reference in a new issue