54 lines
2.2 KiB
Rust
54 lines
2.2 KiB
Rust
use async_trait::async_trait;
|
|
use bson::oid::ObjectId;
|
|
use crate::models::user::User;
|
|
|
|
// Define custom error type
|
|
#[derive(Debug)]
|
|
pub enum UserError {
|
|
MongoError(mongodb::error::Error),
|
|
NotFound,
|
|
ValidationError(String),
|
|
DuplicateKey(String),
|
|
}
|
|
|
|
impl std::fmt::Display for UserError {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
match self {
|
|
UserError::MongoError(e) => write!(f, "MongoDB error: {}", e),
|
|
UserError::NotFound => write!(f, "User not found"),
|
|
UserError::ValidationError(msg) => write!(f, "Validation error: {}", msg),
|
|
UserError::DuplicateKey(field) => write!(f, "Duplicate key error: {}", field),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl std::error::Error for UserError {}
|
|
|
|
impl From<mongodb::error::Error> for UserError {
|
|
fn from(error: mongodb::error::Error) -> Self {
|
|
UserError::MongoError(error)
|
|
}
|
|
}
|
|
|
|
// Repository trait
|
|
#[async_trait]
|
|
pub trait UserRepository {
|
|
async fn create(&self, user: User) -> Result<User, UserError>;
|
|
async fn get(&self, id: ObjectId) -> Result<User, UserError>;
|
|
async fn update(&self, id: ObjectId, user: User) -> Result<User, UserError>;
|
|
async fn delete(&self, id: ObjectId) -> Result<(), UserError>;
|
|
async fn list(&self, limit: Option<i64>, skip: Option<u64>) -> Result<Vec<User>, UserError>;
|
|
async fn search(&self, query: String) -> Result<Vec<User>, UserError>;
|
|
async fn count(&self) -> Result<u64, UserError>;
|
|
async fn count_by_name(&self, name: String) -> Result<u64, UserError>;
|
|
async fn count_by_email(&self, email: String) -> Result<u64, UserError>;
|
|
async fn count_by_phone(&self, phone: String) -> Result<u64, UserError>;
|
|
async fn count_by_id(&self, id: ObjectId) -> Result<u64, UserError>;
|
|
async fn find_by_email(&self, email: String) -> Result<Option<User>, UserError>;
|
|
async fn find_by_username(&self, username: String) -> Result<Option<User>, UserError>;
|
|
async fn exists_by_email(&self, email: String) -> Result<bool, UserError>;
|
|
async fn exists_by_username(&self, username: String) -> Result<bool, UserError>;
|
|
async fn get_active_users(&self) -> Result<Vec<User>, UserError>;
|
|
async fn get_users_by_role(&self, role: String) -> Result<Vec<User>, UserError>;
|
|
}
|