backbone-bucket 0.2.0

Bucket Bounded Context: File Storage Module for Backbone Framework
Documentation
//! List UserQuota use case
//!
//! Generated by backbone-schema. Do not edit manually.

use std::sync::Arc;

use anyhow::Result;
use async_trait::async_trait;

use super::UseCase;
use crate::domain::entity::UserQuota;
use crate::infrastructure::persistence::UserQuotaRepository;
use backbone_orm::repository::PaginationParams;

/// Input for listing UserQuotas
#[derive(Debug, Clone, Default)]
pub struct ListUserQuotaInput {
    pub page: Option<u32>,
    pub per_page: Option<u32>,
    // <<< CUSTOM FILTERS START >>>
    // Add custom filter fields here
    // <<< CUSTOM FILTERS END >>>
}

/// Output containing list of UserQuotas
#[derive(Debug, Clone)]
pub struct ListUserQuotaOutput {
    pub user_quotas: Vec<UserQuota>,
    pub total: u64,
    pub page: u32,
    pub per_page: u32,
    pub total_pages: u32,
}

/// Use case for listing UserQuotas with pagination
pub struct ListUserQuotaUseCase {
    repository: Arc<UserQuotaRepository>,
}

impl ListUserQuotaUseCase {
    pub fn new(repository: Arc<UserQuotaRepository>) -> Self {
        Self { repository }
    }
}

#[async_trait]
impl UseCase for ListUserQuotaUseCase {
    type Input = ListUserQuotaInput;
    type Output = ListUserQuotaOutput;

    async fn execute(&self, input: Self::Input) -> Result<Self::Output> {
        let page = input.page.unwrap_or(1);
        let per_page = input.per_page.unwrap_or(20);

        let pagination = PaginationParams::new(page, per_page);

        // <<< CUSTOM QUERY START >>>
        // Add custom query/filter logic here
        // <<< CUSTOM QUERY END >>>

        let result = self.repository.find_all(pagination).await?;

        let total_pages = ((result.total as f64) / (per_page as f64)).ceil() as u32;

        Ok(ListUserQuotaOutput {
            user_quotas: result.items,
            total: result.total,
            page,
            per_page,
            total_pages,
        })
    }
}