Replaces reflection-based repository instantiation with unit of work property accessors

This commit is contained in:
BinaryAlley committed 2026-08-14 09:03:30 -07:00
1 parent 4fa9eb6e26
commit fee14afb89
87 files changed
+541 -735

No files matched your search

@@ -22,8 +22,8 @@ namespace Lumina.Application.Core.Admin.Authorization.Permissions.Queries.GetPer
public class GetPermissionsQueryHandler : IQueryHandler<GetPermissionsQuery, Result<IEnumerable<PermissionResponse>>>
{
private readonly ICurrentUserService _currentUserService;
private readonly IUnitOfWork _unitOfWork;
private readonly IAuthorizationService _authorizationService;
private readonly IPermissionRepository _permissionRepository;
/// <summary>
/// Initializes a new instance of the <see cref="GetPermissionsQueryHandler"/> class.
@@ -35,7 +35,7 @@ public class GetPermissionsQueryHandler : IQueryHandler<GetPermissionsQuery, Res
{
_authorizationService = authorizationService;
_currentUserService = currentUserService;
_permissionRepository = unitOfWork.GetRepository<IPermissionRepository>();
_unitOfWork = unitOfWork;
}
/// <summary>
@@ -52,7 +52,7 @@ public class GetPermissionsQueryHandler : IQueryHandler<GetPermissionsQuery, Res
bool isAdmin = await _authorizationService.IsInRoleAsync(_currentUserService.UserId!.Value, "Admin", cancellationToken).ConfigureAwait(false);
if (!isAdmin)
return Errors.Authorization.NotAuthorized;
Result<IEnumerable<PermissionEntity>> getPermissionsResult = await _permissionRepository.GetAllAsync(cancellationToken).ConfigureAwait(false);
Result<IEnumerable<PermissionEntity>> getPermissionsResult = await _unitOfWork.PermissionRepository.GetAllAsync(cancellationToken).ConfigureAwait(false);
return getPermissionsResult.Match(value => Result.From(value.ToResponses()), errors => errors);
}
}
@@ -62,8 +62,6 @@ public class AddRoleCommandHandler : ICommandHandler<AddRoleCommand, Result<Role
if (!isAdmin)
return Errors.Authorization.NotAuthorized;
IRoleRepository roleRepository = _unitOfWork.GetRepository<IRoleRepository>();
// create the new role, with its permissions
RoleEntity newRole = new()
{
@@ -77,12 +75,12 @@ public class AddRoleCommandHandler : ICommandHandler<AddRoleCommand, Result<Role
})]
};
// save the new role in the repository
Result<Created> insertRoleResult = await roleRepository.InsertAsync(newRole, cancellationToken).ConfigureAwait(false);
Result<Created> insertRoleResult = await _unitOfWork.RoleRepository.InsertAsync(newRole, cancellationToken).ConfigureAwait(false);
if (insertRoleResult.IsFailure)
return insertRoleResult.Errors;
await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
// retrieve the newly saved authorization role from the persistence medium and return it
Result<RoleEntity?> getRoleResult = await roleRepository.GetByNameAsync(request.RoleName, cancellationToken).ConfigureAwait(false);
Result<RoleEntity?> getRoleResult = await _unitOfWork.RoleRepository.GetByNameAsync(request.RoleName, cancellationToken).ConfigureAwait(false);
if (getRoleResult.IsFailure)
return getRoleResult.Errors;
if (getRoleResult.Value is null)
@@ -62,8 +62,7 @@ public class DeleteRoleCommandHandler : ICommandHandler<DeleteRoleCommand, Resul
return Errors.Authorization.NotAuthorized;
// check if a role with the requested Id exists
IRoleRepository roleRepository = _unitOfWork.GetRepository<IRoleRepository>();
Result<RoleEntity?> getExistingRoleResult = await roleRepository.GetByIdAsync(command.RoleId, cancellationToken).ConfigureAwait(false);
Result<RoleEntity?> getExistingRoleResult = await _unitOfWork.RoleRepository.GetByIdAsync(command.RoleId, cancellationToken).ConfigureAwait(false);
if (getExistingRoleResult.IsFailure)
return getExistingRoleResult.Errors;
else if (getExistingRoleResult.Value is null)
@@ -71,7 +70,7 @@ public class DeleteRoleCommandHandler : ICommandHandler<DeleteRoleCommand, Resul
else if (getExistingRoleResult.Value.RoleName == "Admin")
return Errors.Authorization.AdminRoleCannotBeDeleted;
// delete the role and its permissions
Result<Deleted> deleteRoleResult = await roleRepository.DeleteByIdAsync(command.RoleId, cancellationToken).ConfigureAwait(false);
Result<Deleted> deleteRoleResult = await _unitOfWork.RoleRepository.DeleteByIdAsync(command.RoleId, cancellationToken).ConfigureAwait(false);
if (deleteRoleResult.IsFailure)
return deleteRoleResult.Errors;
await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
@@ -62,8 +62,6 @@ public class UpdateRoleCommandHandler : ICommandHandler<UpdateRoleCommand, Resul
if (!isAdmin)
return Errors.Authorization.NotAuthorized;
IRoleRepository roleRepository = _unitOfWork.GetRepository<IRoleRepository>();
// update the role and its permissions
RoleEntity newRole = new()
{
@@ -78,12 +76,12 @@ public class UpdateRoleCommandHandler : ICommandHandler<UpdateRoleCommand, Resul
}).ToList()
};
// save the updated role in the repository
Result<Updated> updateRoleResult = await roleRepository.UpdateAsync(newRole, cancellationToken).ConfigureAwait(false);
Result<Updated> updateRoleResult = await _unitOfWork.RoleRepository.UpdateAsync(newRole, cancellationToken).ConfigureAwait(false);
if (updateRoleResult.IsFailure)
return updateRoleResult.Errors;
await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
// retrieve the updated authorization role from the persistence medium and return it
Result<RoleEntity?> getRoleResult = await roleRepository.GetByIdAsync(command.RoleId, cancellationToken).ConfigureAwait(false);
Result<RoleEntity?> getRoleResult = await _unitOfWork.RoleRepository.GetByIdAsync(command.RoleId, cancellationToken).ConfigureAwait(false);
if (getRoleResult.IsFailure)
return getRoleResult.Errors;
if (getRoleResult.Value is null)
@@ -38,7 +38,7 @@ public class GetRolePermissionsQueryHandler : IQueryHandler<GetRolePermissionsQu
{
_authorizationService = authorizationService;
_currentUserService = currentUserService;
_roleRepository = unitOfWork.GetRepository<IRoleRepository>();
_roleRepository = unitOfWork.RoleRepository;
_validator = validator;
}
@@ -35,7 +35,7 @@ public class GetRolesQueryHandler : IQueryHandler<GetRolesQuery, Result<IEnumera
{
_authorizationService = authorizationService;
_currentUserService = currentUserService;
_roleRepository = unitOfWork.GetRepository<IRoleRepository>();
_roleRepository = unitOfWork.RoleRepository;
}
/// <summary>
@@ -80,8 +80,7 @@ public class SetupApplicationCommandHandler : ICommandHandler<SetupApplicationCo
return validationResult;
// check if any users already exists (admin account is only set once!)
IUserRepository userRepository = _unitOfWork.GetRepository<IUserRepository>();
Result<IEnumerable<UserEntity>> selectUsersResult = await userRepository.GetAllAsync(cancellationToken).ConfigureAwait(false);
Result<IEnumerable<UserEntity>> selectUsersResult = await _unitOfWork.UserRepository.GetAllAsync(cancellationToken).ConfigureAwait(false);
if (selectUsersResult.IsFailure)
return selectUsersResult.Errors;
else if (selectUsersResult.Value.Any())
@@ -112,7 +111,7 @@ public class SetupApplicationCommandHandler : ICommandHandler<SetupApplicationCo
user.TotpSecret = _cryptographyService.Encrypt(Convert.ToBase64String(secret));
}
// insert the user
Result<Created> insertUserResult = await userRepository.InsertAsync(user, cancellationToken).ConfigureAwait(false);
Result<Created> insertUserResult = await _unitOfWork.UserRepository.InsertAsync(user, cancellationToken).ConfigureAwait(false);
if (insertUserResult.IsFailure)
return insertUserResult.Errors;
await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
@@ -38,9 +38,8 @@ public class CheckInitializationQueryHandler : IQueryHandler<CheckInitialization
/// </returns>
public async Task<InitializationResponse> HandleAsync(CheckInitializationQuery query, CancellationToken cancellationToken)
{
IUserRepository userRepository = _unitOfWork.GetRepository<IUserRepository>();
// if the repository reports an error, or there are no users, the application has not been initialized
Result<IEnumerable<UserEntity>> selectUsersResult = await userRepository.GetAllAsync(cancellationToken);
Result<IEnumerable<UserEntity>> selectUsersResult = await _unitOfWork.UserRepository.GetAllAsync(cancellationToken);
if (!selectUsersResult.IsFailure)
return new InitializationResponse(selectUsersResult.Value.Any());
return new InitializationResponse(false);
@@ -116,18 +116,16 @@ public class AddLibraryCommandHandler : ICommandHandler<AddLibraryCommand, Resul
if (createLibraryResult.IsFailure)
return createLibraryResult.Errors;
// get a library repository
ILibraryRepository libraryRepository = _unitOfWork.GetRepository<ILibraryRepository>();
// convert the domain library entity to a repository library entity
LibraryEntity persistenceLibrary = createLibraryResult.Value.ToRepositoryEntity();
// insert the repository entity and save changes
Result<Created> insertLibraryResult = await libraryRepository.InsertAsync(persistenceLibrary, cancellationToken).ConfigureAwait(false);
Result<Created> insertLibraryResult = await _unitOfWork.LibraryRepository.InsertAsync(persistenceLibrary, cancellationToken).ConfigureAwait(false);
if (insertLibraryResult.IsFailure)
return insertLibraryResult.Errors;
await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
// retrieve the newly saved media library from the persistence medium and return it
Result<LibraryEntity?> getCreatedLibraryResult = await libraryRepository.GetByIdAsync(createLibraryResult.Value.Id.Value, cancellationToken).ConfigureAwait(false);
Result<LibraryEntity?> getCreatedLibraryResult = await _unitOfWork.LibraryRepository.GetByIdAsync(createLibraryResult.Value.Id.Value, cancellationToken).ConfigureAwait(false);
if (getCreatedLibraryResult.IsFailure)
return getCreatedLibraryResult.Errors;
if (getCreatedLibraryResult.Value is null)
@@ -56,10 +56,8 @@ public class CancelLibrariesScanCommandHandler : ICommandHandler<CancelLibraries
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
public async Task<Result<Success>> HandleAsync(CancelLibrariesScanCommand command, CancellationToken cancellationToken)
{
ILibraryScanRepository libraryScanRepository = _unitOfWork.GetRepository<ILibraryScanRepository>();
// get the running library scans of the current user from the repository
Result<IEnumerable<LibraryScanEntity>> getRunningLibraryScansResult = await libraryScanRepository.GetRunningScansAsync(cancellationToken).ConfigureAwait(false);
Result<IEnumerable<LibraryScanEntity>> getRunningLibraryScansResult = await _unitOfWork.LibraryScanRepository.GetRunningScansAsync(cancellationToken).ConfigureAwait(false);
if (getRunningLibraryScansResult.IsFailure)
return getRunningLibraryScansResult.Errors;
@@ -88,7 +86,7 @@ public class CancelLibrariesScanCommandHandler : ICommandHandler<CancelLibraries
return cancelScanResult.Errors;
// update the status of the library scan in the repository
Result<Updated> updateLibraryScanResult = await libraryScanRepository.UpdateAsync(libraryScanDomainResult.Value.ToRepositoryEntity(), cancellationToken).ConfigureAwait(false);
Result<Updated> updateLibraryScanResult = await _unitOfWork.LibraryScanRepository.UpdateAsync(libraryScanDomainResult.Value.ToRepositoryEntity(), cancellationToken).ConfigureAwait(false);
if (updateLibraryScanResult.IsFailure)
return updateLibraryScanResult.Errors;
@@ -65,10 +65,8 @@ public class CancelLibraryScanCommandHandler : ICommandHandler<CancelLibraryScan
if (validationResult.Count > 0)
return validationResult;
ILibraryScanRepository libraryScanRepository = _unitOfWork.GetRepository<ILibraryScanRepository>();
// get the library scan from the repository
Result<LibraryScanEntity?> getLibraryScansResult = await libraryScanRepository.GetByIdAsync(command.ScanId, cancellationToken).ConfigureAwait(false);
Result<LibraryScanEntity?> getLibraryScansResult = await _unitOfWork.LibraryScanRepository.GetByIdAsync(command.ScanId, cancellationToken).ConfigureAwait(false);
if (getLibraryScansResult.IsFailure)
return getLibraryScansResult.Errors;
if (getLibraryScansResult.Value is null)
@@ -90,7 +88,7 @@ public class CancelLibraryScanCommandHandler : ICommandHandler<CancelLibraryScan
return cancelScanResult.Errors;
// update the status of the library scan in the repository
Result<Updated> updateLibraryScanResult = await libraryScanRepository.UpdateAsync(libraryScanDomainResult.Value.ToRepositoryEntity(), cancellationToken).ConfigureAwait(false);
Result<Updated> updateLibraryScanResult = await _unitOfWork.LibraryScanRepository.UpdateAsync(libraryScanDomainResult.Value.ToRepositoryEntity(), cancellationToken).ConfigureAwait(false);
if (updateLibraryScanResult.IsFailure)
return updateLibraryScanResult.Errors;
@@ -66,8 +66,7 @@ public class DeleteLibraryCommandHandler : ICommandHandler<DeleteLibraryCommand,
return validationResult;
// get the library with the specified id from the repository
ILibraryRepository libraryRepository = _unitOfWork.GetRepository<ILibraryRepository>();
Result<LibraryEntity?> getLibraryResult = await libraryRepository.GetByIdAsync(command.Id, cancellationToken).ConfigureAwait(false);
Result<LibraryEntity?> getLibraryResult = await _unitOfWork.LibraryRepository.GetByIdAsync(command.Id, cancellationToken).ConfigureAwait(false);
if (getLibraryResult.IsFailure)
return getLibraryResult.Errors;
else if (getLibraryResult.Value is null)
@@ -93,7 +92,7 @@ public class DeleteLibraryCommandHandler : ICommandHandler<DeleteLibraryCommand,
_domainEventsQueue.Enqueue(domainEvent);
// perform the deletion
Result<Deleted> deletePersistenceLibraryResult = await libraryRepository.DeleteByIdAsync(command.Id, cancellationToken).ConfigureAwait(false);
Result<Deleted> deletePersistenceLibraryResult = await _unitOfWork.LibraryRepository.DeleteByIdAsync(command.Id, cancellationToken).ConfigureAwait(false);
if (deletePersistenceLibraryResult.IsFailure)
return deletePersistenceLibraryResult.Errors;
await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
@@ -59,11 +59,8 @@ public class ScanLibrariesCommandHandler : ICommandHandler<ScanLibrariesCommand,
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
public async Task<Result<IEnumerable<MediaLibraryScanResponse>>> HandleAsync(ScanLibrariesCommand command, CancellationToken cancellationToken)
{
ILibraryRepository libraryRepository = _unitOfWork.GetRepository<ILibraryRepository>();
ILibraryScanRepository libraryScanRepository = _unitOfWork.GetRepository<ILibraryScanRepository>();
// get all media libraries that are enabled and unlocked from the persistence medium
Result<IEnumerable<LibraryEntity>> getLibrariesResult = await libraryRepository.GetAllEnabledAndUnlockedAsync(cancellationToken).ConfigureAwait(false);
Result<IEnumerable<LibraryEntity>> getLibrariesResult = await _unitOfWork.LibraryRepository.GetAllEnabledAndUnlockedAsync(cancellationToken).ConfigureAwait(false);
if (getLibrariesResult.IsFailure)
return getLibrariesResult.Errors;
@@ -85,7 +82,7 @@ public class ScanLibrariesCommandHandler : ICommandHandler<ScanLibrariesCommand,
// get the past month's scans for this library
Result<IEnumerable<LibraryScanEntity>> pastLibraryScansResult =
await libraryScanRepository.GetPastMonthScansByLibraryIdAsync(domainLibraryResult.Value.Id.Value, cancellationToken).ConfigureAwait(false);
await _unitOfWork.LibraryScanRepository.GetPastMonthScansByLibraryIdAsync(domainLibraryResult.Value.Id.Value, cancellationToken).ConfigureAwait(false);
if (pastLibraryScansResult.IsFailure)
return pastLibraryScansResult.Errors;
@@ -111,7 +108,7 @@ public class ScanLibrariesCommandHandler : ICommandHandler<ScanLibrariesCommand,
continue;
// add the library scan to the persistence medium
Result<Created> insertLibraryScanResult = await libraryScanRepository.InsertAsync(libraryScanResult.Value.ToRepositoryEntity(), cancellationToken).ConfigureAwait(false);
Result<Created> insertLibraryScanResult = await _unitOfWork.LibraryScanRepository.InsertAsync(libraryScanResult.Value.ToRepositoryEntity(), cancellationToken).ConfigureAwait(false);
if (insertLibraryScanResult.IsFailure)
return insertLibraryScanResult.Errors;
@@ -71,11 +71,8 @@ public class ScanLibraryCommandHandler : ICommandHandler<ScanLibraryCommand, Res
if (validationResult.Count > 0)
return validationResult;
ILibraryRepository libraryRepository = _unitOfWork.GetRepository<ILibraryRepository>();
ILibraryScanRepository libraryScanRepository = _unitOfWork.GetRepository<ILibraryScanRepository>();
// get the media library from the persistence medium
Result<LibraryEntity?> getLibraryResult = await libraryRepository.GetByIdAsync(command.Id, cancellationToken).ConfigureAwait(false);
Result<LibraryEntity?> getLibraryResult = await _unitOfWork.LibraryRepository.GetByIdAsync(command.Id, cancellationToken).ConfigureAwait(false);
if (getLibraryResult.IsFailure)
return getLibraryResult.Errors;
@@ -100,7 +97,7 @@ public class ScanLibraryCommandHandler : ICommandHandler<ScanLibraryCommand, Res
return domainLibraryResult.Errors;
// get the past month's scans for this library
Result<IEnumerable<LibraryScanEntity>> pastLibraryScansResult = await libraryScanRepository.GetPastMonthScansByLibraryIdAsync(command.Id, cancellationToken).ConfigureAwait(false);
Result<IEnumerable<LibraryScanEntity>> pastLibraryScansResult = await _unitOfWork.LibraryScanRepository.GetPastMonthScansByLibraryIdAsync(command.Id, cancellationToken).ConfigureAwait(false);
if (pastLibraryScansResult.IsFailure)
return pastLibraryScansResult.Errors;
@@ -124,7 +121,7 @@ public class ScanLibraryCommandHandler : ICommandHandler<ScanLibraryCommand, Res
return queueScanResult.Errors;
// add the library scan to the persistence medium
Result<Created> insertLibraryScanResult = await libraryScanRepository.InsertAsync(libraryScanResult.Value.ToRepositoryEntity(), cancellationToken).ConfigureAwait(false);
Result<Created> insertLibraryScanResult = await _unitOfWork.LibraryScanRepository.InsertAsync(libraryScanResult.Value.ToRepositoryEntity(), cancellationToken).ConfigureAwait(false);
if (insertLibraryScanResult.IsFailure)
return insertLibraryScanResult.Errors;
@@ -97,8 +97,7 @@ public class UpdateLibraryCommandHandler : ICommandHandler<UpdateLibraryCommand,
}
// get a library repository and retrieve the library to update
ILibraryRepository libraryRepository = _unitOfWork.GetRepository<ILibraryRepository>();
Result<LibraryEntity?> getLibraryResult = await libraryRepository.GetByIdAsync(command.Id, cancellationToken).ConfigureAwait(false);
Result<LibraryEntity?> getLibraryResult = await _unitOfWork.LibraryRepository.GetByIdAsync(command.Id, cancellationToken).ConfigureAwait(false);
if (getLibraryResult.IsFailure)
return getLibraryResult.Errors;
else if (getLibraryResult.Value is null)
@@ -131,13 +130,13 @@ public class UpdateLibraryCommandHandler : ICommandHandler<UpdateLibraryCommand,
LibraryEntity persistenceLibrary = createLibraryResult.Value.ToRepositoryEntity();
// update the repository entity and save changes
Result<Updated> updateLibraryResult = await libraryRepository.UpdateAsync(persistenceLibrary, cancellationToken).ConfigureAwait(false);
Result<Updated> updateLibraryResult = await _unitOfWork.LibraryRepository.UpdateAsync(persistenceLibrary, cancellationToken).ConfigureAwait(false);
if (updateLibraryResult.IsFailure)
return updateLibraryResult.Errors;
await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
// retrieve the updated media library from the persistence medium and return it
Result<LibraryEntity?> getCreatedLibraryResult = await libraryRepository.GetByIdAsync(createLibraryResult.Value.Id.Value, cancellationToken).ConfigureAwait(false);
Result<LibraryEntity?> getCreatedLibraryResult = await _unitOfWork.LibraryRepository.GetByIdAsync(createLibraryResult.Value.Id.Value, cancellationToken).ConfigureAwait(false);
if (getCreatedLibraryResult.IsFailure)
return getCreatedLibraryResult.Errors;
if (getCreatedLibraryResult.Value is null)
@@ -66,8 +66,7 @@ public class LibrarySavedDomainEventHandler : IDomainEventHandler<LibrarySavedDo
throw new EventualConsistencyException(saveCoverImageResult.FirstError, saveCoverImageResult.Errors);
domainEvent.Library.SetInternalLibraryCoverImagePath(saveCoverImageResult.Value);
// update the media library with the new cover location
ILibraryRepository libraryRepository = _unitOfWork.GetRepository<ILibraryRepository>();
Result<Updated> updateLibraryResult = await libraryRepository.UpdateAsync(domainEvent.Library.ToRepositoryEntity(), cancellationToken).ConfigureAwait(false);
Result<Updated> updateLibraryResult = await _unitOfWork.LibraryRepository.UpdateAsync(domainEvent.Library.ToRepositoryEntity(), cancellationToken).ConfigureAwait(false);
if (updateLibraryResult.IsFailure)
throw new EventualConsistencyException(updateLibraryResult.FirstError, updateLibraryResult.Errors);
@@ -28,7 +28,6 @@ public class LibraryScanCancelledDomainEventHandler : IDomainEventHandler<Librar
private readonly IMediaLibraryScanningService _mediaLibraryScanningService;
private readonly IMediaLibrariesScanCancellationTracker _mediaLibrariesScanCancellationTracker;
private readonly IMediaLibrariesScanProgressTracker _mediaLibrariesScanProgressTracker;
private readonly ILibraryScanRepository _libraryScanRepository;
private readonly IUnitOfWork _unitOfWork;
/// <summary>
@@ -47,7 +46,6 @@ public class LibraryScanCancelledDomainEventHandler : IDomainEventHandler<Librar
_mediaLibraryScanningService = mediaLibraryScanningService;
_mediaLibrariesScanCancellationTracker = mediaLibrariesScanCancellationTracker;
_mediaLibrariesScanProgressTracker = mediaLibrariesScanProgressTracker;
_libraryScanRepository = unitOfWork.GetRepository<ILibraryScanRepository>();
_unitOfWork = unitOfWork;
}
@@ -59,7 +57,7 @@ public class LibraryScanCancelledDomainEventHandler : IDomainEventHandler<Librar
public async ValueTask HandleAsync(LibraryScanCancelledDomainEvent domainEvent, CancellationToken cancellationToken)
{
// get the library scan from the repository
Result<LibraryScanEntity?> getLibraryScansResult = await _libraryScanRepository.GetByIdAsync(domainEvent.ScanId.Value, cancellationToken).ConfigureAwait(false);
Result<LibraryScanEntity?> getLibraryScansResult = await _unitOfWork.LibraryScanRepository.GetByIdAsync(domainEvent.ScanId.Value, cancellationToken).ConfigureAwait(false);
if (getLibraryScansResult.IsFailure)
throw new EventualConsistencyException(getLibraryScansResult.FirstError, getLibraryScansResult.Errors);
if (getLibraryScansResult.Value is null)
@@ -76,11 +74,11 @@ public class LibraryScanCancelledDomainEventHandler : IDomainEventHandler<Librar
throw new EventualConsistencyException(cancelScanResult.FirstError, cancelScanResult.Errors);
// release the scan processing resources
ILibraryScanStagingResultsRepository stagingResultsRepository = _unitOfWork.GetRepository<ILibraryScanStagingResultsRepository>();
MediaLibraryScanCompositeId compositeId = MediaLibraryScanCompositeId.Create(domainEvent.ScanId, UserId.Create(getLibraryScansResult.Value.UserId));
_mediaLibrariesScanCancellationTracker.RemoveScan(compositeId);
_mediaLibrariesScanProgressTracker.RemoveScanProgress(compositeId);
Result<Success> clearStagingResult = await stagingResultsRepository.ClearForScanAsync(domainEvent.ScanId.Value, cancellationToken).ConfigureAwait(false);
Result<Success> clearStagingResult = await _unitOfWork.LibraryScanStagingResultsRepository.ClearForScanAsync(domainEvent.ScanId.Value, cancellationToken).ConfigureAwait(false);
if (clearStagingResult.IsFailure)
throw new EventualConsistencyException(clearStagingResult.FirstError, clearStagingResult.Errors);
}
@@ -54,11 +54,8 @@ public class LibraryScanFailedDomainEventHandler : IDomainEventHandler<LibrarySc
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
public async ValueTask HandleAsync(LibraryScanFailedDomainEvent domainEvent, CancellationToken cancellationToken)
{
ILibraryScanRepository libraryScanRepository = _unitOfWork.GetRepository<ILibraryScanRepository>();
ILibraryScanStagingResultsRepository stagingResultsRepository = _unitOfWork.GetRepository<ILibraryScanStagingResultsRepository>();
// get the library scan from the repository
Result<LibraryScanEntity?> getLibraryScansResult = await libraryScanRepository.GetByIdAsync(
Result<LibraryScanEntity?> getLibraryScansResult = await _unitOfWork.LibraryScanRepository.GetByIdAsync(
domainEvent.MediaLibraryScanCompositeId.ScanId.Value, cancellationToken).ConfigureAwait(false);
if (getLibraryScansResult.IsFailure)
throw new EventualConsistencyException(getLibraryScansResult.FirstError, getLibraryScansResult.Errors);
@@ -77,7 +74,7 @@ public class LibraryScanFailedDomainEventHandler : IDomainEventHandler<LibrarySc
if (!failScanResult.IsFailure)
{
// update the status of the library scan in the repository
Result<Updated> updateLibraryScanResult = await libraryScanRepository.UpdateAsync(libraryScanDomainResult.Value.ToRepositoryEntity(), cancellationToken).ConfigureAwait(false);
Result<Updated> updateLibraryScanResult = await _unitOfWork.LibraryScanRepository.UpdateAsync(libraryScanDomainResult.Value.ToRepositoryEntity(), cancellationToken).ConfigureAwait(false);
if (updateLibraryScanResult.IsFailure)
throw new EventualConsistencyException(updateLibraryScanResult.FirstError, updateLibraryScanResult.Errors);
@@ -87,7 +84,7 @@ public class LibraryScanFailedDomainEventHandler : IDomainEventHandler<LibrarySc
// release the scan processing resources, regardless of whether the scan was already marked as failed by a concurrent job
_mediaLibrariesScanCancellationTracker.RemoveScan(domainEvent.MediaLibraryScanCompositeId);
_mediaLibrariesScanProgressTracker.RemoveScanProgress(domainEvent.MediaLibraryScanCompositeId);
Result<Success> clearStagingResult = await stagingResultsRepository.ClearForScanAsync(domainEvent.MediaLibraryScanCompositeId.ScanId.Value, cancellationToken).ConfigureAwait(false);
Result<Success> clearStagingResult = await _unitOfWork.LibraryScanStagingResultsRepository.ClearForScanAsync(domainEvent.MediaLibraryScanCompositeId.ScanId.Value, cancellationToken).ConfigureAwait(false);
if (clearStagingResult.IsFailure)
throw new EventualConsistencyException(clearStagingResult.FirstError, clearStagingResult.Errors);
@@ -54,10 +54,8 @@ public class LibraryScanFinishedDomainEventHandler : IDomainEventHandler<Library
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
public async ValueTask HandleAsync(LibraryScanFinishedDomainEvent domainEvent, CancellationToken cancellationToken)
{
ILibraryScanRepository libraryScanRepository = _unitOfWork.GetRepository<ILibraryScanRepository>();
// get the library scan from the repository
Result<LibraryScanEntity?> getLibraryScansResult = await libraryScanRepository.GetByIdAsync(
Result<LibraryScanEntity?> getLibraryScansResult = await _unitOfWork.LibraryScanRepository.GetByIdAsync(
domainEvent.MediaLibraryScanCompositeId.ScanId.Value, cancellationToken).ConfigureAwait(false);
if (getLibraryScansResult.IsFailure)
throw new EventualConsistencyException(getLibraryScansResult.FirstError, getLibraryScansResult.Errors);
@@ -75,7 +73,7 @@ public class LibraryScanFinishedDomainEventHandler : IDomainEventHandler<Library
throw new EventualConsistencyException(finishScanResult.FirstError, finishScanResult.Errors);
// update the library scan in the repository
Result<Updated> updateLibraryScanResult = await libraryScanRepository.UpdateAsync(libraryScanDomainResult.Value.ToRepositoryEntity(), cancellationToken).ConfigureAwait(false);
Result<Updated> updateLibraryScanResult = await _unitOfWork.LibraryScanRepository.UpdateAsync(libraryScanDomainResult.Value.ToRepositoryEntity(), cancellationToken).ConfigureAwait(false);
if (updateLibraryScanResult.IsFailure)
throw new EventualConsistencyException(updateLibraryScanResult.FirstError, updateLibraryScanResult.Errors);
@@ -48,10 +48,8 @@ public class LibraryScanQueuedDomainEventHandler : IDomainEventHandler<LibrarySc
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
public async ValueTask HandleAsync(LibraryScanQueuedDomainEvent domainEvent, CancellationToken cancellationToken)
{
ILibraryScanRepository libraryScanRepository = _unitOfWork.GetRepository<ILibraryScanRepository>();
// get the library scan that was queued, from the repository
Result<LibraryScanEntity?> getLibraryScanResult = await libraryScanRepository.GetByIdAsync(domainEvent.ScanId.Value, cancellationToken).ConfigureAwait(false);
Result<LibraryScanEntity?> getLibraryScanResult = await _unitOfWork.LibraryScanRepository.GetByIdAsync(domainEvent.ScanId.Value, cancellationToken).ConfigureAwait(false);
if (getLibraryScanResult.IsFailure || getLibraryScanResult.Value is null)
throw new EventualConsistencyException(getLibraryScanResult.FirstError, getLibraryScanResult.Errors);
@@ -35,10 +35,8 @@ public class LibraryScanStartedDomainEventHandler : IDomainEventHandler<LibraryS
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
public async ValueTask HandleAsync(LibraryScanStartedDomainEvent domainEvent, CancellationToken cancellationToken)
{
ILibraryScanRepository libraryScanRepository = _unitOfWork.GetRepository<ILibraryScanRepository>();
// update the status of the library scan in the repository
Result<Updated> updateLibraryScanResult = await libraryScanRepository.UpdateAsync(domainEvent.LibraryScan.ToRepositoryEntity(), cancellationToken).ConfigureAwait(false);
Result<Updated> updateLibraryScanResult = await _unitOfWork.LibraryScanRepository.UpdateAsync(domainEvent.LibraryScan.ToRepositoryEntity(), cancellationToken).ConfigureAwait(false);
if (updateLibraryScanResult.IsFailure)
throw new EventualConsistencyException(updateLibraryScanResult.FirstError, updateLibraryScanResult.Errors);
@@ -52,8 +52,7 @@ public class GetEnabledLibrariesQueryHandler : IQueryHandler<GetEnabledLibraries
public async Task<Result<LibraryResponse[]>> HandleAsync(GetEnabledLibrariesQuery query, CancellationToken cancellationToken)
{
// get the enabled libraries from the repository
ILibraryRepository libraryRepository = _unitOfWork.GetRepository<ILibraryRepository>();
Result<IEnumerable<LibraryEntity>> getEnabledLibrariesResult = await libraryRepository.GetAllEnabledAsync(cancellationToken).ConfigureAwait(false);
Result<IEnumerable<LibraryEntity>> getEnabledLibrariesResult = await _unitOfWork.LibraryRepository.GetAllEnabledAsync(cancellationToken).ConfigureAwait(false);
if (getEnabledLibrariesResult.IsFailure)
return getEnabledLibrariesResult.Errors;
@@ -52,8 +52,7 @@ public class GetLibrariesQueryHandler : IQueryHandler<GetLibrariesQuery, Result<
public async Task<Result<LibraryResponse[]>> HandleAsync(GetLibrariesQuery query, CancellationToken cancellationToken)
{
// get the libraries from the repository
ILibraryRepository libraryRepository = _unitOfWork.GetRepository<ILibraryRepository>();
Result<IEnumerable<LibraryEntity>> getLibrariesResult = await libraryRepository.GetAllAsync(cancellationToken).ConfigureAwait(false);
Result<IEnumerable<LibraryEntity>> getLibrariesResult = await _unitOfWork.LibraryRepository.GetAllAsync(cancellationToken).ConfigureAwait(false);
if (getLibrariesResult.IsFailure)
return getLibrariesResult.Errors;
@@ -63,8 +63,7 @@ public class GetLibraryQueryHandler : IQueryHandler<GetLibraryQuery, Result<Libr
return validationResult;
// get the library with the specified id from the repository
ILibraryRepository libraryRepository = _unitOfWork.GetRepository<ILibraryRepository>();
Result<LibraryEntity?> getLibraryResult = await libraryRepository.GetByIdAsync(query.Id, cancellationToken).ConfigureAwait(false);
Result<LibraryEntity?> getLibraryResult = await _unitOfWork.LibraryRepository.GetByIdAsync(query.Id, cancellationToken).ConfigureAwait(false);
if (getLibraryResult.IsFailure)
return getLibraryResult.Errors;
else if (getLibraryResult.Value is null)
@@ -70,8 +70,7 @@ public class GetLibraryScanProgressQueryHandler : IQueryHandler<GetLibraryScanPr
return validationResult;
// get the library with the specified id from the repository
ILibraryRepository libraryRepository = _unitOfWork.GetRepository<ILibraryRepository>();
Result<LibraryEntity?> getLibraryResult = await libraryRepository.GetByIdAsync(query.LibraryId, cancellationToken).ConfigureAwait(false);
Result<LibraryEntity?> getLibraryResult = await _unitOfWork.LibraryRepository.GetByIdAsync(query.LibraryId, cancellationToken).ConfigureAwait(false);
if (getLibraryResult.IsFailure)
return getLibraryResult.Errors;
else if (getLibraryResult.Value is null)
@@ -59,8 +59,7 @@ public class GetRunningLibraryScansQueryHandler : IQueryHandler<GetRunningLibrar
public async Task<Result<IEnumerable<MediaLibraryScanProgressResponse>>> HandleAsync(GetRunningLibraryScansQuery query, CancellationToken cancellationToken)
{
// get the ongoing library scans from the repository
ILibraryScanRepository libraryScanRepository = _unitOfWork.GetRepository<ILibraryScanRepository>();
Result<IEnumerable<LibraryScanEntity>> getRunningScansResult = await libraryScanRepository.GetRunningScansAsync(cancellationToken).ConfigureAwait(false);
Result<IEnumerable<LibraryScanEntity>> getRunningScansResult = await _unitOfWork.LibraryScanRepository.GetRunningScansAsync(cancellationToken).ConfigureAwait(false);
if (getRunningScansResult.IsFailure)
return getRunningScansResult.Errors;
@@ -171,9 +171,8 @@ public class AddBookCommandHandler : ICommandHandler<AddBookCommand, Result<Book
if (createBookResult.IsFailure)
return createBookResult.Errors;
IBookRepository bookRepository = _unitOfWork.GetRepository<IBookRepository>();
BookEntity persistenceBook = createBookResult.Value.ToRepositoryEntity();
Result<Created> insertBookResult = await bookRepository.InsertAsync(persistenceBook, cancellationToken).ConfigureAwait(false);
Result<Created> insertBookResult = await _unitOfWork.BookRepository.InsertAsync(persistenceBook, cancellationToken).ConfigureAwait(false);
if (insertBookResult.IsFailure)
return insertBookResult.Errors;
await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
@@ -39,10 +39,8 @@ public class GetBooksQueryHandler : IQueryHandler<GetBooksQuery, Result<IEnumera
/// </returns>
public async Task<Result<IEnumerable<BookResponse>>> HandleAsync(GetBooksQuery query, CancellationToken cancellationToken)
{
// get a books repository
IBookRepository bookRepository = _unitOfWork.GetRepository<IBookRepository>();
// get all books of the media library from the book repository
Result<IEnumerable<BookEntity>> getBooksResult = await bookRepository.GetByLibraryIdAsync(query.LibraryId, cancellationToken).ConfigureAwait(false);
Result<IEnumerable<BookEntity>> getBooksResult = await _unitOfWork.BookRepository.GetByLibraryIdAsync(query.LibraryId, cancellationToken).ConfigureAwait(false);
return getBooksResult.Match(result => Result.From(getBooksResult.Value.ToResponses()), errors => errors);
}
}
@@ -47,8 +47,7 @@ public class ReorderLibraryMetadataProvidersCommandHandler : ICommandHandler<Reo
if (validationResult.Count > 0)
return validationResult;
ILibraryMetadataProviderConfigurationRepository configurationRepository = _unitOfWork.GetRepository<ILibraryMetadataProviderConfigurationRepository>();
Result<IReadOnlyList<LibraryMetadataProviderConfigurationEntity>> getConfigurationsResult = await configurationRepository.GetByLibraryIdAsync(command.LibraryId, cancellationToken).ConfigureAwait(false);
Result<IReadOnlyList<LibraryMetadataProviderConfigurationEntity>> getConfigurationsResult = await _unitOfWork.LibraryMetadataProviderConfigurationRepository.GetByLibraryIdAsync(command.LibraryId, cancellationToken).ConfigureAwait(false);
if (getConfigurationsResult.IsFailure)
return getConfigurationsResult.Errors;
@@ -59,7 +58,7 @@ public class ReorderLibraryMetadataProvidersCommandHandler : ICommandHandler<Reo
if (configurationsByPluginId.TryGetValue(pluginId, out LibraryMetadataProviderConfigurationEntity? configuration))
{
configuration.Rank = rank + 1;
Result<Updated> upsertResult = await configurationRepository.UpsertAsync(configuration, cancellationToken).ConfigureAwait(false);
Result<Updated> upsertResult = await _unitOfWork.LibraryMetadataProviderConfigurationRepository.UpsertAsync(configuration, cancellationToken).ConfigureAwait(false);
if (upsertResult.IsFailure)
return upsertResult.Errors;
}
@@ -47,8 +47,7 @@ public class SetLibraryMetadataProviderEnabledCommandHandler : ICommandHandler<S
if (validationResult.Count > 0)
return validationResult;
ILibraryMetadataProviderConfigurationRepository configurationRepository = _unitOfWork.GetRepository<ILibraryMetadataProviderConfigurationRepository>();
Result<LibraryMetadataProviderConfigurationEntity?> getConfigurationResult = await configurationRepository.GetByLibraryAndPluginIdAsync(command.LibraryId, command.PluginId, cancellationToken).ConfigureAwait(false);
Result<LibraryMetadataProviderConfigurationEntity?> getConfigurationResult = await _unitOfWork.LibraryMetadataProviderConfigurationRepository.GetByLibraryAndPluginIdAsync(command.LibraryId, command.PluginId, cancellationToken).ConfigureAwait(false);
if (getConfigurationResult.IsFailure)
return getConfigurationResult.Errors;
@@ -61,7 +60,7 @@ public class SetLibraryMetadataProviderEnabledCommandHandler : ICommandHandler<S
else
{
// compute the rank to assign to the new configuration, appending it after the existing ones
Result<IReadOnlyList<LibraryMetadataProviderConfigurationEntity>> getConfigurationsResult = await configurationRepository.GetByLibraryIdAsync(command.LibraryId, cancellationToken).ConfigureAwait(false);
Result<IReadOnlyList<LibraryMetadataProviderConfigurationEntity>> getConfigurationsResult = await _unitOfWork.LibraryMetadataProviderConfigurationRepository.GetByLibraryIdAsync(command.LibraryId, cancellationToken).ConfigureAwait(false);
if (getConfigurationsResult.IsFailure)
return getConfigurationsResult.Errors;
int nextRank = getConfigurationsResult.Value.Count == 0 ? 1 : getConfigurationsResult.Value.Max(configuration => configuration.Rank) + 1;
@@ -78,7 +77,7 @@ public class SetLibraryMetadataProviderEnabledCommandHandler : ICommandHandler<S
};
}
Result<Updated> upsertResult = await configurationRepository.UpsertAsync(configuration, cancellationToken).ConfigureAwait(false);
Result<Updated> upsertResult = await _unitOfWork.LibraryMetadataProviderConfigurationRepository.UpsertAsync(configuration, cancellationToken).ConfigureAwait(false);
if (upsertResult.IsFailure)
return upsertResult.Errors;
await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
@@ -45,9 +45,8 @@ public class UpdatePluginSettingsCommandHandler : ICommandHandler<UpdatePluginSe
if (validationResult.Count > 0)
return validationResult;
IPluginRepository pluginRepository = _unitOfWork.GetRepository<IPluginRepository>();
string? settingsJson = command.Settings is not null ? JsonSerializer.Serialize(command.Settings) : null;
Result<Updated> updateResult = await pluginRepository.UpdateSettingsAsync(command.PluginId, settingsJson, cancellationToken).ConfigureAwait(false);
Result<Updated> updateResult = await _unitOfWork.PluginRepository.UpdateSettingsAsync(command.PluginId, settingsJson, cancellationToken).ConfigureAwait(false);
if (updateResult.IsFailure)
return updateResult.Errors;
await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
@@ -41,14 +41,12 @@ public class GetLibraryMetadataProvidersQueryHandler : IQueryHandler<GetLibraryM
/// </returns>
public async Task<Result<IReadOnlyList<LibraryMetadataProviderResponse>>> HandleAsync(GetLibraryMetadataProvidersQuery query, CancellationToken cancellationToken)
{
ILibraryMetadataProviderConfigurationRepository configurationRepository = _unitOfWork.GetRepository<ILibraryMetadataProviderConfigurationRepository>();
Result<IReadOnlyList<LibraryMetadataProviderConfigurationEntity>> getConfigurationsResult = await configurationRepository.GetByLibraryIdAsync(query.LibraryId, cancellationToken).ConfigureAwait(false);
Result<IReadOnlyList<LibraryMetadataProviderConfigurationEntity>> getConfigurationsResult = await _unitOfWork.LibraryMetadataProviderConfigurationRepository.GetByLibraryIdAsync(query.LibraryId, cancellationToken).ConfigureAwait(false);
if (getConfigurationsResult.IsFailure)
return getConfigurationsResult.Errors;
// build a plugin name lookup from the detected plugins
IPluginRepository pluginRepository = _unitOfWork.GetRepository<IPluginRepository>();
Result<IEnumerable<PluginEntity>> getPluginsResult = await pluginRepository.GetAllAsync(cancellationToken).ConfigureAwait(false);
Result<IEnumerable<PluginEntity>> getPluginsResult = await _unitOfWork.PluginRepository.GetAllAsync(cancellationToken).ConfigureAwait(false);
if (getPluginsResult.IsFailure)
return getPluginsResult.Errors;
Dictionary<Guid, string> pluginNames = getPluginsResult.Value.ToDictionary(plugin => plugin.Id, plugin => plugin.Name);
@@ -46,8 +46,7 @@ public class GetPluginSettingsQueryHandler : IQueryHandler<GetPluginSettingsQuer
/// </returns>
public async Task<Result<PluginSettingsResponse>> HandleAsync(GetPluginSettingsQuery query, CancellationToken cancellationToken)
{
IPluginRepository pluginRepository = _unitOfWork.GetRepository<IPluginRepository>();
Result<PluginEntity?> getPluginResult = await pluginRepository.GetByIdAsync(query.PluginId, cancellationToken).ConfigureAwait(false);
Result<PluginEntity?> getPluginResult = await _unitOfWork.PluginRepository.GetByIdAsync(query.PluginId, cancellationToken).ConfigureAwait(false);
if (getPluginResult.IsFailure)
return getPluginResult.Errors;
PluginEntity? pluginEntity = getPluginResult.Value;
@@ -40,10 +40,7 @@ public class GetPluginsQueryHandler : IQueryHandler<GetPluginsQuery, Result<IRea
/// </returns>
public async Task<Result<IReadOnlyList<PluginResponse>>> HandleAsync(GetPluginsQuery query, CancellationToken cancellationToken)
{
IPluginRepository pluginRepository = _unitOfWork.GetRepository<IPluginRepository>();
Result<IEnumerable<PluginEntity>> getPluginsResult = await pluginRepository.GetAllAsync(cancellationToken).ConfigureAwait(false);
return getPluginsResult.Match(
plugins => Result.From<IReadOnlyList<PluginResponse>>(plugins.Select(plugin => plugin.ToResponse()).ToList()),
errors => errors);
Result<IEnumerable<PluginEntity>> getPluginsResult = await _unitOfWork.PluginRepository.GetAllAsync(cancellationToken).ConfigureAwait(false);
return getPluginsResult.Match(plugins => Result.From<IReadOnlyList<PluginResponse>>([.. plugins.Select(plugin => plugin.ToResponse())]), errors => errors);
}
}
@@ -55,8 +55,7 @@ public class ChangePasswordCommandHandler : ICommandHandler<ChangePasswordComman
if (validationResult.Count > 0)
return validationResult;
IUserRepository userRepository = _unitOfWork.GetRepository<IUserRepository>();
Result<UserEntity?> getUserResult = await userRepository.GetByUsernameAsync(command.Username!, cancellationToken).ConfigureAwait(false);
Result<UserEntity?> getUserResult = await _unitOfWork.UserRepository.GetByUsernameAsync(command.Username!, cancellationToken).ConfigureAwait(false);
if (getUserResult.IsFailure)
return getUserResult.Errors;
else if (getUserResult.Value is null)
@@ -69,7 +68,7 @@ public class ChangePasswordCommandHandler : ICommandHandler<ChangePasswordComman
getUserResult.Value.TempPassword = null;
getUserResult.Value.TempPasswordCreated = null;
// update the user
Result<Updated> updateUserResult = await userRepository.UpdateAsync(getUserResult.Value, cancellationToken).ConfigureAwait(false);
Result<Updated> updateUserResult = await _unitOfWork.UserRepository.UpdateAsync(getUserResult.Value, cancellationToken).ConfigureAwait(false);
if (updateUserResult.IsFailure)
return updateUserResult.Errors;
await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
@@ -65,8 +65,7 @@ public class RecoverPasswordCommandHandler : ICommandHandler<RecoverPasswordComm
return validationResult;
// check if any users already exists
IUserRepository userRepository = _unitOfWork.GetRepository<IUserRepository>();
Result<UserEntity?> getUserResult = await userRepository.GetByUsernameAsync(command.Username!, cancellationToken).ConfigureAwait(false);
Result<UserEntity?> getUserResult = await _unitOfWork.UserRepository.GetByUsernameAsync(command.Username!, cancellationToken).ConfigureAwait(false);
if (getUserResult.IsFailure)
return getUserResult.Errors;
else if (getUserResult.Value is null)
@@ -86,7 +85,7 @@ public class RecoverPasswordCommandHandler : ICommandHandler<RecoverPasswordComm
getUserResult.Value.TempPassword = Uri.EscapeDataString(_hashService.HashString("Abcd123$")); // TODO: replace with random password generator
getUserResult.Value.TempPasswordCreated = DateTime.UtcNow;
// update the user
Result<Updated> updateUserResult = await userRepository.UpdateAsync(getUserResult.Value, cancellationToken).ConfigureAwait(false);
Result<Updated> updateUserResult = await _unitOfWork.UserRepository.UpdateAsync(getUserResult.Value, cancellationToken).ConfigureAwait(false);
if (updateUserResult.IsFailure)
return updateUserResult.Errors;
await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
@@ -74,8 +74,7 @@ public class RegisterUserCommandHandler : ICommandHandler<RegisterUserCommand, R
return validationResult;
// check if any users already exists (admin account is only set once!)
IUserRepository userRepository = _unitOfWork.GetRepository<IUserRepository>();
Result<UserEntity?> getUserResult = await userRepository.GetByUsernameAsync(command.Username!, cancellationToken).ConfigureAwait(false);
Result<UserEntity?> getUserResult = await _unitOfWork.UserRepository.GetByUsernameAsync(command.Username!, cancellationToken).ConfigureAwait(false);
if (getUserResult.IsFailure)
return getUserResult.Errors;
else if (getUserResult.Value is not null)
@@ -105,7 +104,7 @@ public class RegisterUserCommandHandler : ICommandHandler<RegisterUserCommand, R
user.TotpSecret = _cryptographyService.Encrypt(Convert.ToBase64String(secret));
}
// insert the user
Result<Created> insertUserResult = await userRepository.InsertAsync(user, cancellationToken).ConfigureAwait(false);
Result<Created> insertUserResult = await _unitOfWork.UserRepository.InsertAsync(user, cancellationToken).ConfigureAwait(false);
if (insertUserResult.IsFailure)
return insertUserResult.Errors;
await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
@@ -22,9 +22,9 @@ namespace Lumina.Application.Core.UsersManagement.Authentication.Queries.GetUser
/// </summary>
public class GetUsersQueryHandler : IQueryHandler<GetUsersQuery, Result<IEnumerable<UserResponse>>>
{
private readonly IUnitOfWork _unitOfWork;
private readonly ICurrentUserService _currentUserService;
private readonly IAuthorizationService _authorizationService;
private readonly IUserRepository _userRepository;
/// <summary>
/// Initializes a new instance of the <see cref="GetUsersQueryHandler"/> class.
@@ -36,7 +36,7 @@ public class GetUsersQueryHandler : IQueryHandler<GetUsersQuery, Result<IEnumera
{
_authorizationService = authorizationService;
_currentUserService = currentUserService;
_userRepository = unitOfWork.GetRepository<IUserRepository>();
_unitOfWork = unitOfWork;
}
/// <summary>
@@ -53,7 +53,7 @@ public class GetUsersQueryHandler : IQueryHandler<GetUsersQuery, Result<IEnumera
bool isAdmin = await _authorizationService.IsInRoleAsync(_currentUserService.UserId!.Value, "Admin", cancellationToken).ConfigureAwait(false);
if (!isAdmin)
return Errors.Authorization.NotAuthorized;
Result<IEnumerable<UserEntity>> getRolesResult = await _userRepository.GetAllAsync(cancellationToken).ConfigureAwait(false);
Result<IEnumerable<UserEntity>> getRolesResult = await _unitOfWork.UserRepository.GetAllAsync(cancellationToken).ConfigureAwait(false);
return getRolesResult.Match(value => Result.From(value.ToResponses()), errors => errors);
}
}
@@ -74,8 +74,7 @@ public class LoginUserQueryHandler : IQueryHandler<LoginUserQuery, Result<LoginR
return validationResult;
// check if any users already exists
IUserRepository userRepository = _unitOfWork.GetRepository<IUserRepository>();
Result<UserEntity?> getUserResult = await userRepository.GetByUsernameAsync(query.Username!, cancellationToken).ConfigureAwait(false);
Result<UserEntity?> getUserResult = await _unitOfWork.UserRepository.GetByUsernameAsync(query.Username!, cancellationToken).ConfigureAwait(false);
if (getUserResult.IsFailure)
return getUserResult.Errors;
else if (getUserResult.Value is null)
@@ -91,7 +90,7 @@ public class LoginUserQueryHandler : IQueryHandler<LoginUserQuery, Result<LoginR
{
getUserResult.Value.TempPassword = null;
getUserResult.Value.TempPasswordCreated = null;
await userRepository.UpdateAsync(getUserResult.Value, cancellationToken);
await _unitOfWork.UserRepository.UpdateAsync(getUserResult.Value, cancellationToken);
await _unitOfWork.SaveChangesAsync(cancellationToken);
return Errors.Authentication.TempPasswordExpired;
}
@@ -65,13 +65,8 @@ public class UpdateUserRoleAndPermissionsCommandHandler : ICommandHandler<Update
if (!isAdmin)
return ApplicationErrors.Authorization.NotAuthorized;
// get repositories
IUserRepository userRepository = _unitOfWork.GetRepository<IUserRepository>();
IRoleRepository roleRepository = _unitOfWork.GetRepository<IRoleRepository>();
IPermissionRepository permissionRepository = _unitOfWork.GetRepository<IPermissionRepository>();
// get the user to update
Result<UserEntity?> getUserResult = await userRepository.GetByIdAsync(command.UserId, cancellationToken).ConfigureAwait(false);
Result<UserEntity?> getUserResult = await _unitOfWork.UserRepository.GetByIdAsync(command.UserId, cancellationToken).ConfigureAwait(false);
if (getUserResult.IsFailure || getUserResult.Value is null)
return DomainErrors.Users.UserDoesNotExist;
@@ -79,7 +74,7 @@ public class UpdateUserRoleAndPermissionsCommandHandler : ICommandHandler<Update
if (command.RoleId is not null)
{
// get the new role
getRoleResult = await roleRepository.GetByIdAsync(command.RoleId!.Value, cancellationToken).ConfigureAwait(false);
getRoleResult = await _unitOfWork.RoleRepository.GetByIdAsync(command.RoleId!.Value, cancellationToken).ConfigureAwait(false);
if (getRoleResult.IsFailure || getRoleResult.Value is null)
return ApplicationErrors.Authorization.RoleNotFound;
@@ -87,7 +82,7 @@ public class UpdateUserRoleAndPermissionsCommandHandler : ICommandHandler<Update
if (getUserResult.Value.UserRole?.Role.RoleName == "Admin" && getRoleResult.Value.RoleName != "Admin")
{
// count how many admins we have
Result<IEnumerable<UserEntity>> getAllUsersResult = await userRepository.GetAllAsync(cancellationToken).ConfigureAwait(false);
Result<IEnumerable<UserEntity>> getAllUsersResult = await _unitOfWork.UserRepository.GetAllAsync(cancellationToken).ConfigureAwait(false);
if (getAllUsersResult.IsFailure)
return getAllUsersResult.Errors;
@@ -98,7 +93,7 @@ public class UpdateUserRoleAndPermissionsCommandHandler : ICommandHandler<Update
}
// get the permissions to assign
Result<IEnumerable<PermissionEntity>> getPermissionsResult =
await permissionRepository.GetByIdsAsync(command.Permissions, cancellationToken).ConfigureAwait(false);
await _unitOfWork.PermissionRepository.GetByIdsAsync(command.Permissions, cancellationToken).ConfigureAwait(false);
if (getPermissionsResult.IsFailure)
return getPermissionsResult.Errors;
@@ -127,18 +122,18 @@ public class UpdateUserRoleAndPermissionsCommandHandler : ICommandHandler<Update
TempPasswordCreated = userToUpdate.TempPasswordCreated,
Libraries = userToUpdate.Libraries,
UserRole = userRole,
UserPermissions = command.Permissions.Select(permissionId => new UserPermissionEntity
UserPermissions = [.. command.Permissions.Select(permissionId => new UserPermissionEntity
{
UserId = userToUpdate.Id,
PermissionId = permissionId,
Permission = getPermissionsResult.Value.First(permission => permission.Id == permissionId),
User = userToUpdate
}).ToList(),
})],
LibraryScans = userToUpdate.LibraryScans
};
// save changes and return result
Result<Updated> updateResult = await userRepository.UpdateAsync(updatedUser, cancellationToken).ConfigureAwait(false);
Result<Updated> updateResult = await _unitOfWork.UserRepository.UpdateAsync(updatedUser, cancellationToken).ConfigureAwait(false);
if (updateResult.IsFailure)
return updateResult.Errors;
@@ -23,9 +23,9 @@ namespace Lumina.Application.Core.UsersManagement.Authorization.Queries.GetUserP
/// </summary>
public class GetUserPermissionsQueryHandler : IQueryHandler<GetUserPermissionsQuery, Result<IEnumerable<PermissionResponse>>>
{
private readonly IUnitOfWork _unitOfWork;
private readonly ICurrentUserService _currentUserService;
private readonly IAuthorizationService _authorizationService;
private readonly IUserRepository _userRepository;
private readonly IValidator<GetUserPermissionsQuery> _validator;
/// <summary>
@@ -39,7 +39,7 @@ public class GetUserPermissionsQueryHandler : IQueryHandler<GetUserPermissionsQu
{
_authorizationService = authorizationService;
_currentUserService = currentUserService;
_userRepository = unitOfWork.GetRepository<IUserRepository>();
_unitOfWork = unitOfWork;
_validator = validator;
}
@@ -62,7 +62,7 @@ public class GetUserPermissionsQueryHandler : IQueryHandler<GetUserPermissionsQu
if (!isAdmin)
return Errors.Authorization.NotAuthorized;
// get the user from the repository and return its permissions
Result<UserEntity?> getUserResult = await _userRepository.GetByIdAsync(query.UserId!.Value, cancellationToken).ConfigureAwait(false);
Result<UserEntity?> getUserResult = await _unitOfWork.UserRepository.GetByIdAsync(query.UserId!.Value, cancellationToken).ConfigureAwait(false);
if (getUserResult.IsFailure)
return getUserResult.Errors;
else if (getUserResult.Value is null)
@@ -22,9 +22,9 @@ namespace Lumina.Application.Core.UsersManagement.Authorization.Queries.GetUserR
/// </summary>
public class GetUserRoleQueryHandler : IQueryHandler<GetUserRoleQuery, Result<RoleResponse?>>
{
private readonly IUnitOfWork _unitOfWork;
private readonly ICurrentUserService _currentUserService;
private readonly IAuthorizationService _authorizationService;
private readonly IUserRepository _userRepository;
private readonly IValidator<GetUserRoleQuery> _validator;
/// <summary>
@@ -38,7 +38,7 @@ public class GetUserRoleQueryHandler : IQueryHandler<GetUserRoleQuery, Result<Ro
{
_authorizationService = authorizationService;
_currentUserService = currentUserService;
_userRepository = unitOfWork.GetRepository<IUserRepository>();
_unitOfWork = unitOfWork;
_validator = validator;
}
@@ -61,7 +61,7 @@ public class GetUserRoleQueryHandler : IQueryHandler<GetUserRoleQuery, Result<Ro
if (!isAdmin)
return Errors.Authorization.NotAuthorized;
// get the user from the repository and return its roles
Result<UserEntity?> getUserResult = await _userRepository.GetByIdAsync(query.UserId!.Value, cancellationToken).ConfigureAwait(false);
Result<UserEntity?> getUserResult = await _unitOfWork.UserRepository.GetByIdAsync(query.UserId!.Value, cancellationToken).ConfigureAwait(false);
if (getUserResult.IsFailure)
return getUserResult.Errors;
else if (getUserResult.Value is null)