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
use deadpool_postgres::Client;
use rocket::serde::{Deserialize, Serialize};
use tokio_pg_mapper::FromTokioPostgresRow;
use crate::{
errors::AppError,
models::{
full_user::{FullUser, RealFullUser},
user::{RealUserWithId, UserWithId},
user_settings::UserSettingsWithId,
},
};
#[derive(Deserialize, Serialize)]
#[serde(crate = "rocket::serde")]
pub struct LoginInfo<'r> {
pub username: &'r str,
pub password: &'r str,
}
pub async fn get_user<'r>(
client: &Client,
user_info: &LoginInfo<'r>,
) -> Result<RealFullUser, AppError> {
let _stmt = include_str!("../sql/users/get.sql");
let _stmt = _stmt.replace("$username", user_info.username);
let stmt = client.prepare(&_stmt).await.unwrap();
let res = client
.query(&stmt, &[])
.await?
.iter()
.map(|row| RealUserWithId::from_row_ref(row).unwrap())
.collect::<Vec<RealUserWithId>>()
.pop();
let user_find = res.clone();
let user: RealUserWithId;
match user_find {
Some(user_) => user = user_,
None => return Err(AppError::NotFound),
}
let _stmt2 = include_str!("../sql/users/get_settings.sql");
let _stmt2 = _stmt2.replace("$user_id", user.id.to_string().as_str());
let stmt2 = client.prepare(&_stmt2).await.unwrap();
let res2 = client
.query(&stmt2, &[])
.await?
.iter()
.map(|row| UserSettingsWithId::from_row_ref(row).unwrap())
.collect::<Vec<UserSettingsWithId>>()
.pop();
if res2.is_some() {
let full = FullUser::new(
UserWithId {
id: user.id,
first_name: user.first_name.as_str(),
last_name: user.last_name.as_str(),
email: user.email.as_str(),
username: user.username.as_str(),
password: user.password.as_str(),
},
res2.unwrap(),
);
return Ok(RealFullUser::from(full));
} else {
return Err(AppError::NotFound);
}
}