use crate::error::Result;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Debug, Clone, Serialize, Deserialize, sqlx::FromRow)]
pub struct User {
pub id: Option<Uuid>,
pub email: String,
pub name: String,
pub password_hash: String,
pub created_at: Option<chrono::DateTime<chrono::Utc>>,
pub updated_at: Option<chrono::DateTime<chrono::Utc>>,
}
impl User {
pub fn new(email: String, name: String, password_hash: String) -> Self {
Self {
id: None,
email,
name,
password_hash,
created_at: None,
updated_at: None,
}
}
}
#[async_trait]
pub trait Repository: Send + Sync {
type Entity;
type Id;
async fn find_by_id(&self, id: Self::Id) -> Result<Option<Self::Entity>>;
async fn list(&self, limit: i64, offset: i64) -> Result<Vec<Self::Entity>>;
async fn count(&self) -> Result<i64>;
}
#[async_trait]
pub trait CrudRepository: Repository {
async fn insert(&self, entity: &Self::Entity) -> Result<()>;
async fn update(&self, entity: &Self::Entity) -> Result<()>;
async fn delete(&self, id: Self::Id) -> Result<()>;
}
#[async_trait]
pub trait FindById {
type Entity;
type Id;
async fn exists(&self, id: Self::Id) -> Result<bool>;
async fn not_found(&self, id: Self::Id) -> Result<Option<Self::Entity>>;
}
#[async_trait]
pub trait List {
type Entity;
async fn list_all(&self) -> Result<Vec<Self::Entity>>;
async fn list_with_filter(&self, filter: &str) -> Result<Vec<Self::Entity>>;
}