Refactors the get books and get books lite slices across the stack

This commit is contained in:
BinaryAlley committed 2026-09-13 04:16:59 -07:00
1 parent 001146eefc
commit 1d35cd01db
44 files changed
+1681 -418

No files matched your search

@@ -1,6 +1,5 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.CQRS;
using Lumina.Application.Common.DTO.Filtering;
using Lumina.Application.Common.DTO.Pagination;
using Lumina.Domain.SharedKernel.Common.Enums.Common;
#endregion
@@ -10,13 +9,15 @@ namespace Lumina.Application.Core.MediaLibrary.WrittenContentLibrary.BookLibrary
/// <summary>
/// Query for getting all the books of a media library.
/// </summary>
/// <param name="LibraryId">The unique identifier of the media library whose books are retrieved, taken from the route.</param>
/// <param name="PaginationData">The object containing the requested pagination data.</param>
/// <param name="Filter">The object containing the criteria used to filter the results.</param>
/// <param name="SearchTerm">The search term used to filter results.</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>
public record GetBooksQuery(
string? LibraryId,
PaginationDataDto? PaginationData,
LibraryFilterDto Filter,
string? SearchTerm,
string? SortBy,
SortOrder? SortOrder
) : IQuery;
@@ -16,6 +16,7 @@ using Lumina.Application.Common.Infrastructure.Authorization;
using Lumina.Application.Common.Infrastructure.Authorization.Policies.LibraryOwnership;
using Lumina.Application.Common.Infrastructure.Validation;
using Lumina.Application.Common.Errors;
using Lumina.Application.Common.DTO.Filtering;
using Lumina.Application.Common.DTO.Pagination;
#endregion
@@ -60,25 +61,34 @@ public class GetBooksQueryHandler : IQueryHandler<GetBooksQuery, Result<Paginate
if (validationResult.Count > 0)
return validationResult;
// an authenticated request must always carry a user identity
// An authenticated request must always carry a user identity.
Guid? currentUserId = _currentUserService.UserId;
if (currentUserId is null)
return Errors.Authorization.NotAuthorized;
Guid userId = currentUserId.Value;
// admins can see all libraries; for everyone else, only the libraries they own
// The validator guarantees that the library id of the route is a non-empty Guid before this point.
Guid libraryId = Guid.Parse(query.LibraryId!);
// Admins can see all libraries; for everyone else, only the libraries they own.
bool canAccessLibrary = await _authorizationService.EvaluatePolicyAsync<ILibraryOwnershipPolicy>(
userId, new LibraryOwnershipPolicyContext(query.Filter.LibraryId), cancellationToken).ConfigureAwait(false);
userId, new LibraryOwnershipPolicyContext(libraryId), cancellationToken).ConfigureAwait(false);
if (!canAccessLibrary)
return Errors.Authorization.NotAuthorized;
// get all books of the media library from the book repository
Result<PaginatedResultDto<BookEntity>> getBooksResult = await _unitOfWork.BookRepository.GetPaginatedAsync(
// Get all books of the media library from the book repository.
LibraryFilterDto libraryFilter = new()
{
LibraryId = libraryId,
SearchTerm = query.SearchTerm
};
Result<PaginatedResultDto<BookEntity>> getBooksResult = await _unitOfWork.BookRepository.GetAllAsync(
query.PaginationData,
query.SortBy,
query.SortOrder,
query.Filter,
cancellationToken).ConfigureAwait(false);
libraryFilter,
shouldTrackEntities: false,
cancellationToken: cancellationToken).ConfigureAwait(false);
return getBooksResult.Match(value => Result.From(value.ToResponses()), errors => errors);
}
}
@@ -17,10 +17,14 @@ public class GetBooksQueryValidator : AbstractValidator<GetBooksQuery>
/// </summary>
public GetBooksQueryValidator()
{
RuleFor(query => query.Filter.LibraryId)
// Validates the identifier of the media library whose books are retrieved, taken from the route.
RuleFor(query => query.LibraryId)
.NotEmpty()
.WithError(Errors.Library.LibraryIdCannotBeEmpty)
.Must(id => id != Guid.Empty)
.WithError(Errors.Library.LibraryIdCannotBeEmpty);
RuleFor(query => query.LibraryId)
.Must(libraryId => Guid.TryParse(libraryId, out Guid parsedLibraryId) && parsedLibraryId != Guid.Empty)
.When(query => query.LibraryId is not null && query.LibraryId.Length > 0)
.WithError(Errors.Library.LibraryIdCannotBeEmpty);
}
}
@@ -1,6 +1,5 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.CQRS;
using Lumina.Application.Common.DTO.Filtering;
using Lumina.Application.Common.DTO.Pagination;
using Lumina.Domain.SharedKernel.Common.Enums.Common;
#endregion
@@ -10,13 +9,19 @@ namespace Lumina.Application.Core.MediaLibrary.WrittenContentLibrary.BookLibrary
/// <summary>
/// Query for getting the lightweight details of all the books of a media library.
/// </summary>
/// <param name="LibraryId">The unique identifier of the media library whose books are retrieved, taken from the route.</param>
/// <param name="PaginationData">The object containing the requested pagination data.</param>
/// <param name="Filter">The object containing the criteria used to filter the results.</param>
/// <param name="SearchTerm">The search term used to filter results.</param>
/// <param name="FilterAlphaKey">The alpha key used to filter results by the first character of their title.</param>
/// <param name="ShouldIgnoreThePrefixForAlphaPicker">Whether the leading "The " prefix of a title should be ignored when computing the alpha key, or not.</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>
public record GetBooksLiteQuery(
string? LibraryId,
PaginationDataDto? PaginationData,
LibraryFilterDto Filter,
string? SearchTerm,
string? FilterAlphaKey,
bool ShouldIgnoreThePrefixForAlphaPicker,
string? SortBy,
SortOrder? SortOrder
) : IQuery;
@@ -1,7 +1,7 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Domain.Common.Primitives;
using Lumina.Application.Common.CQRS;
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary;
using Lumina.Application.Common.DTO.MediaLibrary.WrittenContentLibrary.BookLibrary;
using Lumina.Application.Common.DataAccess.Repositories.Books;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.Application.Common.Mapping.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
@@ -16,6 +16,7 @@ using Lumina.Application.Common.Infrastructure.Authorization;
using Lumina.Application.Common.Infrastructure.Authorization.Policies.LibraryOwnership;
using Lumina.Application.Common.Infrastructure.Validation;
using Lumina.Application.Common.Errors;
using Lumina.Application.Common.DTO.Filtering;
using Lumina.Application.Common.DTO.Pagination;
#endregion
@@ -60,25 +61,37 @@ public class GetBooksLiteQueryHandler : IQueryHandler<GetBooksLiteQuery, Result<
if (validationResult.Count > 0)
return validationResult;
// an authenticated request must always carry a user identity
// An authenticated request must always carry a user identity.
Guid? currentUserId = _currentUserService.UserId;
if (currentUserId is null)
return Errors.Authorization.NotAuthorized;
Guid userId = currentUserId.Value;
// admins can see all libraries; for everyone else, only the libraries they own
// The validator guarantees that the library id of the route is a non-empty Guid before this point.
Guid libraryId = Guid.Parse(query.LibraryId!);
// Admins can see all libraries; for everyone else, only the libraries they own.
bool canAccessLibrary = await _authorizationService.EvaluatePolicyAsync<ILibraryOwnershipPolicy>(
userId, new LibraryOwnershipPolicyContext(query.Filter.LibraryId), cancellationToken).ConfigureAwait(false);
userId, new LibraryOwnershipPolicyContext(libraryId), cancellationToken).ConfigureAwait(false);
if (!canAccessLibrary)
return Errors.Authorization.NotAuthorized;
// get all books of the media library from the book repository
Result<PaginatedResultDto<BookEntity>> getBooksResult = await _unitOfWork.BookRepository.GetPaginatedAsync(
LibraryFilterDto libraryFilter = new()
{
LibraryId = libraryId,
SearchTerm = query.SearchTerm,
FilterAlphaKey = query.FilterAlphaKey,
ShouldIgnoreThePrefixForAlphaPicker = query.ShouldIgnoreThePrefixForAlphaPicker
};
// Get the lightweight read models of the books of the media library from the book repository, projecting only the fields needed for a grid or a list.
Result<PaginatedResultDto<BookLiteRow>> getBooksResult = await _unitOfWork.BookRepository.GetAllLiteAsync(
query.PaginationData,
query.SortBy,
query.SortOrder,
query.Filter,
cancellationToken).ConfigureAwait(false);
return getBooksResult.Match(value => Result.From(value.ToLiteResponses()), errors => errors);
libraryFilter,
shouldTrackEntities: false,
cancellationToken: cancellationToken).ConfigureAwait(false);
return getBooksResult.Match(value => Result.From(value.ToResponses()), errors => errors);
}
}
@@ -18,15 +18,19 @@ public class GetBooksLiteQueryValidator : AbstractValidator<GetBooksLiteQuery>
/// </summary>
public GetBooksLiteQueryValidator()
{
RuleFor(query => query.Filter.LibraryId)
// Validates the identifier of the media library whose books are retrieved, taken from the route.
RuleFor(query => query.LibraryId)
.NotEmpty()
.WithError(Errors.Library.LibraryIdCannotBeEmpty)
.Must(id => id != Guid.Empty)
.WithError(Errors.Library.LibraryIdCannotBeEmpty);
RuleFor(query => query.LibraryId)
.Must(libraryId => Guid.TryParse(libraryId, out Guid parsedLibraryId) && parsedLibraryId != Guid.Empty)
.When(query => query.LibraryId is not null && query.LibraryId.Length > 0)
.WithError(Errors.Library.LibraryIdCannotBeEmpty);
// the alpha key must be exactly one of the three picker bucket kinds, so that the filter specification can
// derive the key of a title unambiguously: no filter, a number bucket, a symbol bucket, or a single ASCII letter
RuleFor(query => query.Filter.FilterAlphaKey)
RuleFor(query => query.FilterAlphaKey)
.Must(alphaKey => alphaKey is null
|| alphaKey == LibraryItemAlphaKeys.NUMBER
|| alphaKey == LibraryItemAlphaKeys.SYMBOL