use anyhow::Result;
use sqlx::PgPool;
use crate::domain::entity::DiscussChannel;
pub const TABLE_NAME: &str = "messaging.discuss_channels";
pub struct DiscussChannelRepository(
backbone_orm::GenericCrudRepository<DiscussChannel, backbone_orm::SoftDelete>,
);
impl std::ops::Deref for DiscussChannelRepository {
type Target = backbone_orm::GenericCrudRepository<DiscussChannel, backbone_orm::SoftDelete>;
fn deref(&self) -> &Self::Target { &self.0 }
}
impl DiscussChannelRepository {
pub fn new(pool: PgPool) -> Self {
Self(backbone_orm::GenericCrudRepository::new(pool, TABLE_NAME))
}
}
impl DiscussChannelRepository {
pub async fn find_by_uuid(&self, uuid: Option<String>) -> Result<Option<DiscussChannel>> {
let query = format!("SELECT * FROM {} WHERE uuid = $1 AND metadata->>'deleted_at' IS NULL", self.table_name());
let result = sqlx::query_as::<_, DiscussChannel>(&query)
.bind(uuid)
.fetch_optional(self.pool())
.await?;
Ok(result)
}
pub async fn exists_by_uuid(&self, uuid: Option<String>) -> Result<bool> {
let query = format!("SELECT 1 FROM {} WHERE uuid = $1 AND metadata->>'deleted_at' IS NULL LIMIT 1", self.table_name());
let result = sqlx::query_scalar::<_, i32>(&query)
.bind(uuid)
.fetch_optional(self.pool())
.await?;
Ok(result.is_some())
}
}
backbone_core::impl_crud_repository!(DiscussChannelRepository, DiscussChannel, soft_delete);