mayhem_db/client/
removal.rs

1use crate::models::user;
2use migration::DbErr;
3use sea_orm::{ActiveModelTrait, DbConn, DeleteResult, EntityTrait};
4use std::sync::Arc;
5
6#[derive(Clone)]
7pub struct RemovalHelper {
8    client: Arc<DbConn>,
9}
10
11unsafe impl Sync for RemovalHelper {}
12unsafe impl Send for RemovalHelper {}
13
14impl RemovalHelper {
15    pub fn create(client: Arc<DbConn>) -> Self {
16        return Self { client };
17    }
18
19    pub async fn delete_user(&self, id: i32) -> Result<DeleteResult, DbErr> {
20        let object: user::ActiveModel = user::Entity::find_by_id(id)
21            .one(&self.client as &DbConn)
22            .await
23            .unwrap()
24            .ok_or(DbErr::Custom("Cannot find model!".to_owned()))
25            .map(Into::into)
26            .unwrap();
27
28        return object.delete(&self.client as &DbConn).await;
29    }
30
31    pub async fn delete_all_users(&self) -> Result<DeleteResult, DbErr> {
32        return user::Entity::delete_many()
33            .exec(&self.client as &DbConn)
34            .await;
35    }
36}