summaryrefslogtreecommitdiff
path: root/server/src/models/user.rs
blob: dd96f909ff0f2af61d480688039cf24d6926c9a6 (plain)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use crate::db::get_client;
use crate::errors::AppError;

use serde::{Deserialize, Serialize};
use validator::Validate;

#[derive(Deserialize, Serialize, Validate)]
pub struct User {
    id: i32,
    #[validate(length(min = 1, message = "Can not be empty"))]
    email: String,
    #[validate(length(min = 8, message = "Must be min 8 chars length"))]
    password: String,
    is_staff: Option<bool>,
}

#[derive(Deserialize, Serialize)]
pub struct UserList {
    pub id: i32,
    email: String,
    is_staff: Option<bool>,
}

#[derive(Deserialize)]
pub struct UserCreate {
    pub email: String,
    pub password: String,
}

impl User {
    pub fn new(email: String, password: String) -> Self {
        Self {
            id: 0,
            email,
            password,
            is_staff: Some(false),
        }
    }

    pub async fn create(user: User) -> Result<UserList, AppError> {
        let pool = unsafe { get_client() };

        user.validate()
            .map_err(|error| AppError::BadRequest(error.to_string()))?;

        let crypted_password = sha256::digest(user.password);

        let rec = sqlx::query_as!(
            UserList,
            r#"
                INSERT INTO users (email, password)
                VALUES ( $1, $2 )
                RETURNING id, email, is_staff
            "#,
            user.email,
            crypted_password
        )
        .fetch_one(pool)
        .await?;

        Ok(rec)
    }

    pub async fn find(user: User) -> Result<UserList, AppError> {
        let pool = unsafe { get_client() };

        let crypted_password = sha256::digest(user.password);

        let rec = sqlx::query_as!(
            UserList,
            r#"
                SELECT id, email, is_staff FROM "users"
                WHERE email = $1 AND password = $2
            "#,
            user.email,
            crypted_password
        )
        .fetch_one(pool)
        .await?;

        Ok(rec)
    }

    pub async fn find_by_id(user_id: i32) -> Result<UserList, AppError> {
        let pool = unsafe { get_client() };

        let rec = sqlx::query_as!(
            UserList,
            r#"
                SELECT id, email, is_staff FROM "users"
                WHERE id = $1
            "#,
            user_id
        )
        .fetch_one(pool)
        .await?;

        Ok(rec)
    }

    pub async fn list() -> Result<Vec<UserList>, AppError> {
        let pool = unsafe { get_client() };
        let rows = sqlx::query_as!(UserList, r#"SELECT id, email, is_staff FROM users"#)
            .fetch_all(pool)
            .await?;

        Ok(rows)
    }
}