diff --git a/.sqlx/query-78c1fa1b50afb781ed7c28c5454f43c61894b22bcde01097ca41ab691d985aea.json b/.sqlx/query-78c1fa1b50afb781ed7c28c5454f43c61894b22bcde01097ca41ab691d985aea.json new file mode 100644 index 0000000..24f8c48 --- /dev/null +++ b/.sqlx/query-78c1fa1b50afb781ed7c28c5454f43c61894b22bcde01097ca41ab691d985aea.json @@ -0,0 +1,23 @@ +{ + "db_name": "PostgreSQL", + "query": "SELECT EXISTS (\n SELECT 1\n FROM cqrs_inventory_product_customizations_query\n WHERE\n name = $1\n AND\n product_id = $2\n AND\n deleted = false\n );", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "exists", + "type_info": "Bool" + } + ], + "parameters": { + "Left": [ + "Text", + "Uuid" + ] + }, + "nullable": [ + null + ] + }, + "hash": "78c1fa1b50afb781ed7c28c5454f43c61894b22bcde01097ca41ab691d985aea" +} diff --git a/src/inventory/adapters/output/db/postgres/customization_name_exists_for_product.rs b/src/inventory/adapters/output/db/postgres/customization_name_exists_for_product.rs new file mode 100644 index 0000000..ed7b710 --- /dev/null +++ b/src/inventory/adapters/output/db/postgres/customization_name_exists_for_product.rs @@ -0,0 +1,111 @@ +// SPDX-FileCopyrightText: 2024 Aravinth Manivannan +// +// SPDX-License-Identifier: AGPL-3.0-or-later +use uuid::Uuid; + +use super::InventoryDBPostgresAdapter; +use crate::inventory::application::port::output::db::{ + customization_name_exists_for_product::*, errors::*, +}; +use crate::inventory::domain::product_aggregate::*; + +#[async_trait::async_trait] +impl CustomizationNameExistsForProductDBPort for InventoryDBPostgresAdapter { + async fn customization_name_exists_for_product( + &self, + c: &Customization, + product_id: &Uuid, + ) -> InventoryDBResult { + let res = sqlx::query!( + "SELECT EXISTS ( + SELECT 1 + FROM cqrs_inventory_product_customizations_query + WHERE + name = $1 + AND + product_id = $2 + AND + deleted = false + );", + c.name(), + product_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::customization_id_exists::tests::create_dummy_customization_record; + use crate::utils::uuid::tests::UUID; + + #[actix_rt::test] + async fn test_postgres_customization_exists() { + let customization_name = "foo_customization"; + let product_id = UUID; + + 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 customization = { + CustomizationBuilder::default() + .name(customization_name.into()) + .customization_id(UUID) + .deleted(false) + .build() + .unwrap() + }; + + // state doesn't exist + assert!(!db + .customization_name_exists_for_product(&customization, &product_id) + .await + .unwrap()); + + create_dummy_customization_record(&customization, &db).await; + + // state exists + assert!(db + .customization_name_exists_for_product(&customization, &product_id) + .await + .unwrap()); + + // Set customization.deleted = true; now db.customization_name_exists_for_product must return false + sqlx::query!( + "UPDATE + cqrs_inventory_product_customizations_query + SET + deleted = true + WHERE + customization_id = $1 + AND + product_id = $2 + AND + name = $3;", + customization.customization_id(), + &product_id, + customization.name() + ) + .execute(&db.pool) + .await + .unwrap(); + assert!(!db + .customization_name_exists_for_product(&customization, &product_id) + .await + .unwrap()); + + settings.drop_db().await; + } +} diff --git a/src/inventory/adapters/output/db/postgres/mod.rs b/src/inventory/adapters/output/db/postgres/mod.rs index 9c106f3..eac7d1e 100644 --- a/src/inventory/adapters/output/db/postgres/mod.rs +++ b/src/inventory/adapters/output/db/postgres/mod.rs @@ -11,6 +11,8 @@ use crate::db::{migrate::RunMigrations, sqlx_postgres::Postgres}; mod category_id_exists; mod category_name_exists_for_store; mod category_view; +mod customization_id_exists; +mod customization_name_exists_for_product; mod errors; mod product_id_exists; mod product_name_exists_for_category;