feat: add product against categories #34
1 changed files with 78 additions and 0 deletions
|
@ -0,0 +1,78 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use super::InventoryDBPostgresAdapter;
|
||||||
|
use crate::inventory::application::port::output::db::{
|
||||||
|
errors::*, product_name_exists_for_category::*,
|
||||||
|
};
|
||||||
|
use crate::inventory::domain::product_aggregate::*;
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl ProductNameExistsForCategoryDBPort for InventoryDBPostgresAdapter {
|
||||||
|
async fn product_name_exists_for_category(&self, s: &Product) -> InventoryDBResult<bool> {
|
||||||
|
let res = sqlx::query!(
|
||||||
|
"SELECT EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM cqrs_inventory_product_query
|
||||||
|
WHERE
|
||||||
|
name = $1
|
||||||
|
AND
|
||||||
|
category_id = $2
|
||||||
|
);",
|
||||||
|
s.name(),
|
||||||
|
s.category_id(),
|
||||||
|
)
|
||||||
|
.fetch_one(&self.pool)
|
||||||
|
.await?;
|
||||||
|
if let Some(x) = res.exists {
|
||||||
|
Ok(x)
|
||||||
|
} else {
|
||||||
|
Ok(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use crate::inventory::adapters::output::db::postgres::product_id_exists::tests::create_dummy_product_record;
|
||||||
|
use crate::inventory::domain::add_product_command::tests::get_command;
|
||||||
|
use crate::utils::uuid::tests::UUID;
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_postgres_product_exists() {
|
||||||
|
let product_name = "foo_product";
|
||||||
|
|
||||||
|
let settings = crate::settings::tests::get_settings().await;
|
||||||
|
settings.create_db().await;
|
||||||
|
let db = super::InventoryDBPostgresAdapter::new(
|
||||||
|
sqlx::postgres::PgPool::connect(&settings.database.url)
|
||||||
|
.await
|
||||||
|
.unwrap(),
|
||||||
|
);
|
||||||
|
|
||||||
|
let cmd = get_command();
|
||||||
|
|
||||||
|
let product = ProductBuilder::default()
|
||||||
|
.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())
|
||||||
|
.product_id(UUID.clone())
|
||||||
|
.price(cmd.price().clone())
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
// state doesn't exist
|
||||||
|
assert!(!db.product_name_exists_for_category(&product).await.unwrap());
|
||||||
|
|
||||||
|
create_dummy_product_record(&product, &db).await;
|
||||||
|
|
||||||
|
// state exists
|
||||||
|
assert!(db.product_name_exists_for_category(&product).await.unwrap());
|
||||||
|
|
||||||
|
settings.drop_db().await;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue