Overhaul refactor. Still things in progress

This commit is contained in:
2025-04-05 22:42:13 -04:00
parent 310d1eaad8
commit 769762dfa7
133 changed files with 119890 additions and 8784 deletions

View File

@@ -1,11 +1,12 @@
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use diesel::prelude::*;
use crate::{
auth::hash,
db::{connection, schema::users},
error::ApiResult,
};
use crate::{auth::hash, error::ApiResult};
use crate::db;
pub const ADMIN_ROLE: &str = "ADMIN";
pub const USER_ROLE: &str = "USER";
const TABLE_NAME: &str = "users";
/**
* RegisterRequest
@@ -20,18 +21,15 @@ pub struct RegisterRequest {
impl RegisterRequest {
pub fn to_user(self) -> ApiResult<User> {
let hash = hash(&self.password)?;
let password_hash = hash(&self.password)?;
Ok(User {
email: self.email.to_lowercase(),
hash,
role: "user".to_string(),
password_hash,
role: USER_ROLE.to_string(),
first_name: self.first_name,
last_name: self.last_name,
updated_at: chrono::Utc::now().naive_utc(),
created_at: chrono::Utc::now().naive_utc(),
profile_picture: None,
favorites: vec![],
verified: false,
updated_at: Utc::now(),
created_at: Utc::now(),
})
}
}
@@ -54,7 +52,6 @@ pub struct UserResponse {
pub role: String,
pub first_name: String,
pub last_name: String,
pub profile_picture: Option<String>,
}
impl From<User> for UserResponse {
@@ -64,45 +61,69 @@ impl From<User> for UserResponse {
role: user.role,
first_name: user.first_name,
last_name: user.last_name,
profile_picture: user.profile_picture,
}
}
}
/**
* User
*/
#[derive(Debug, Insertable, AsChangeset, Queryable, QueryableByName, Serialize, Deserialize)]
#[diesel(table_name = users)]
#[derive(Serialize, Deserialize, sqlx::FromRow, Debug)]
pub struct User {
pub email: String,
pub hash: String,
pub password_hash: String,
pub role: String,
pub first_name: String,
pub last_name: String,
pub updated_at: chrono::NaiveDateTime,
pub created_at: chrono::NaiveDateTime,
pub profile_picture: Option<String>,
pub favorites: Vec<String>,
pub verified: bool,
pub updated_at: DateTime<Utc>,
pub created_at: DateTime<Utc>,
}
impl User {
pub fn get_by_email(email: &str) -> ApiResult<User> {
let mut conn = connection()?;
// Check if the user exists by email, case insensitive
pub async fn select(email: &str) -> Option<Self> {
let pool = db::pool();
let user: Option<Self> = sqlx::query_as::<_, Self>(&format!(
r#"
SELECT * FROM {} WHERE email = LOWER($1)
"#,
TABLE_NAME
))
.bind(email)
.fetch_optional(pool)
.await
.unwrap_or_else(|err| {
log::error!("Unable to find user '{}': {}", email, err);
None
});
let user = users::table
.filter(users::email.eq(email.to_lowercase()))
.first(&mut conn)?;
Ok(user)
user
}
pub fn insert(user: Self) -> ApiResult<User> {
let mut conn = connection()?;
let user = diesel::insert_into(users::table)
.values(user)
.get_result(&mut conn)?;
pub async fn insert(&self) -> ApiResult<User> {
let pool = db::pool();
let user: User = sqlx::query_as::<_, Self>(&format!(
r#"
INSERT INTO {} (
email,
password_hash,
role,
first_name,
last_name,
created_at,
updated_at
)
VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING *
"#,
TABLE_NAME,
))
.bind(&self.email)
.bind(&self.password_hash)
.bind(&self.role)
.bind(&self.first_name)
.bind(&self.last_name)
.bind(self.created_at)
.bind(self.updated_at)
.fetch_one(pool)
.await?;
Ok(user)
}
}