use async_trait::async_trait;
use anyhow::Result;
use crate::domain::entity::{Question, EventQuestionKind};
#[derive(Debug, Clone, Default)]
pub struct QuestionPaginationParams {
pub page: u32,
pub per_page: u32,
}
impl QuestionPaginationParams {
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 QuestionPaginatedResult {
pub data: Vec<Question>,
pub total: u64,
pub page: u32,
pub per_page: u32,
}
#[derive(Debug, Clone, Default)]
pub struct QuestionFilter {
pub question: Option<String>,
pub question_kind: Option<EventQuestionKind>,
pub is_default: Option<bool>,
pub is_reusable: Option<bool>,
pub once_per_order: Option<bool>,
}
impl QuestionFilter {
pub fn has_filters(&self) -> bool {
self.question.is_some() || self.question_kind.is_some() || self.is_default.is_some() || self.is_reusable.is_some() || self.once_per_order.is_some()
}
}
#[async_trait]
pub trait QuestionRepository: Send + Sync {
async fn save(&self, entity: &Question) -> Result<Question>;
async fn find_by_id(&self, id: &str) -> Result<Option<Question>>;
async fn find_all(&self) -> Result<Vec<Question>>;
async fn update(&self, id: &str, entity: &Question) -> Result<Option<Question>>;
async fn delete(&self, id: &str) -> Result<bool>;
async fn list(&self, params: QuestionPaginationParams) -> Result<QuestionPaginatedResult>;
async fn list_with_filters(&self, params: QuestionPaginationParams, filters: QuestionFilter) -> Result<QuestionPaginatedResult>;
async fn count(&self) -> Result<u64>;
async fn count_with_filters(&self, filters: QuestionFilter) -> 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<Question>>;
async fn list_deleted(&self, params: QuestionPaginationParams) -> Result<QuestionPaginatedResult>;
async fn empty_trash(&self) -> Result<u64>;
async fn bulk_save(&self, entities: &[Question]) -> Result<Vec<Question>>;
async fn bulk_delete(&self, ids: &[&str]) -> Result<u64>;
}