Adds pagination and filtering to the API

This commit is contained in:
BinaryAlley committed 2026-08-16 03:10:47 -07:00
1 parent ad984910a2
commit ce4ebf36bc
59 files changed
+4423 -242

No files matched your search

@@ -0,0 +1,12 @@
namespace Lumina.Application.Common.DTO.Filtering;
/// <summary>
/// Data transfer object that defines common filter properties used when querying paginated data. Implementations may add additional properties specific to their module or domain.
/// </summary>
public record BaseFilterDto
{
/// <summary>
/// The search term used to filter results.
/// </summary>
public string? SearchTerm { get; init; }
}
@@ -0,0 +1,16 @@
#region ========================================================================= USING =====================================================================================
using System;
#endregion
namespace Lumina.Application.Common.DTO.Filtering;
/// <summary>
/// Data transfer object that represents the parameters required to filter by library data.
/// </summary>
public record LibraryFilterDto : BaseFilterDto
{
/// <summary>
/// The Id of the media library used to filter data.
/// </summary>
public required Guid LibraryId { get; init; }
}
@@ -0,0 +1,37 @@
#region ========================================================================= USING =====================================================================================
using System.Collections.Generic;
#endregion
namespace Lumina.Application.Common.DTO.Pagination;
/// <summary>
/// Represents a paginated result containing a subset of data and pagination metadata for the current page.
/// </summary>
/// <typeparam name="TData">The type of the data items contained in the paginated result.</typeparam>
public sealed record PaginatedResultDto<TData>
{
/// <summary>
/// Gets the collection of data items associated with this instance.
/// </summary>
public required IReadOnlyList<TData> Data { get; init; }
/// <summary>
/// Gets the current page number in a paginated collection.
/// </summary>
public required int CurrentPage { get; init; }
/// <summary>
/// Gets or sets the number of items to include per page in paginated results.
/// </summary>
public required int PerPage { get; init; }
/// <summary>
/// Gets the total number of elements available in the paginated result set.
/// </summary>
public required int Count { get; init; }
/// <summary>
/// Gets the total number of pages based on elements available in the result set and per page.
/// </summary>
public required int NumberOfPages { get; init; }
}
@@ -0,0 +1,38 @@
#region ========================================================================= USING =====================================================================================
using System;
#endregion
namespace Lumina.Application.Common.DTO.Pagination;
/// <summary>
/// Data transfer object for pagination metadata.
/// </summary>
public sealed record PaginationDataDto
{
private int perPage = 200;
private int currentPage = 1;
/// <summary>
/// Gets the current page number in a paginated collection.
/// </summary>
public int CurrentPage
{
get { return currentPage; }
set
{
currentPage = Math.Max(value, 1);
}
}
/// <summary>
/// Gets or sets the number of items to include per page in paginated results.
/// </summary>
public int PerPage
{
get { return perPage; }
set
{
perPage = Math.Max(value, 1);
}
}
}
@@ -18,7 +18,8 @@ public interface IBookRepository : IRepository<BookEntity>,
IInsertRepositoryAction<BookEntity>,
IUpdateRepositoryAction<BookEntity>,
IGetByIdRepositoryAction<BookEntity, Guid>,
IGetAllRepositoryAction<BookEntity>
IGetAllRepositoryAction<BookEntity>,
IGetPaginatedRepositoryAction<BookEntity>
{
/// <summary>
/// Gets all the books of the media library identified by <paramref name="libraryId"/>.
@@ -0,0 +1,30 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Entities.Common;
using Lumina.Application.Common.DTO.Filtering;
using Lumina.Application.Common.DTO.Pagination;
using Lumina.Domain.Common.Primitives;
using Lumina.Domain.SharedKernel.Common.Enums.Common;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.Common.DataAccess.Repositories.Common.Actions;
/// <summary>
/// Interface defining the "get paginated" action for interacting with a generic persistence medium.
/// </summary>
/// <typeparam name="TModel">The type used as a result for the "get paginated" action. It should implement <see cref="IStorageEntity"/>.</typeparam>
public interface IGetPaginatedRepositoryAction<TModel> where TModel : IStorageEntity
{
/// <summary>
/// Gets paginated data of type <typeparamref name="TModel"/> from the storage medium.
/// </summary>
/// <typeparam name="TFilter">The type of the filter carrying the criteria used to filter the results.</typeparam>
/// <param name="paginationData">The pagination data that includes current page and number of items per page to retrieve. If <see langword="null"/>, all matching data is returned.</param>
/// <param name="sortBy">The name of the field by which to sort the results.</param>
/// <param name="sortOrder">The direction in which to sort the results.</param>
/// <param name="filterModel">The model containing parameters used to filter results.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="Result{TValue}"/> containing either a paginated result of type <typeparamref name="TModel"/>, or an error.</returns>
Task<Result<PaginatedResultDto<TModel>>> GetPaginatedAsync<TFilter>(PaginationDataDto? paginationData, string? sortBy = null, SortOrder? sortOrder = null, TFilter? filterModel = null, CancellationToken cancellationToken = default) where TFilter : BaseFilterDto;
}
@@ -1,19 +1,20 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Domain.Common.Primitives;
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary;
using Lumina.Application.Common.DTO.Pagination;
using Lumina.Application.Common.Mapping.Common.Metadata;
using Lumina.Application.Common.Mapping.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
using Lumina.Application.Common.Mapping.MediaLibrary.WrittenContentLibrary.BookLibrary.Common;
using Lumina.Contracts.DTO.Common;
using Lumina.Contracts.DTO.MediaLibrary.WrittenContentLibrary;
using Lumina.Contracts.Responses.Common;
using Lumina.Contracts.Responses.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
using Lumina.Domain.SharedKernel.Common.Enums.BookLibrary;
using Lumina.Domain.Common.Primitives;
using Lumina.Domain.Common.ValueObjects.Metadata;
using Lumina.Domain.Core.BoundedContexts.WrittenContentLibraryBoundedContext.BookLibraryAggregate;
using Lumina.Domain.Core.BoundedContexts.WrittenContentLibraryBoundedContext.BookLibraryAggregate.Entities;
using Lumina.Domain.Core.BoundedContexts.WrittenContentLibraryBoundedContext.BookLibraryAggregate.ValueObjects;
using Lumina.Domain.Core.BoundedContexts.WrittenContentLibraryBoundedContext.ExternalIdentifiers.LibraryManagementBoundedContext.LibraryAggregate;
using Lumina.Domain.SharedKernel.Common.Enums.BookLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -225,8 +226,68 @@ public static class BookEntityMapping
/// </summary>
/// <param name="repositoryEntities">The repository entities to be converted.</param>
/// <returns>The converted reponses.</returns>
public static IEnumerable<BookResponse> ToResponses(this IEnumerable<BookEntity> repositoryEntities)
public static IReadOnlyList<BookResponse> ToResponses(this IEnumerable<BookEntity> repositoryEntities)
{
return repositoryEntities.Select(repositoryEntity => repositoryEntity.ToResponse());
return [.. repositoryEntities.Select(repositoryEntity => repositoryEntity.ToResponse())];
}
/// <summary>
/// Converts <paramref name="repositoryEntities"/> to a paginated collection of <see cref="BookResponse"/>.
/// </summary>
/// <param name="repositoryEntities">The paginated repository entities to be converted.</param>
/// <returns>The converted paginated responses.</returns>
public static PaginatedResponse<BookResponse> ToResponses(this PaginatedResultDto<BookEntity> repositoryEntities)
{
return new PaginatedResponse<BookResponse>
{
Data = repositoryEntities.Data.ToResponses(),
CurrentPage = repositoryEntities.CurrentPage,
PerPage = repositoryEntities.PerPage,
Count = repositoryEntities.Count,
NumberOfPages = repositoryEntities.NumberOfPages
};
}
/// <summary>
/// Converts <paramref name="repositoryEntity"/> to <see cref="BookLiteResponse"/>.
/// </summary>
/// <param name="repositoryEntity">The repository entity to be converted.</param>
/// <returns>The converted response entity.</returns>
public static BookLiteResponse ToLiteResponse(this BookEntity repositoryEntity)
{
return new BookLiteResponse(
repositoryEntity.Id,
repositoryEntity.Title,
repositoryEntity.ReReleaseYear ?? repositoryEntity.OriginalReleaseYear,
// TODO: populate from the book cover when cover support is implemented
null
);
}
/// <summary>
/// Converts <paramref name="repositoryEntities"/> to a collection of <see cref="BookLiteResponse"/>.
/// </summary>
/// <param name="repositoryEntities">The repository entities to be converted.</param>
/// <returns>The converted reponses.</returns>
public static IReadOnlyList<BookLiteResponse> ToLiteResponses(this IEnumerable<BookEntity> repositoryEntities)
{
return [.. repositoryEntities.Select(repositoryEntity => repositoryEntity.ToLiteResponse())];
}
/// <summary>
/// Converts <paramref name="repositoryEntities"/> to a paginated collection of <see cref="BookLiteResponse"/>.
/// </summary>
/// <param name="repositoryEntities">The paginated repository entities to be converted.</param>
/// <returns>The converted paginated responses.</returns>
public static PaginatedResponse<BookLiteResponse> ToLiteResponses(this PaginatedResultDto<BookEntity> repositoryEntities)
{
return new PaginatedResponse<BookLiteResponse>
{
Data = repositoryEntities.Data.ToLiteResponses(),
CurrentPage = repositoryEntities.CurrentPage,
PerPage = repositoryEntities.PerPage,
Count = repositoryEntities.Count,
NumberOfPages = repositoryEntities.NumberOfPages
};
}
}
@@ -0,0 +1,38 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DTO.Filtering;
using Lumina.Application.Common.DTO.Pagination;
using Lumina.Application.Core.MediaLibrary.WrittenContentLibrary.BooksLibrary.Books.Queries.GetBooksLite;
using Lumina.Contracts.Requests.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
#endregion
namespace Lumina.Application.Common.Mapping.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
/// <summary>
/// Extension methods for converting <see cref="GetBooksLiteRequest"/>.
/// </summary>
public static class GetBooksLiteRequestMapping
{
/// <summary>
/// Converts <paramref name="request"/> to <see cref="GetBooksLiteQuery"/>.
/// </summary>
/// <param name="request">The request to be converted.</param>
/// <returns>The converted query.</returns>
public static GetBooksLiteQuery ToQuery(this GetBooksLiteRequest request)
{
PaginationDataDto? paginationData = null;
if (request.CurrentPage is not null || request.PerPage is not null)
paginationData = new PaginationDataDto
{
CurrentPage = request.CurrentPage ?? 1,
PerPage = request.PerPage ?? 200
};
LibraryFilterDto libraryFilter = new()
{
LibraryId = request.LibraryId,
SearchTerm = request.SearchTerm
};
return new GetBooksLiteQuery(paginationData, libraryFilter, request.SortBy, request.SortOrder);
}
}
@@ -1,4 +1,6 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DTO.Filtering;
using Lumina.Application.Common.DTO.Pagination;
using Lumina.Application.Core.MediaLibrary.WrittenContentLibrary.BooksLibrary.Books.Queries.GetBooks;
using Lumina.Contracts.Requests.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
#endregion
@@ -17,6 +19,20 @@ public static class GetBooksRequestMapping
/// <returns>The converted query.</returns>
public static GetBooksQuery ToQuery(this GetBooksRequest request)
{
return new GetBooksQuery(request.LibraryId);
PaginationDataDto? paginationData = null;
if (request.CurrentPage is not null || request.PerPage is not null)
paginationData = new PaginationDataDto
{
CurrentPage = request.CurrentPage ?? 1,
PerPage = request.PerPage ?? 200
};
LibraryFilterDto libraryFilter = new()
{
LibraryId = request.LibraryId,
SearchTerm = request.SearchTerm
};
return new GetBooksQuery(paginationData, libraryFilter, request.SortBy, request.SortOrder);
}
}
@@ -0,0 +1,45 @@
#region ========================================================================= USING =====================================================================================
using System;
using System.Linq.Expressions;
#endregion
namespace Lumina.Application.Common.Specifications;
/// <summary>
/// Represents a filter specification that combines two filter specifications using a logical AND operation.
/// </summary>
/// <typeparam name="T">The type of entity to which the filter specification is applied.</typeparam>
internal class AndFilterSpecification<T> : FilterSpecification<T>
{
private readonly FilterSpecification<T> _left;
private readonly FilterSpecification<T> _right;
/// <summary>
/// Overload C-tor.
/// </summary>
/// <param name="left">The first filter specification to combine.</param>
/// <param name="right">The second filter specification to combine.</param>
public AndFilterSpecification(FilterSpecification<T> left, FilterSpecification<T> right)
{
_left = left;
_right = right;
}
/// <summary>
/// Creates a LINQ expression that represents the predicate defined by the current specification.
/// </summary>
/// <returns>An expression tree that can be used to evaluate whether an object of type <typeparamref name="T"/> satisfies the specification criteria.</returns>
public override Expression<Func<T, bool>> ToExpression()
{
Expression<Func<T, bool>> leftExpression = _left.ToExpression();
Expression<Func<T, bool>> rightExpression = _right.ToExpression();
ParameterExpression parameter = Expression.Parameter(typeof(T));
BinaryExpression combined = Expression.AndAlso(
Expression.Invoke(leftExpression, parameter),
Expression.Invoke(rightExpression, parameter)
);
return Expression.Lambda<Func<T, bool>>(combined, parameter);
}
}
@@ -0,0 +1,50 @@
#region ========================================================================= USING =====================================================================================
using System;
using System.Linq.Expressions;
#endregion
namespace Lumina.Application.Common.Specifications;
/// <summary>
/// Represents the abstract base for a filtering specification, enabling the encapsulation of business query logic as reusable objects.
/// </summary>
/// <typeparam name="T">The type of entity to which the specification applies.</typeparam>
public abstract class FilterSpecification<T>
{
/// <summary>
/// Creates a LINQ expression that represents the predicate defined by the current specification.
/// </summary>
/// <returns>An expression tree that can be used to evaluate whether an object of type <typeparamref name="T"/> satisfies the specification criteria.</returns>
public abstract Expression<Func<T, bool>> ToExpression();
/// <summary>
/// Determines whether the specified entity satisfies the criteria defined by the specification.
/// </summary>
/// <param name="entity">The entity to evaluate against the specification criteria.</param>
/// <returns><see langword="true"/> if the entity satisfies the specification; <see langword="false"/> otherwise.</returns>
public bool IsSatisfiedBy(T entity)
{
Func<T, bool> predicate = ToExpression().Compile();
return predicate(entity);
}
/// <summary>
/// Combines the current filter specification with another specification using a logical AND operation.
/// </summary>
/// <param name="specification">The filter specification to combine with the current specification.</param>
/// <returns>A new filter specification that is satisfied if both the current specification and the specified specification is satisfied.</returns>
public FilterSpecification<T> And(FilterSpecification<T> specification)
{
return new AndFilterSpecification<T>(this, specification);
}
/// <summary>
/// Combines the current filter specification with another specification using a logical OR operation.
/// </summary>
/// <param name="specification">The filter specification to combine with the current specification.</param>
/// <returns>A new filter specification that is satisfied if either the current specification or the specified specification is satisfied.</returns>
public FilterSpecification<T> Or(FilterSpecification<T> specification)
{
return new OrFilterSpecification<T>(this, specification);
}
}
@@ -0,0 +1,45 @@
#region ========================================================================= USING =====================================================================================
using System;
using System.Linq.Expressions;
#endregion
namespace Lumina.Application.Common.Specifications;
/// <summary>
/// Represents a filter specification that combines two filter specifications using a logical OR operation.
/// </summary>
/// <typeparam name="T">The type of entity to which the filter specification is applied.</typeparam>
internal class OrFilterSpecification<T> : FilterSpecification<T>
{
private readonly FilterSpecification<T> _left;
private readonly FilterSpecification<T> _right;
/// <summary>
/// Overload C-tor.
/// </summary>
/// <param name="left">The first filter specification to combine.</param>
/// <param name="right">The second filter specification to combine.</param>
public OrFilterSpecification(FilterSpecification<T> left, FilterSpecification<T> right)
{
_left = left;
_right = right;
}
/// <summary>
/// Creates a LINQ expression that represents the predicate defined by the current specification.
/// </summary>
/// <returns>An expression tree that can be used to evaluate whether an object of type <typeparamref name="T"/> satisfies the specification criteria.</returns>
public override Expression<Func<T, bool>> ToExpression()
{
Expression<Func<T, bool>> leftExpression = _left.ToExpression();
Expression<Func<T, bool>> rightExpression = _right.ToExpression();
ParameterExpression parameter = Expression.Parameter(typeof(T));
BinaryExpression combined = Expression.OrElse(
Expression.Invoke(leftExpression, parameter),
Expression.Invoke(rightExpression, parameter)
);
return Expression.Lambda<Func<T, bool>>(combined, parameter);
}
}