use sqlx::PgPool;
use uuid::Uuid;
use crate::domain::entity::Brand;
pub const TABLE_NAME: &str = "catalog.brands";
pub struct BrandRepository(
backbone_orm::GenericCrudRepository<Brand, backbone_orm::SoftDelete>,
);
impl std::ops::Deref for BrandRepository {
type Target = backbone_orm::GenericCrudRepository<Brand, backbone_orm::SoftDelete>;
fn deref(&self) -> &Self::Target { &self.0 }
}
impl BrandRepository {
pub fn new(pool: PgPool) -> Self {
Self(backbone_orm::GenericCrudRepository::new(pool, TABLE_NAME))
}
}
pub struct NewBrandRow<'a> {
pub id: Uuid,
pub code: &'a str,
pub name: &'a str,
pub short_description: Option<&'a str>,
pub description: Option<&'a str>,
pub logo_url: Option<&'a str>,
pub sort_order: i32,
}
impl BrandRepository {
pub async fn exists_id(&self, executor: impl sqlx::Executor<'_, Database = sqlx::Postgres>, id: Uuid) -> Result<bool, sqlx::Error> {
let found: Option<Uuid> = sqlx::query_scalar(
"SELECT id FROM catalog.brands \
WHERE id = $1 AND (metadata->>'deleted_at') IS NULL",
)
.bind(id)
.fetch_optional(executor)
.await?;
Ok(found.is_some())
}
pub async fn insert_brand(
&self,
executor: impl sqlx::Executor<'_, Database = sqlx::Postgres>,
r: &NewBrandRow<'_>,
) -> Result<(), sqlx::Error> {
sqlx::query(
r#"INSERT INTO catalog.brands
(id, code, name, short_description, description, logo_url, sort_order, status)
VALUES ($1,$2,$3,$4,$5,$6,$7,'active'::catalog_status)"#,
)
.bind(r.id)
.bind(r.code)
.bind(r.name)
.bind(r.short_description)
.bind(r.description)
.bind(r.logo_url)
.bind(r.sort_order)
.execute(executor)
.await?;
Ok(())
}
}
backbone_core::impl_crud_repository!(BrandRepository, Brand, soft_delete);