algohub_server/utils/
account.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use anyhow::Result;
use serde::Deserialize;
use surrealdb::engine::remote::ws::Client;
use surrealdb::Surreal;

use crate::models::account::{Account, Profile, Role};
use crate::models::UpdateAt;
use crate::routes::account::RegisterData;

pub async fn create(db: &Surreal<Client>, register: RegisterData) -> Result<Option<Account>> {
    let mut queried = db
        .query("SELECT * FROM account WHERE username = $username OR email = $email")
        .bind(("username", register.username.clone()))
        .bind(("email", register.email.clone()))
        .await?;
    let exist_account: Option<Account> = queried.take(0)?;
    if exist_account.is_some() {
        return Ok(None);
    }

    let account: Option<Account> = db
        .create("account")
        .content(Account {
            username: register.username,
            password: register.password,
            email: register.email,
            role: Role::User,
            created_at: chrono::Local::now().naive_local(),
            ..Default::default()
        })
        .await?;
    Ok(account)
}

pub async fn get_by_id<M>(db: &Surreal<Client>, id: &str) -> Result<Option<M>>
where
    for<'de> M: Deserialize<'de>,
{
    Ok(db.select(("account", id)).await?)
}

pub async fn merge_profile(
    db: &Surreal<Client>,
    id: &str,
    profile: Profile,
) -> Result<Option<Account>> {
    db.update::<Option<Account>>(("account", id))
        .merge(profile)
        .await?;
    Ok(db
        .update(("account", id))
        .merge(UpdateAt {
            updated_at: chrono::Local::now().naive_local(),
        })
        .await?)
}

pub async fn delete(db: &Surreal<Client>, id: &str) -> Result<()> {
    db.delete::<Option<Account>>(("account", id)).await?;
    Ok(())
}

pub async fn get_by_identity(db: &Surreal<Client>, identity: &str) -> Result<Option<Account>> {
    Ok(db
        .query("SELECT * FROM account WHERE username = $identity OR email = $identity")
        .bind(("identity", identity.to_string()))
        .await?
        .take(0)?)
}