feat: impl customization_id_exists for postgres

This commit is contained in:
Aravinth Manivannan 2024-07-16 11:28:51 +05:30
parent 38485e572f
commit 37aeac8c69
Signed by: realaravinth
GPG key ID: F8F50389936984FF
3 changed files with 126 additions and 2 deletions

View file

@ -0,0 +1,22 @@
{
"db_name": "PostgreSQL",
"query": "SELECT EXISTS (\n SELECT 1\n FROM cqrs_inventory_product_customizations_query\n WHERE\n customization_id = $1\n );",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "exists",
"type_info": "Bool"
}
],
"parameters": {
"Left": [
"Uuid"
]
},
"nullable": [
null
]
},
"hash": "3292856681e8d41391ba1d213b118a1f879677b14a2e6aad113b513d93b7c9a3"
}

View file

@ -0,0 +1,100 @@
// 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::{customization_id_exists::*, errors::*};
#[async_trait::async_trait]
impl CustomizationIDExistsDBPort for InventoryDBPostgresAdapter {
async fn customization_id_exists(&self, customization_id: &Uuid) -> InventoryDBResult<bool> {
let res = sqlx::query!(
"SELECT EXISTS (
SELECT 1
FROM cqrs_inventory_product_customizations_query
WHERE
customization_id = $1
);",
customization_id
)
.fetch_one(&self.pool)
.await?;
if let Some(x) = res.exists {
Ok(x)
} else {
Ok(false)
}
}
}
#[cfg(test)]
pub mod tests {
use super::*;
use crate::inventory::domain::add_product_command::tests::get_customizations;
use crate::inventory::domain::product_aggregate::*;
use crate::utils::uuid::tests::UUID;
#[actix_rt::test]
async fn test_postgres_customization_exists() {
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 = {
let c = get_customizations().first().unwrap().clone();
CustomizationBuilder::default()
.name(c.name().into())
.customization_id(UUID)
.deleted(false)
.build()
.unwrap()
};
// state doesn't exist
assert!(!db
.customization_id_exists(customization.customization_id())
.await
.unwrap());
create_dummy_customization_record(&customization, &db).await;
// state exists
assert!(db
.customization_id_exists(customization.customization_id())
.await
.unwrap());
settings.drop_db().await;
}
pub async fn create_dummy_customization_record(
c: &Customization,
db: &InventoryDBPostgresAdapter,
) {
sqlx::query!(
"INSERT INTO cqrs_inventory_product_customizations_query (
version,
name,
customization_id,
product_id,
deleted
) VALUES (
$1, $2, $3, $4, $5
);",
1,
c.name(),
c.customization_id(),
UUID,
c.deleted().clone(),
)
.execute(&db.pool)
.await
.unwrap();
}
}

View file

@ -19,7 +19,9 @@ impl From<SqlxError> for InventoryDBError {
return Self::DuplicateStoreID; return Self::DuplicateStoreID;
} else if msg.contains("cqrs_inventory_store_query_product_id_key") { } else if msg.contains("cqrs_inventory_store_query_product_id_key") {
return Self::DuplicateProductID; return Self::DuplicateProductID;
} else if msg.contains("cqrs_inventory_product_customizations_query_customization_id_key") { } else if msg
.contains("cqrs_inventory_product_customizations_query_customization_id_key")
{
return Self::DuplicateCustomizationID; return Self::DuplicateCustomizationID;
} else if msg.contains("cqrs_inventory_store_query_category_id_key") { } else if msg.contains("cqrs_inventory_store_query_category_id_key") {
return Self::DuplicateCategoryID; return Self::DuplicateCategoryID;
@ -30,7 +32,7 @@ impl From<SqlxError> for InventoryDBError {
} else if msg.contains("cqrs_inventory_store_query_name_key") { } else if msg.contains("cqrs_inventory_store_query_name_key") {
return Self::DuplicateStoreName; return Self::DuplicateStoreName;
} else if msg.contains("cqrs_inventory_product_customizations_query_name_key") { } else if msg.contains("cqrs_inventory_product_customizations_query_name_key") {
return Self::DuplicateCustomizationName return Self::DuplicateCustomizationName;
} else { } else {
println!("{msg}"); println!("{msg}");
} }