pre-metrics
This commit is contained in:
+32
-29
@@ -1,75 +1,70 @@
|
||||
use crate::auth::password;
|
||||
use crate::models::user;
|
||||
use crate::repositories::RepositoryContext;
|
||||
use crate::utils::password;
|
||||
use crate::repositories::{AnyResult, RepositoryContext};
|
||||
use sea_orm::{
|
||||
ActiveModelTrait, ColumnTrait, DbErr, EntityTrait, IntoActiveModel, PaginatorTrait,
|
||||
QueryFilter, Set,
|
||||
ActiveModelTrait, ColumnTrait, EntityTrait, IntoActiveModel, PaginatorTrait, QueryFilter, Set,
|
||||
};
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Clone)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct UserRepository {
|
||||
pub context: Arc<RepositoryContext>,
|
||||
}
|
||||
|
||||
impl UserRepository {
|
||||
pub async fn get_all(&self) -> Result<Vec<user::Model>, DbErr> {
|
||||
user::Entity::find().all(&self.context.db).await
|
||||
pub async fn get_all(&self) -> AnyResult<Vec<user::Model>> {
|
||||
Ok(user::Entity::find().all(&self.context.db).await?)
|
||||
}
|
||||
|
||||
pub async fn count(&self) -> Result<u64, DbErr> {
|
||||
user::Entity::find().count(&self.context.db).await
|
||||
pub async fn count(&self) -> AnyResult<u64> {
|
||||
Ok(user::Entity::find().count(&self.context.db).await?)
|
||||
}
|
||||
|
||||
pub async fn get_by_id(&self, id: uuid::Uuid) -> Result<Option<user::Model>, DbErr> {
|
||||
user::Entity::find_by_id(id).one(&self.context.db).await
|
||||
pub async fn get_by_id(&self, id: uuid::Uuid) -> AnyResult<Option<user::Model>> {
|
||||
Ok(user::Entity::find_by_id(id).one(&self.context.db).await?)
|
||||
}
|
||||
|
||||
pub async fn get_by_username(&self, username: String) -> Result<Option<user::Model>, DbErr> {
|
||||
user::Entity::find()
|
||||
pub async fn get_by_username(&self, username: String) -> AnyResult<Option<user::Model>> {
|
||||
Ok(user::Entity::find()
|
||||
.filter(user::Column::Username.eq(username))
|
||||
.one(&self.context.db)
|
||||
.await
|
||||
.await?)
|
||||
}
|
||||
|
||||
pub async fn check_password(
|
||||
&self,
|
||||
username: String,
|
||||
password: String,
|
||||
) -> Result<user::Model, DbErr> {
|
||||
let user = self.get_by_username(username).await?;
|
||||
pub async fn check_password(&self, username: &str, password: &str) -> AnyResult<user::Model> {
|
||||
let user = self.get_by_username(username.to_string()).await?;
|
||||
if let Some(user) = user {
|
||||
let password_ok = password::verify_password(password.as_str(), user.password.as_str())
|
||||
.map_err(|_| DbErr::Custom("Password hashing failed".to_string()))?;
|
||||
let password_ok = password::verify_password(password, user.password.as_str())
|
||||
.map_err(|e| anyhow::anyhow!("Password hashing failed: {}", e))?;
|
||||
|
||||
if password_ok {
|
||||
return Ok(user);
|
||||
}
|
||||
}
|
||||
Err(DbErr::Custom("Invalid username or password".to_string()))
|
||||
Err(anyhow::anyhow!("Invalid username or password"))
|
||||
}
|
||||
|
||||
pub async fn update(&self, active: user::ActiveModel) -> Result<user::Model, DbErr> {
|
||||
pub async fn update(&self, active: user::ActiveModel) -> AnyResult<user::Model> {
|
||||
let user = active.update(&self.context.db).await?;
|
||||
self.context.events.emit("user_updated", user.clone());
|
||||
Ok(user)
|
||||
}
|
||||
|
||||
pub async fn create(&self, active: user::ActiveModel) -> Result<user::Model, DbErr> {
|
||||
pub async fn create(&self, active: user::ActiveModel) -> AnyResult<user::Model> {
|
||||
let user = active.insert(&self.context.db).await?;
|
||||
self.context.events.emit("user_created", user.clone());
|
||||
Ok(user)
|
||||
}
|
||||
|
||||
pub async fn set_password(&self, id: uuid::Uuid, password: String) -> Result<(), DbErr> {
|
||||
pub async fn set_password(&self, id: uuid::Uuid, password: String) -> AnyResult<()> {
|
||||
let user = self
|
||||
.get_by_id(id)
|
||||
.await?
|
||||
.ok_or_else(|| DbErr::Custom("User not found".to_string()))?;
|
||||
.ok_or_else(|| anyhow::anyhow!("User not found"))?;
|
||||
|
||||
let mut active = user.into_active_model();
|
||||
let password = password::hash_password(&password)
|
||||
.map_err(|_| DbErr::Custom("Password hashing failed".to_string()))?;
|
||||
.map_err(|e| anyhow::anyhow!("Password hashing failed: {}", e))?;
|
||||
active.password = Set(password);
|
||||
|
||||
let user = self.update(active).await?;
|
||||
@@ -78,11 +73,19 @@ impl UserRepository {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn delete(&self, id: uuid::Uuid) -> Result<(), DbErr> {
|
||||
pub async fn delete(&self, id: uuid::Uuid) -> AnyResult<()> {
|
||||
user::Entity::delete_by_id(id)
|
||||
.exec(&self.context.db)
|
||||
.await?;
|
||||
self.context.events.emit("user_deleted", id);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn username_exists(&self, username: &str) -> AnyResult<bool> {
|
||||
Ok(user::Entity::find()
|
||||
.filter(user::Column::Username.eq(username))
|
||||
.count(&self.context.db)
|
||||
.await?
|
||||
> 0)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user