use async_trait::async_trait;
use anyhow::Result;
use crate::domain::entity::{Attribute, AttributeType, CatalogStatus};
#[derive(Debug, Clone, Default)]
pub struct AttributePaginationParams {
pub page: u32,
pub per_page: u32,
}
impl AttributePaginationParams {
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 AttributePaginatedResult {
pub data: Vec<Attribute>,
pub total: u64,
pub page: u32,
pub per_page: u32,
}
#[derive(Debug, Clone, Default)]
pub struct AttributeFilter {
pub code: Option<String>,
pub name: Option<String>,
pub attribute_type: Option<AttributeType>,
pub status: Option<CatalogStatus>,
}
impl AttributeFilter {
pub fn has_filters(&self) -> bool {
self.code.is_some() || self.name.is_some() || self.attribute_type.is_some() || self.status.is_some()
}
}
#[async_trait]
pub trait AttributeRepository: Send + Sync {
async fn save(&self, entity: &Attribute) -> Result<Attribute>;
async fn find_by_id(&self, id: &str) -> Result<Option<Attribute>>;
async fn find_all(&self) -> Result<Vec<Attribute>>;
async fn update(&self, id: &str, entity: &Attribute) -> Result<Option<Attribute>>;
async fn delete(&self, id: &str) -> Result<bool>;
async fn list(&self, params: AttributePaginationParams) -> Result<AttributePaginatedResult>;
async fn list_with_filters(&self, params: AttributePaginationParams, filters: AttributeFilter) -> Result<AttributePaginatedResult>;
async fn count(&self) -> Result<u64>;
async fn count_with_filters(&self, filters: AttributeFilter) -> 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<Attribute>>;
async fn list_deleted(&self, params: AttributePaginationParams) -> Result<AttributePaginatedResult>;
async fn empty_trash(&self) -> Result<u64>;
async fn bulk_save(&self, entities: &[Attribute]) -> Result<Vec<Attribute>>;
async fn bulk_delete(&self, ids: &[&str]) -> Result<u64>;
}