backbone-bucket 0.2.0

Bucket Bounded Context: File Storage Module for Backbone Framework
Documentation
//! List StoredFile 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::StoredFile;
use crate::infrastructure::persistence::StoredFileRepository;
use backbone_orm::repository::PaginationParams;

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

/// Output containing list of StoredFiles
#[derive(Debug, Clone)]
pub struct ListStoredFileOutput {
    pub stored_files: Vec<StoredFile>,
    pub total: u64,
    pub page: u32,
    pub per_page: u32,
    pub total_pages: u32,
}

/// Use case for listing StoredFiles with pagination
pub struct ListStoredFileUseCase {
    repository: Arc<StoredFileRepository>,
}

impl ListStoredFileUseCase {
    pub fn new(repository: Arc<StoredFileRepository>) -> Self {
        Self { repository }
    }
}

#[async_trait]
impl UseCase for ListStoredFileUseCase {
    type Input = ListStoredFileInput;
    type Output = ListStoredFileOutput;

    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(ListStoredFileOutput {
            stored_files: result.items,
            total: result.total,
            page,
            per_page,
            total_pages,
        })
    }
}