mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 06:11:10 +00:00
Replaces reflection-based repository instantiation with unit of work property accessors
This commit is contained in:
1 parent
4fa9eb6e26
commit
fee14afb89
87 files changed
+541
-735
No files matched your search
+3
-3
@@ -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);
|
||||
}
|
||||
}
|
||||
+2
-4
@@ -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)
|
||||
|
||||
+2
-3
@@ -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);
|
||||
|
||||
+2
-4
@@ -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)
|
||||
|
||||
+1
-1
@@ -38,7 +38,7 @@ public class GetRolePermissionsQueryHandler : IQueryHandler<GetRolePermissionsQu
|
||||
{
|
||||
_authorizationService = authorizationService;
|
||||
_currentUserService = currentUserService;
|
||||
_roleRepository = unitOfWork.GetRepository<IRoleRepository>();
|
||||
_roleRepository = unitOfWork.RoleRepository;
|
||||
_validator = validator;
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -35,7 +35,7 @@ public class GetRolesQueryHandler : IQueryHandler<GetRolesQuery, Result<IEnumera
|
||||
{
|
||||
_authorizationService = authorizationService;
|
||||
_currentUserService = currentUserService;
|
||||
_roleRepository = unitOfWork.GetRepository<IRoleRepository>();
|
||||
_roleRepository = unitOfWork.RoleRepository;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in new issue
Block a user