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
|
use crate::db::get_client;
use crate::errors::AppError;
use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize)]
pub struct User {
id: i32,
email: String,
password: String,
}
impl User {
pub async fn create(user: User) -> Result<i32, AppError> {
let pool = unsafe { get_client() };
let rec = sqlx::query!(
r#"
INSERT INTO users (email, password)
VALUES ( $1, $2 )
RETURNING id
"#,
user.email,
user.password
)
.fetch_one(pool)
.await?;
Ok(rec.id)
}
pub async fn list() -> Result<Vec<User>, AppError> {
let pool = unsafe { get_client() };
let rows = sqlx::query_as!(User, r#"SELECT id, email, password FROM users"#)
.fetch_all(pool)
.await?;
Ok(rows)
}
}
|