GeneIT/geneit_backend/src/models.rs

47 lines
1.1 KiB
Rust
Raw Normal View History

2023-05-24 14:19:46 +00:00
use crate::schema::users;
use diesel::prelude::*;
/// User ID holder
#[derive(Debug, Clone, Copy, serde::Serialize, serde::Deserialize)]
2023-05-30 13:12:58 +00:00
pub struct UserID(pub i32);
2023-05-24 14:19:46 +00:00
#[derive(Queryable, Debug)]
pub struct User {
2023-05-30 13:12:58 +00:00
pub id: i32,
pub name: String,
pub email: String,
pub password: Option<String>,
pub reset_password_token: Option<String>,
pub time_create: i64,
pub time_gen_reset_token: i64,
pub time_activate: i64,
pub active: bool,
pub admin: bool,
2023-05-24 14:19:46 +00:00
}
impl User {
pub fn id(&self) -> UserID {
UserID(self.id)
}
2023-05-31 13:17:00 +00:00
pub fn check_password(&self, password: &str) -> bool {
self.password
.as_deref()
.map(|hash| {
bcrypt::verify(password, hash).unwrap_or_else(|e| {
log::error!("Failed to validate password! {}", e);
false
})
})
.unwrap_or(false)
}
2023-05-24 14:19:46 +00:00
}
#[derive(Insertable)]
#[diesel(table_name = users)]
pub struct NewUser<'a> {
pub name: &'a str,
pub email: &'a str,
pub time_create: i64,
}