mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 11:11:10 +00:00
Adds pagination and filtering to the API
This commit is contained in:
1 parent
ad984910a2
commit
ce4ebf36bc
59 files changed
+4423
-242
No files matched your search
+11
-3
@@ -1,6 +1,8 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using System;
|
||||
using Lumina.Application.Common.DTO.Filtering;
|
||||
using Lumina.Application.Common.DTO.Pagination;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.Common;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Core.MediaLibrary.WrittenContentLibrary.BooksLibrary.Books.Queries.GetBooks;
|
||||
@@ -8,7 +10,13 @@ namespace Lumina.Application.Core.MediaLibrary.WrittenContentLibrary.BooksLibrar
|
||||
/// <summary>
|
||||
/// Query for getting all the books of a media library.
|
||||
/// </summary>
|
||||
/// <param name="LibraryId">The Id of the media library whose books are retrieved.</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="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(
|
||||
Guid LibraryId
|
||||
PaginationDataDto? PaginationData,
|
||||
LibraryFilterDto Filter,
|
||||
string? SortBy,
|
||||
SortOrder? SortOrder
|
||||
) : IQuery;
|
||||
+39
-5
@@ -9,6 +9,13 @@ using Lumina.Contracts.Responses.MediaLibrary.WrittenContentLibrary.BookLibrary.
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Lumina.Contracts.Responses.Common;
|
||||
using Lumina.Application.Common.Infrastructure.Authentication;
|
||||
using Lumina.Application.Common.Infrastructure.Authorization;
|
||||
using Lumina.Application.Common.Infrastructure.Validation;
|
||||
using Lumina.Application.Common.Errors;
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management;
|
||||
using Lumina.Application.Common.DTO.Pagination;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Core.MediaLibrary.WrittenContentLibrary.BooksLibrary.Books.Queries.GetBooks;
|
||||
@@ -16,17 +23,26 @@ namespace Lumina.Application.Core.MediaLibrary.WrittenContentLibrary.BooksLibrar
|
||||
/// <summary>
|
||||
/// Handler for the query to get all books.
|
||||
/// </summary>
|
||||
public class GetBooksQueryHandler : IQueryHandler<GetBooksQuery, Result<IEnumerable<BookResponse>>>
|
||||
public class GetBooksQueryHandler : IQueryHandler<GetBooksQuery, Result<PaginatedResponse<BookResponse>>>
|
||||
{
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly IAuthorizationService _authorizationService;
|
||||
private readonly ICurrentUserService _currentUserService;
|
||||
private readonly IValidator<GetBooksQuery> _validator;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="GetBooksQueryHandler"/> class.
|
||||
/// </summary>
|
||||
/// <param name="unitOfWork">Injected unit of work for interacting with the data access layer repositories.</param>
|
||||
public GetBooksQueryHandler(IUnitOfWork unitOfWork)
|
||||
/// <param name="authorizationService">Injected service for authorization related functionality.</param>
|
||||
/// <param name="currentUserService">Injected service to retrieve the current user information.</param>
|
||||
/// <param name="validator">Injected validator for application validation rules.</param>
|
||||
public GetBooksQueryHandler(IUnitOfWork unitOfWork, IAuthorizationService authorizationService, ICurrentUserService currentUserService, IValidator<GetBooksQuery> validator)
|
||||
{
|
||||
_unitOfWork = unitOfWork;
|
||||
_authorizationService = authorizationService;
|
||||
_currentUserService = currentUserService;
|
||||
_validator = validator;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -37,10 +53,28 @@ public class GetBooksQueryHandler : IQueryHandler<GetBooksQuery, Result<IEnumera
|
||||
/// <returns>
|
||||
/// An <see cref="Result{TValue}"/> containing either a collection of <see cref="BookResponse"/>, or an error message.
|
||||
/// </returns>
|
||||
public async Task<Result<IEnumerable<BookResponse>>> HandleAsync(GetBooksQuery query, CancellationToken cancellationToken)
|
||||
public async Task<Result<PaginatedResponse<BookResponse>>> HandleAsync(GetBooksQuery query, CancellationToken cancellationToken)
|
||||
{
|
||||
List<Error> validationResult = _validator.Validate(query);
|
||||
if (validationResult.Count > 0)
|
||||
return validationResult;
|
||||
|
||||
// check if the user has the rights to access the library they are requesting (admins can see all libraries)
|
||||
bool isAdmin = await _authorizationService.IsInRoleAsync(_currentUserService.UserId!.Value, "Admin", cancellationToken).ConfigureAwait(false);
|
||||
if (!isAdmin) // if its not an admin, then it must own the library
|
||||
{
|
||||
Result<LibraryEntity?> getLibraryResult = await _unitOfWork.LibraryRepository.GetByIdAsync(query.Filter.LibraryId, cancellationToken).ConfigureAwait(false);
|
||||
if (getLibraryResult.IsFailure || getLibraryResult.Value?.UserId != _currentUserService.UserId)
|
||||
return Errors.Authorization.NotAuthorized;
|
||||
}
|
||||
|
||||
// get all books of the media library from the book repository
|
||||
Result<IEnumerable<BookEntity>> getBooksResult = await _unitOfWork.BookRepository.GetByLibraryIdAsync(query.LibraryId, cancellationToken).ConfigureAwait(false);
|
||||
return getBooksResult.Match(result => Result.From(getBooksResult.Value.ToResponses()), errors => errors);
|
||||
Result<PaginatedResultDto<BookEntity>> getBooksResult = await _unitOfWork.BookRepository.GetPaginatedAsync(
|
||||
query.PaginationData,
|
||||
query.SortBy,
|
||||
query.SortOrder,
|
||||
query.Filter,
|
||||
cancellationToken).ConfigureAwait(false);
|
||||
return getBooksResult.Match(value => Result.From(value.ToResponses()), errors => errors);
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.Infrastructure.Validation;
|
||||
using Lumina.Application.Common.Utilities;
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using System;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Core.MediaLibrary.WrittenContentLibrary.BooksLibrary.Books.Queries.GetBooks;
|
||||
|
||||
/// <summary>
|
||||
/// Validates the needed validation rules for <see cref="GetBooksQuery"/>.
|
||||
/// </summary>
|
||||
public class GetBooksQueryValidator : AbstractValidator<GetBooksQuery>
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="GetBooksQueryValidator"/> class.
|
||||
/// </summary>
|
||||
public GetBooksQueryValidator()
|
||||
{
|
||||
RuleFor(query => query.Filter.LibraryId)
|
||||
.NotEmpty()
|
||||
.WithError(Errors.Library.LibraryIdCannotBeEmpty)
|
||||
.Must(id => id != Guid.Empty)
|
||||
.WithError(Errors.Library.LibraryIdCannotBeEmpty);
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
#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
|
||||
|
||||
namespace Lumina.Application.Core.MediaLibrary.WrittenContentLibrary.BooksLibrary.Books.Queries.GetBooksLite;
|
||||
|
||||
/// <summary>
|
||||
/// Query for getting the lightweight details of all the books of a media library.
|
||||
/// </summary>
|
||||
/// <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="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(
|
||||
PaginationDataDto? PaginationData,
|
||||
LibraryFilterDto Filter,
|
||||
string? SortBy,
|
||||
SortOrder? SortOrder
|
||||
) : IQuery;
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
#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.DataAccess.Repositories.Books;
|
||||
using Lumina.Application.Common.DataAccess.UoW;
|
||||
using Lumina.Application.Common.Mapping.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
|
||||
using Lumina.Contracts.Responses.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Lumina.Contracts.Responses.Common;
|
||||
using Lumina.Application.Common.Infrastructure.Authentication;
|
||||
using Lumina.Application.Common.Infrastructure.Authorization;
|
||||
using Lumina.Application.Common.Infrastructure.Validation;
|
||||
using Lumina.Application.Common.Errors;
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management;
|
||||
using Lumina.Application.Common.DTO.Pagination;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Core.MediaLibrary.WrittenContentLibrary.BooksLibrary.Books.Queries.GetBooksLite;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for the query to get the lightweight details of all books.
|
||||
/// </summary>
|
||||
public class GetBooksLiteQueryHandler : IQueryHandler<GetBooksLiteQuery, Result<PaginatedResponse<BookLiteResponse>>>
|
||||
{
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly IAuthorizationService _authorizationService;
|
||||
private readonly ICurrentUserService _currentUserService;
|
||||
private readonly IValidator<GetBooksLiteQuery> _validator;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="GetBooksLiteQueryHandler"/> class.
|
||||
/// </summary>
|
||||
/// <param name="unitOfWork">Injected unit of work for interacting with the data access layer repositories.</param>
|
||||
/// <param name="authorizationService">Injected service for authorization related functionality.</param>
|
||||
/// <param name="currentUserService">Injected service to retrieve the current user information.</param>
|
||||
/// <param name="validator">Injected validator for application validation rules.</param>
|
||||
public GetBooksLiteQueryHandler(IUnitOfWork unitOfWork, IAuthorizationService authorizationService, ICurrentUserService currentUserService, IValidator<GetBooksLiteQuery> validator)
|
||||
{
|
||||
_unitOfWork = unitOfWork;
|
||||
_authorizationService = authorizationService;
|
||||
_currentUserService = currentUserService;
|
||||
_validator = validator;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles the query to get the lightweight details of all books.
|
||||
/// </summary>
|
||||
/// <param name="query">The query to be handled.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>
|
||||
/// An <see cref="Result{TValue}"/> containing either a collection of <see cref="BookLiteResponse"/>, or an error message.
|
||||
/// </returns>
|
||||
public async Task<Result<PaginatedResponse<BookLiteResponse>>> HandleAsync(GetBooksLiteQuery query, CancellationToken cancellationToken)
|
||||
{
|
||||
List<Error> validationResult = _validator.Validate(query);
|
||||
if (validationResult.Count > 0)
|
||||
return validationResult;
|
||||
|
||||
// check if the user has the rights to access the library they are requesting (admins can see all libraries)
|
||||
bool isAdmin = await _authorizationService.IsInRoleAsync(_currentUserService.UserId!.Value, "Admin", cancellationToken).ConfigureAwait(false);
|
||||
if (!isAdmin) // if its not an admin, then it must own the library
|
||||
{
|
||||
Result<LibraryEntity?> getLibraryResult = await _unitOfWork.LibraryRepository.GetByIdAsync(query.Filter.LibraryId, cancellationToken).ConfigureAwait(false);
|
||||
if (getLibraryResult.IsFailure || getLibraryResult.Value?.UserId != _currentUserService.UserId)
|
||||
return Errors.Authorization.NotAuthorized;
|
||||
}
|
||||
|
||||
// get all books of the media library from the book repository
|
||||
Result<PaginatedResultDto<BookEntity>> getBooksResult = await _unitOfWork.BookRepository.GetPaginatedAsync(
|
||||
query.PaginationData,
|
||||
query.SortBy,
|
||||
query.SortOrder,
|
||||
query.Filter,
|
||||
cancellationToken).ConfigureAwait(false);
|
||||
return getBooksResult.Match(value => Result.From(value.ToLiteResponses()), errors => errors);
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.Infrastructure.Validation;
|
||||
using Lumina.Application.Common.Utilities;
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using System;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Core.MediaLibrary.WrittenContentLibrary.BooksLibrary.Books.Queries.GetBooksLite;
|
||||
|
||||
/// <summary>
|
||||
/// Validates the needed validation rules for <see cref="GetBooksLiteQuery"/>.
|
||||
/// </summary>
|
||||
public class GetBooksLiteQueryValidator : AbstractValidator<GetBooksLiteQuery>
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="GetBooksLiteQueryValidator"/> class.
|
||||
/// </summary>
|
||||
public GetBooksLiteQueryValidator()
|
||||
{
|
||||
RuleFor(query => query.Filter.LibraryId)
|
||||
.NotEmpty()
|
||||
.WithError(Errors.Library.LibraryIdCannotBeEmpty)
|
||||
.Must(id => id != Guid.Empty)
|
||||
.WithError(Errors.Library.LibraryIdCannotBeEmpty);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user