use async_trait::async_trait;
use anyhow::Result;
use uuid::Uuid;
use crate::domain::entity::{Uom, CatalogStatus, UomType};
#[derive(Debug, Clone, Default)]
pub struct UomPaginationParams {
pub page: u32,
pub per_page: u32,
}
impl UomPaginationParams {
pub fn new(page: u32, per_page: u32) -> Self {
Self { page, per_page }
}
pub fn offset(&self) -> u64 {
((self.page.saturating_sub(1)) * self.per_page) as u64
}
pub fn limit(&self) -> u64 {
self.per_page as u64
}
}
#[derive(Debug, Clone)]
pub struct UomPaginatedResult {
pub data: Vec<Uom>,
pub total: u64,
pub page: u32,
pub per_page: u32,
}
#[derive(Debug, Clone, Default)]
pub struct UomFilter {
pub code: Option<String>,
pub name: Option<String>,
pub uom_type: Option<UomType>,
pub relative_uom_id: Option<Uuid>,
pub is_protected: Option<bool>,
pub status: Option<CatalogStatus>,
}
impl UomFilter {
pub fn has_filters(&self) -> bool {
self.code.is_some() || self.name.is_some() || self.uom_type.is_some() || self.relative_uom_id.is_some() || self.is_protected.is_some() || self.status.is_some()
}
}
#[async_trait]
pub trait UomRepository: Send + Sync {
async fn save(&self, entity: &Uom) -> Result<Uom>;
async fn find_by_id(&self, id: &str) -> Result<Option<Uom>>;
async fn find_all(&self) -> Result<Vec<Uom>>;
async fn update(&self, id: &str, entity: &Uom) -> Result<Option<Uom>>;
async fn delete(&self, id: &str) -> Result<bool>;
async fn list(&self, params: UomPaginationParams) -> Result<UomPaginatedResult>;
async fn list_with_filters(&self, params: UomPaginationParams, filters: UomFilter) -> Result<UomPaginatedResult>;
async fn count(&self) -> Result<u64>;
async fn count_with_filters(&self, filters: UomFilter) -> Result<u64>;
async fn exists(&self, id: &str) -> Result<bool>;
async fn soft_delete(&self, id: &str) -> Result<bool>;
async fn restore(&self, id: &str) -> Result<Option<Uom>>;
async fn list_deleted(&self, params: UomPaginationParams) -> Result<UomPaginatedResult>;
async fn empty_trash(&self) -> Result<u64>;
async fn bulk_save(&self, entities: &[Uom]) -> Result<Vec<Uom>>;
async fn bulk_delete(&self, ids: &[&str]) -> Result<u64>;
}