use sqlx::PgPool;
use uuid::Uuid;
use crate::domain::entity::ItemGroup;
pub const TABLE_NAME: &str = "catalog.item_groups";
pub struct ItemGroupRepository(
backbone_orm::GenericCrudRepository<ItemGroup, backbone_orm::SoftDelete>,
);
impl std::ops::Deref for ItemGroupRepository {
type Target = backbone_orm::GenericCrudRepository<ItemGroup, backbone_orm::SoftDelete>;
fn deref(&self) -> &Self::Target { &self.0 }
}
impl ItemGroupRepository {
pub fn new(pool: PgPool) -> Self {
Self(backbone_orm::GenericCrudRepository::new(pool, TABLE_NAME))
}
}
pub struct NewItemGroupRow<'a> {
pub id: Uuid,
pub code: &'a str,
pub name: &'a str,
pub parent_id: Option<Uuid>,
pub is_group: bool,
}
impl ItemGroupRepository {
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.item_groups \
WHERE id = $1 AND (metadata->>'deleted_at') IS NULL",
)
.bind(id)
.fetch_optional(executor)
.await?;
Ok(found.is_some())
}
pub async fn insert_item_group(
&self,
executor: impl sqlx::Executor<'_, Database = sqlx::Postgres>,
r: &NewItemGroupRow<'_>,
) -> Result<(), sqlx::Error> {
sqlx::query(
r#"INSERT INTO catalog.item_groups (id, code, name, parent_id, is_group, status)
VALUES ($1,$2,$3,$4,$5,'active'::catalog_status)"#,
)
.bind(r.id)
.bind(r.code)
.bind(r.name)
.bind(r.parent_id)
.bind(r.is_group)
.execute(executor)
.await?;
Ok(())
}
}
backbone_core::impl_crud_repository!(ItemGroupRepository, ItemGroup, soft_delete);