GeneIT/geneit_backend/src/models.rs

119 lines
2.6 KiB
Rust
Raw Normal View History

2023-06-16 15:51:51 +00:00
use crate::schema::{families, memberships, users};
2023-05-24 14:19:46 +00:00
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
2023-05-31 13:52:49 +00:00
#[derive(Queryable, Debug, serde::Serialize)]
2023-05-24 14:19:46 +00:00
pub struct User {
2023-06-21 15:01:52 +00:00
id: i32,
2023-05-30 13:12:58 +00:00
pub name: String,
pub email: String,
2023-05-31 13:52:49 +00:00
#[serde(skip_serializing)]
2023-05-30 13:12:58 +00:00
pub password: Option<String>,
2023-06-06 07:47:52 +00:00
pub time_create: i64,
2023-05-31 13:52:49 +00:00
#[serde(skip_serializing)]
2023-05-30 13:12:58 +00:00
pub reset_password_token: Option<String>,
2023-05-31 13:52:49 +00:00
#[serde(skip_serializing)]
2023-05-30 13:12:58 +00:00
pub time_gen_reset_token: i64,
2023-06-06 07:47:52 +00:00
#[serde(skip_serializing)]
pub delete_account_token: Option<String>,
#[serde(skip_serializing)]
pub time_gen_delete_account_token: i64,
2023-05-30 13:12:58 +00:00
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,
}
2023-06-16 15:51:51 +00:00
/// Family ID holder
#[derive(Debug, Clone, Copy, serde::Serialize, serde::Deserialize)]
pub struct FamilyID(pub i32);
#[derive(Queryable, Debug, serde::Serialize)]
pub struct Family {
2023-06-21 15:01:52 +00:00
id: i32,
2023-06-16 15:51:51 +00:00
pub time_create: i64,
pub name: String,
pub invitation_code: String,
}
impl Family {
pub fn id(&self) -> FamilyID {
FamilyID(self.id)
}
}
#[derive(Insertable)]
#[diesel(table_name = families)]
pub struct NewFamily<'a> {
pub name: &'a str,
pub invitation_code: String,
pub time_create: i64,
}
#[derive(Queryable, Debug, serde::Serialize)]
pub struct Membership {
2023-06-21 15:01:52 +00:00
user_id: i32,
family_id: i32,
2023-06-21 14:36:46 +00:00
pub time_create: i64,
pub is_admin: bool,
}
impl Membership {
pub fn user_id(&self) -> UserID {
UserID(self.user_id)
}
pub fn family_id(&self) -> FamilyID {
FamilyID(self.family_id)
}
2023-06-16 15:51:51 +00:00
}
#[derive(Insertable)]
#[diesel(table_name = memberships)]
pub struct NewMembership {
pub user_id: i32,
pub family_id: i32,
pub time_create: i64,
pub is_admin: bool,
}
2023-06-20 16:55:14 +00:00
#[derive(Queryable, Debug, serde::Serialize)]
pub struct FamilyMembership {
user_id: i32,
family_id: i32,
name: String,
time_create: i64,
2023-06-21 15:35:07 +00:00
pub is_admin: bool,
2023-06-20 16:55:14 +00:00
invitation_code: String,
2023-06-21 15:35:07 +00:00
pub count_members: i64,
pub count_admins: i64,
2023-06-20 16:55:14 +00:00
}