use crate::core::condition::SqlValue;
use crate::orm::postgres::model::PgModel;
pub struct SoftDeleteService<M>(std::marker::PhantomData<M>);
impl<M: PgModel> SoftDeleteService<M> {
pub async fn all_active(pool: &sqlx::PgPool) -> Result<Vec<M>, sqlx::Error> {
let builder = match M::soft_delete_column() {
Some(col) => M::query().where_null(col),
None => M::query(),
};
M::find_where(pool, builder).await
}
pub async fn all_deleted(pool: &sqlx::PgPool) -> Result<Vec<M>, sqlx::Error> {
match M::soft_delete_column() {
Some(col) => M::find_where(pool, M::query().where_not_null(col)).await,
None => Ok(Vec::new()),
}
}
pub async fn with_trashed(pool: &sqlx::PgPool) -> Result<Vec<M>, sqlx::Error> {
M::all(pool).await
}
pub async fn soft_delete(
id: impl Into<SqlValue> + Send,
pool: &sqlx::PgPool,
) -> Result<u64, sqlx::Error> {
M::soft_delete_by_pk(pool, id.into()).await
}
pub async fn restore(
id: impl Into<SqlValue> + Send,
pool: &sqlx::PgPool,
) -> Result<u64, sqlx::Error> {
M::restore_by_pk(pool, id.into()).await
}
pub async fn force_delete(
id: impl Into<SqlValue> + Send,
pool: &sqlx::PgPool,
) -> Result<u64, sqlx::Error> {
M::delete_by_pk(pool, id.into()).await
}
pub async fn purge_deleted(pool: &sqlx::PgPool) -> Result<u64, sqlx::Error> {
match M::soft_delete_column() {
Some(col) => M::delete_where(pool, M::query().where_not_null(col)).await,
None => Ok(0),
}
}
}