mayhem_db/client/
mutation.rs

1use crate::models::user;
2use migration::DbErr;
3use sea_orm::{ActiveModelTrait, DbConn, EntityTrait, Set};
4use std::sync::Arc;
5
6#[derive(Clone)]
7pub struct MutationHelper {
8    client: Arc<DbConn>,
9}
10
11unsafe impl Sync for MutationHelper {}
12unsafe impl Send for MutationHelper {}
13
14impl MutationHelper {
15    pub fn create(client: Arc<DbConn>) -> Self {
16        return Self { client };
17    }
18
19    pub async fn update_user(&self, id: i32, data: user::Model) -> Result<user::Model, 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 (user::ActiveModel {
29            id: object.id,
30            first_name: Set(data.first_name),
31            last_name: Set(data.last_name),
32            email: Set(data.email),
33            username: Set(data.username),
34            password: Set(data.password),
35        })
36        .update(&self.client as &DbConn)
37        .await;
38    }
39}