kegani 0.1.1

A developer-friendly, ergonomic, production-ready Rust web framework
Documentation
//! Repository traits for Kegani
//!
//! Defines standard repository operations.

use crate::error::Result;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

/// User entity
#[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 {
    /// Create a new 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,
        }
    }
}

/// Repository trait for CRUD operations
#[async_trait]
pub trait Repository: Send + Sync {
    /// Entity type
    type Entity;

    /// ID type
    type Id;

    /// Find by ID
    async fn find_by_id(&self, id: Self::Id) -> Result<Option<Self::Entity>>;

    /// List with pagination
    async fn list(&self, limit: i64, offset: i64) -> Result<Vec<Self::Entity>>;

    /// Count total
    async fn count(&self) -> Result<i64>;
}

/// CRUD repository trait
#[async_trait]
pub trait CrudRepository: Repository {
    /// Insert a new entity
    async fn insert(&self, entity: &Self::Entity) -> Result<()>;

    /// Update an existing entity
    async fn update(&self, entity: &Self::Entity) -> Result<()>;

    /// Delete by ID
    async fn delete(&self, id: Self::Id) -> Result<()>;
}

/// FindById extension trait
#[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>>;
}

/// List extension trait
#[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>>;
}