Adds media library cover image

This commit is contained in:
BinaryAlley committed 2025-01-03 16:01:20 +02:00
1 parent 89debaeaad
commit 666a739b1d
43 files changed
+1170 -94

No files matched your search

@@ -30,6 +30,11 @@ public class LibraryEntity : IStorageEntity, IAuditableEntity
/// </summary>
public required string Title { get; set; }
/// <summary>
/// Gets or sets the path of the image file used as the cover for the library.
/// </summary>
public string? CoverImage { get; set; }
/// <summary>
/// Gets or sets the type of the media library.
/// </summary>
@@ -17,15 +17,14 @@ public static class AddMediaLibraryRequestMapping
/// Converts <paramref name="request"/> to <see cref="AddLibraryCommand"/>.
/// </summary>
/// <param name="request">The request to be converted.</param>
/// <param name="userId">The Id of the user owning the media library.</param>
/// <returns>The converted command.</returns>
public static AddLibraryCommand ToCommand(this AddLibraryRequest request, Guid userId)
public static AddLibraryCommand ToCommand(this AddLibraryRequest request)
{
return new AddLibraryCommand(
userId,
request.Title,
request.LibraryType,
request.ContentLocations
request.ContentLocations,
request.CoverImage
);
}
}
@@ -24,6 +24,7 @@ public static class LibraryEntityMapping
repositoryEntity.Title,
repositoryEntity.LibraryType,
repositoryEntity.ContentLocations.Select(location => location.Path).ToList(),
repositoryEntity.CoverImage,
repositoryEntity.CreatedOnUtc,
repositoryEntity.UpdatedOnUtc
);
@@ -26,6 +26,7 @@ public static class LibraryMapping
LibraryType = domainEntity.LibraryType,
UserId = domainEntity.UserId.Value,
ContentLocations = domainEntity.ContentLocations.Select(path => new LibraryContentLocationEntity() { Path = path.Path }).ToList(),
CoverImage = domainEntity.CoverImage,
CreatedOnUtc = domainEntity.CreatedOnUtc,
CreatedBy = Guid.Empty,
UpdatedOnUtc = domainEntity.UpdatedOnUtc.HasValue ? domainEntity.UpdatedOnUtc : null,
@@ -15,17 +15,16 @@ public static class UpdateLibraryRequestMapping
/// Converts <paramref name="request"/> to <see cref="UpdateLibraryCommand"/>.
/// </summary>
/// <param name="request">The request to be converted.</param>
/// <param name="userId">The Id of the user making the request to update the media library.</param>
/// <returns>The converted command.</returns>
public static UpdateLibraryCommand ToCommand(this UpdateLibraryRequest request, Guid userId)
public static UpdateLibraryCommand ToCommand(this UpdateLibraryRequest request)
{
return new UpdateLibraryCommand(
request.Id,
request.UserId,
userId,
request.Title,
request.LibraryType,
request.ContentLocations
request.ContentLocations,
request.CoverImage
);
}
}
@@ -11,14 +11,14 @@ namespace Lumina.Application.Core.MediaLibrary.Management.Commands.AddLibrary;
/// <summary>
/// Command for adding a media library.
/// </summary>
/// <param name="UserId">The Id of the user owning the media library.</param>
/// <param name="Title">The title of the media library.</param>
/// <param name="LibraryType">The type of the media library.</param>
/// <param name="ContentLocations">The file system paths of the directories where the media library elements are located.</param>
/// <param name="Title">The title of the media library.</param>
/// <param name="CoverImage">The path of the image file used as the cover for the library.</param>
[DebuggerDisplay("Title: {Title}")]
public record AddLibraryCommand(
Guid UserId,
string? Title,
string? LibraryType,
string[]? ContentLocations
string[]? ContentLocations,
string? CoverImage
) : IRequest<ErrorOr<LibraryResponse>>;
@@ -4,6 +4,7 @@ using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management;
using Lumina.Application.Common.DataAccess.Repositories.MediaLibrary;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.Application.Common.Errors;
using Lumina.Application.Common.Infrastructure.Authentication;
using Lumina.Application.Common.Mapping.MediaLibrary.Management;
using Lumina.Contracts.Responses.MediaLibrary.Management;
using Lumina.Domain.Common.Enums.MediaLibrary;
@@ -22,14 +23,17 @@ namespace Lumina.Application.Core.MediaLibrary.Management.Commands.AddLibrary;
public class AddLibraryCommandHandler : IRequestHandler<AddLibraryCommand, ErrorOr<LibraryResponse>>
{
private readonly IUnitOfWork _unitOfWork;
private readonly ICurrentUserService _currentUserService;
/// <summary>
/// Initializes a new instance of the <see cref="AddLibraryCommandHandler"/> class.
/// </summary>
/// <param name="unitOfWork">Injected unit of work for interacting with the data access layer repositories.</param>
public AddLibraryCommandHandler(IUnitOfWork unitOfWork)
/// <param name="currentUserService">Injected service to retrieve the current user information.</param>
public AddLibraryCommandHandler(IUnitOfWork unitOfWork, ICurrentUserService currentUserService)
{
_unitOfWork = unitOfWork;
_currentUserService = currentUserService;
}
/// <summary>
@@ -44,10 +48,11 @@ public class AddLibraryCommandHandler : IRequestHandler<AddLibraryCommand, Error
{
// create a domain library object
ErrorOr<Library> createLibraryResult = Library.Create(
request.UserId,
_currentUserService.UserId!.Value,
request.Title!,
Enum.Parse<LibraryType>(request.LibraryType!),
request.ContentLocations!
request.ContentLocations!,
request.CoverImage
);
if (createLibraryResult.IsError)
@@ -17,10 +17,6 @@ public class AddLibraryCommandValidator : AbstractValidator<AddLibraryCommand>
/// </summary>
public AddLibraryCommandValidator()
{
RuleFor(x => x.UserId)
.NotEmpty().WithMessage(Errors.Users.UserIdCannotBeEmpty.Description)
.Must(id => id != Guid.Empty).WithMessage(Errors.Users.UserIdCannotBeEmpty.Description);
RuleFor(x => x.LibraryType)
.NotNull().WithMessage(Errors.Library.LibraryTypeCannotBeNull.Description)
.Must(x => Enum.TryParse<LibraryType>(x, out _))
@@ -1,6 +1,5 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Domain.Common.Enums.MediaLibrary;
using Lumina.Contracts.Responses.MediaLibrary.Management;
using Mediator;
using System;
@@ -14,16 +13,16 @@ namespace Lumina.Application.Core.MediaLibrary.Management.Commands.UpdateLibrary
/// </summary>
/// <param name="Id">The Id of the media library.</param>
/// <param name="OwnerId">The Id of the user owning the media library.</param>
/// <param name="CurrentUserId">The Id of the user making the request to update the media library.</param>
/// <param name="Title">The title of the media library.</param>
/// <param name="LibraryType">The type of the media library.</param>
/// <param name="ContentLocations">The file system paths of the directories where the media library elements are located.</param>
/// <param name="Title">The title of the media library.</param>
/// <param name="CoverImage">The path of the image file used as the cover for the library.</param>
[DebuggerDisplay("Title: {Title}")]
public record UpdateLibraryCommand(
Guid Id,
Guid OwnerId,
Guid CurrentUserId,
string? Title,
string? LibraryType,
string[]? ContentLocations
string[]? ContentLocations,
string? CoverImage
) : IRequest<ErrorOr<LibraryResponse>>;
@@ -3,6 +3,8 @@ using ErrorOr;
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management;
using Lumina.Application.Common.DataAccess.Repositories.MediaLibrary;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.Application.Common.Infrastructure.Authentication;
using Lumina.Application.Common.Infrastructure.Authorization;
using Lumina.Application.Common.Mapping.MediaLibrary.Management;
using Lumina.Contracts.Responses.MediaLibrary.Management;
using Lumina.Domain.Common.Enums.MediaLibrary;
@@ -24,13 +26,19 @@ namespace Lumina.Application.Core.MediaLibrary.Management.Commands.UpdateLibrary
public class UpdateLibraryCommandHandler : IRequestHandler<UpdateLibraryCommand, ErrorOr<LibraryResponse>>
{
private readonly IUnitOfWork _unitOfWork;
private readonly ICurrentUserService _currentUserService;
private readonly IAuthorizationService _authorizationService;
/// <summary>
/// Initializes a new instance of the <see cref="UpdateLibraryCommandHandler"/> class.
/// </summary>
/// <param name="authorizationService">Injected service for authorization related functionality.</param>
/// <param name="currentUserService">Injected service to retrieve the current user information.</param>
/// <param name="unitOfWork">Injected unit of work for interacting with the data access layer repositories.</param>
public UpdateLibraryCommandHandler(IUnitOfWork unitOfWork)
public UpdateLibraryCommandHandler(IAuthorizationService authorizationService, ICurrentUserService currentUserService, IUnitOfWork unitOfWork)
{
_authorizationService = authorizationService;
_currentUserService = currentUserService;
_unitOfWork = unitOfWork;
}
@@ -52,7 +60,7 @@ public class UpdateLibraryCommandHandler : IRequestHandler<UpdateLibraryCommand,
else if (getLibraryResult.Value is null)
return DomainErrors.Library.LibraryNotFound;
// if the user that made the request is not an Admin or is not the owner of the library, they do not have the right to update it
if (getLibraryResult.Value.UserId != request.CurrentUserId) // TODO: after implementing the Admin role, add that permission check here too
if (getLibraryResult.Value.UserId != _currentUserService.UserId || !await _authorizationService.IsInRoleAsync(_currentUserService.UserId!.Value, "Admin", cancellationToken))
return ApplicationErrors.Authorization.NotAuthorized;
// create a domain library object
@@ -61,7 +69,8 @@ public class UpdateLibraryCommandHandler : IRequestHandler<UpdateLibraryCommand,
request.OwnerId,
request.Title!,
Enum.Parse<LibraryType>(request.LibraryType!),
request.ContentLocations!
request.ContentLocations!,
request.CoverImage
);
if (createLibraryResult.IsError)
return createLibraryResult.Errors;
@@ -21,10 +21,6 @@ public class UpdateLibraryCommandValidator : AbstractValidator<UpdateLibraryComm
.NotEmpty().WithMessage(Errors.Library.LibraryIdCannotBeEmpty.Description)
.Must(id => id != Guid.Empty).WithMessage(Errors.Library.LibraryIdCannotBeEmpty.Description);
RuleFor(x => x.CurrentUserId)
.NotEmpty().WithMessage(Errors.Users.UserIdCannotBeEmpty.Description)
.Must(id => id != Guid.Empty).WithMessage(Errors.Users.UserIdCannotBeEmpty.Description);
RuleFor(x => x.OwnerId)
.NotEmpty().WithMessage(Errors.Users.UserIdCannotBeEmpty.Description)
.Must(id => id != Guid.Empty).WithMessage(Errors.Users.UserIdCannotBeEmpty.Description);