use postgres_native_tls::MakeTlsConnector;
use bb8::PooledConnection;
use bb8_postgres::PostgresConnectionManager;
use serde::Deserialize;
use serde::Serialize;
#[derive(Serialize, Deserialize, Clone)]
pub struct ModelUser {
pub id: i32,
pub email: String,
pub password: String,
pub state: i32,
pub verified: i32,
pub role: String,
}
pub async fn get_user_by_id(
tracking_label: &str,
id: i32,
conn: &PooledConnection<'_, PostgresConnectionManager<MakeTlsConnector>>,
) -> Result<ModelUser, String> {
let query = format!(
"SELECT \
users.id, \
users.email, \
users.password, \
users.state, \
users.verified, \
users.role \
FROM \
users \
WHERE \
users.id = {id} \
LIMIT 1;"
);
let stmt = conn.prepare(&query).await.unwrap();
match conn.query(&stmt, &[]).await {
Ok(query_result) => {
if let Some(row) = query_result.first() {
let id: i32 = row.try_get("id").unwrap();
let email: String = row.try_get("email").unwrap();
let password: String = row.try_get("email").unwrap();
let state: i32 = row.try_get("state").unwrap();
let verified: i32 = row.try_get("verified").unwrap();
let role: String = row.try_get("role").unwrap();
return Ok(ModelUser {
id,
email,
password,
state,
verified,
role,
});
}
Err(format!(
"{tracking_label} - \
failed to find any user with id={id}"
))
}
Err(e) => Err(format!(
"{tracking_label} - \
failed to find user by id={id} \
with err='{e}'"
)),
}
}