adding several features
This commit is contained in:
@@ -32,14 +32,33 @@ pub struct FileDto {
|
||||
impl From<File> for FileDto {
|
||||
fn from(file: File) -> Self {
|
||||
Self {
|
||||
id: file.id,
|
||||
name: file.name,
|
||||
path: file.path.to_string_lossy().to_string(),
|
||||
size: file.size,
|
||||
mime_type: file.mime_type,
|
||||
folder_id: file.folder_id,
|
||||
created_at: file.created_at,
|
||||
modified_at: file.modified_at,
|
||||
id: file.id().to_string(),
|
||||
name: file.name().to_string(),
|
||||
path: file.path_string().to_string(),
|
||||
size: file.size(),
|
||||
mime_type: file.mime_type().to_string(),
|
||||
folder_id: file.folder_id().map(String::from),
|
||||
created_at: file.created_at(),
|
||||
modified_at: file.modified_at(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Para convertir de FileDto a File para los batch handlers
|
||||
impl From<FileDto> for File {
|
||||
fn from(dto: FileDto) -> Self {
|
||||
// Usar constructor para crear una entidad desde DTO
|
||||
// Nota: esto debe simplificarse si File tiene un constructor adecuado
|
||||
// Si no, deberías hacer la conversión de la mejor manera posible
|
||||
File::from_dto(
|
||||
dto.id,
|
||||
dto.name,
|
||||
dto.path,
|
||||
dto.size,
|
||||
dto.mime_type,
|
||||
dto.folder_id,
|
||||
dto.created_at,
|
||||
dto.modified_at
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -52,15 +52,32 @@ pub struct FolderDto {
|
||||
|
||||
impl From<Folder> for FolderDto {
|
||||
fn from(folder: Folder) -> Self {
|
||||
let is_root = folder.parent_id.is_none();
|
||||
let is_root = folder.parent_id().is_none();
|
||||
|
||||
Self {
|
||||
id: folder.id,
|
||||
name: folder.name,
|
||||
path: folder.path.to_string_lossy().to_string(),
|
||||
parent_id: folder.parent_id,
|
||||
created_at: folder.created_at,
|
||||
modified_at: folder.modified_at,
|
||||
id: folder.id().to_string(),
|
||||
name: folder.name().to_string(),
|
||||
path: folder.path_string().to_string(),
|
||||
parent_id: folder.parent_id().map(String::from),
|
||||
created_at: folder.created_at(),
|
||||
modified_at: folder.modified_at(),
|
||||
is_root,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Para convertir de FolderDto a Folder para los batch handlers
|
||||
impl From<FolderDto> for Folder {
|
||||
fn from(dto: FolderDto) -> Self {
|
||||
// Usar constructor para crear una entidad desde DTO
|
||||
// Nota: esto debe simplificarse si Folder tiene un constructor adecuado
|
||||
Folder::from_dto(
|
||||
dto.id,
|
||||
dto.name,
|
||||
dto.path,
|
||||
dto.parent_id,
|
||||
dto.created_at,
|
||||
dto.modified_at
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
pub mod file_dto;
|
||||
pub mod folder_dto;
|
||||
pub mod i18n_dto;
|
||||
pub mod pagination;
|
||||
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
/// Un DTO para representar información de paginación
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct PaginationDto {
|
||||
/// Página actual (comienza en 0)
|
||||
pub page: usize,
|
||||
/// Tamaño de página
|
||||
pub page_size: usize,
|
||||
/// Número total de elementos
|
||||
pub total_items: usize,
|
||||
/// Número total de páginas
|
||||
pub total_pages: usize,
|
||||
/// Indica si hay una página siguiente
|
||||
pub has_next: bool,
|
||||
/// Indica si hay una página anterior
|
||||
pub has_prev: bool,
|
||||
}
|
||||
|
||||
/// Un DTO para representar una solicitud de paginación
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct PaginationRequestDto {
|
||||
/// Página solicitada (comienza en 0)
|
||||
#[serde(default)]
|
||||
pub page: usize,
|
||||
/// Tamaño de página solicitado
|
||||
#[serde(default = "default_page_size")]
|
||||
pub page_size: usize,
|
||||
}
|
||||
|
||||
/// Un DTO para representar una respuesta paginada
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct PaginatedResponseDto<T> {
|
||||
/// Datos en la página actual
|
||||
pub items: Vec<T>,
|
||||
/// Información de paginación
|
||||
pub pagination: PaginationDto,
|
||||
}
|
||||
|
||||
impl Default for PaginationRequestDto {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
page: 0,
|
||||
page_size: default_page_size(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Función para establecer el tamaño de página por defecto
|
||||
fn default_page_size() -> usize {
|
||||
100 // Por defecto, 100 items por página
|
||||
}
|
||||
|
||||
impl PaginationRequestDto {
|
||||
/// Calcula el offset para consultas paginadas
|
||||
pub fn offset(&self) -> usize {
|
||||
self.page * self.page_size
|
||||
}
|
||||
|
||||
/// Calcula el límite para consultas paginadas
|
||||
pub fn limit(&self) -> usize {
|
||||
self.page_size
|
||||
}
|
||||
|
||||
/// Valida y ajusta los parámetros de paginación
|
||||
pub fn validate_and_adjust(&self) -> Self {
|
||||
let mut page = self.page;
|
||||
let mut page_size = self.page_size;
|
||||
|
||||
// Asegurar que la página sea al menos 0
|
||||
if page < 1 {
|
||||
page = 0;
|
||||
}
|
||||
|
||||
// Asegurar que el tamaño de página esté entre 10 y 500
|
||||
if page_size < 10 {
|
||||
page_size = 10;
|
||||
} else if page_size > 500 {
|
||||
page_size = 500;
|
||||
}
|
||||
|
||||
Self {
|
||||
page,
|
||||
page_size,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> PaginatedResponseDto<T> {
|
||||
/// Crea una nueva respuesta paginada a partir de los datos y la información de paginación
|
||||
pub fn new(
|
||||
items: Vec<T>,
|
||||
page: usize,
|
||||
page_size: usize,
|
||||
total_items: usize,
|
||||
) -> Self {
|
||||
let total_pages = if total_items == 0 {
|
||||
0
|
||||
} else {
|
||||
(total_items + page_size - 1) / page_size
|
||||
};
|
||||
|
||||
let pagination = PaginationDto {
|
||||
page,
|
||||
page_size,
|
||||
total_items,
|
||||
total_pages,
|
||||
has_next: page < total_pages - 1,
|
||||
has_prev: page > 0,
|
||||
};
|
||||
|
||||
Self {
|
||||
items,
|
||||
pagination,
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user