mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 08: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
+2
-3
@@ -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);
|
||||
|
||||
+2
-3
@@ -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);
|
||||
|
||||
+2
-3
@@ -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);
|
||||
|
||||
+3
-3
@@ -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);
|
||||
}
|
||||
}
|
||||
+2
-3
@@ -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;
|
||||
}
|
||||
|
||||
+7
-12
@@ -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;
|
||||
|
||||
|
||||
+3
-3
@@ -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)
|
||||
|
||||
+3
-3
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user