mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 12:11:10 +00:00
Replaces ErrorOr library with a custom implementation.
This commit is contained in:
1 parent
8fe90a4dde
commit
3b772d68ca
436 files changed
+4234
-3922
No files matched your search
+6
-6
@@ -1,5 +1,5 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using ErrorOr;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using Lumina.Application.Common.DataAccess.Entities.Authorization;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Authorization;
|
||||
@@ -19,7 +19,7 @@ namespace Lumina.Application.Core.Admin.Authorization.Permissions.Queries.GetPer
|
||||
/// <summary>
|
||||
/// Handler for the query to retrieve the list of authorization permissions.
|
||||
/// </summary>
|
||||
public class GetPermissionsQueryHandler : IQueryHandler<GetPermissionsQuery, ErrorOr<IEnumerable<PermissionResponse>>>
|
||||
public class GetPermissionsQueryHandler : IQueryHandler<GetPermissionsQuery, Result<IEnumerable<PermissionResponse>>>
|
||||
{
|
||||
private readonly ICurrentUserService _currentUserService;
|
||||
private readonly IAuthorizationService _authorizationService;
|
||||
@@ -44,15 +44,15 @@ public class GetPermissionsQueryHandler : IQueryHandler<GetPermissionsQuery, Err
|
||||
/// <param name="query">The request to be handled.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>
|
||||
/// An <see cref="ErrorOr{TValue}"/> containing either a collection of <see cref="PermissionResponse"/>, or an error message.
|
||||
/// An <see cref="Result{TValue}"/> containing either a collection of <see cref="PermissionResponse"/>, or an error message.
|
||||
/// </returns>
|
||||
public async Task<ErrorOr<IEnumerable<PermissionResponse>>> HandleAsync(GetPermissionsQuery query, CancellationToken cancellationToken)
|
||||
public async Task<Result<IEnumerable<PermissionResponse>>> HandleAsync(GetPermissionsQuery query, CancellationToken cancellationToken)
|
||||
{
|
||||
// only admins can see the list of authorization permissions
|
||||
bool isAdmin = await _authorizationService.IsInRoleAsync(_currentUserService.UserId!.Value, "Admin", cancellationToken).ConfigureAwait(false);
|
||||
if (!isAdmin)
|
||||
return Errors.Authorization.NotAuthorized;
|
||||
ErrorOr<IEnumerable<PermissionEntity>> getPermissionsResult = await _permissionRepository.GetAllAsync(cancellationToken).ConfigureAwait(false);
|
||||
return getPermissionsResult.Match(value => ErrorOrFactory.From(value.ToResponses()), errors => errors);
|
||||
Result<IEnumerable<PermissionEntity>> getPermissionsResult = await _permissionRepository.GetAllAsync(cancellationToken).ConfigureAwait(false);
|
||||
return getPermissionsResult.Match(value => Result.From(value.ToResponses()), errors => errors);
|
||||
}
|
||||
}
|
||||
+8
-8
@@ -1,5 +1,5 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using ErrorOr;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using Lumina.Application.Common.DataAccess.Entities.Authorization;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Authorization;
|
||||
@@ -21,7 +21,7 @@ namespace Lumina.Application.Core.Admin.Authorization.Roles.Commands.AddRole;
|
||||
/// <summary>
|
||||
/// Handler for the command to add an authorization role.
|
||||
/// </summary>
|
||||
public class AddRoleCommandHandler : ICommandHandler<AddRoleCommand, ErrorOr<RolePermissionsResponse>>
|
||||
public class AddRoleCommandHandler : ICommandHandler<AddRoleCommand, Result<RolePermissionsResponse>>
|
||||
{
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly IAuthorizationService _authorizationService;
|
||||
@@ -49,9 +49,9 @@ public class AddRoleCommandHandler : ICommandHandler<AddRoleCommand, ErrorOr<Rol
|
||||
/// <param name="request">The request to be handled.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>
|
||||
/// An <see cref="ErrorOr{TValue}"/> containing either a successfully created <see cref="RoleResponse"/>, or an error message.
|
||||
/// An <see cref="Result{TValue}"/> containing either a successfully created <see cref="RoleResponse"/>, or an error message.
|
||||
/// </returns>
|
||||
public async Task<ErrorOr<RolePermissionsResponse>> HandleAsync(AddRoleCommand request, CancellationToken cancellationToken)
|
||||
public async Task<Result<RolePermissionsResponse>> HandleAsync(AddRoleCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
List<Error> validationResult = _validator.Validate(request);
|
||||
if (validationResult.Count > 0)
|
||||
@@ -77,13 +77,13 @@ public class AddRoleCommandHandler : ICommandHandler<AddRoleCommand, ErrorOr<Rol
|
||||
})]
|
||||
};
|
||||
// save the new role in the repository
|
||||
ErrorOr<Created> insertRoleResult = await roleRepository.InsertAsync(newRole, cancellationToken).ConfigureAwait(false);
|
||||
if (insertRoleResult.IsError)
|
||||
Result<Created> insertRoleResult = await 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
|
||||
ErrorOr<RoleEntity?> getRoleResult = await roleRepository.GetByNameAsync(request.RoleName, cancellationToken).ConfigureAwait(false);
|
||||
if (getRoleResult.IsError)
|
||||
Result<RoleEntity?> getRoleResult = await roleRepository.GetByNameAsync(request.RoleName, cancellationToken).ConfigureAwait(false);
|
||||
if (getRoleResult.IsFailure)
|
||||
return getRoleResult.Errors;
|
||||
if (getRoleResult.Value is null)
|
||||
return Errors.Persistence.ErrorPersistingAuthorizationRole;
|
||||
|
||||
+8
-8
@@ -1,5 +1,5 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using ErrorOr;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using Lumina.Application.Common.DataAccess.Entities.Authorization;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Authorization;
|
||||
@@ -22,7 +22,7 @@ namespace Lumina.Application.Core.Admin.Authorization.Roles.Commands.DeleteRole;
|
||||
/// <summary>
|
||||
/// Handler for the command to delete an authorization role.
|
||||
/// </summary>
|
||||
public class DeleteRoleCommandHandler : ICommandHandler<DeleteRoleCommand, ErrorOr<Deleted>>
|
||||
public class DeleteRoleCommandHandler : ICommandHandler<DeleteRoleCommand, Result<Deleted>>
|
||||
{
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly IAuthorizationService _authorizationService;
|
||||
@@ -49,8 +49,8 @@ public class DeleteRoleCommandHandler : ICommandHandler<DeleteRoleCommand, Error
|
||||
/// </summary>
|
||||
/// <param name="command">The request to be handled.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="ErrorOr{TValue}"/> representing either a successful operation, or an error.</returns>
|
||||
public async Task<ErrorOr<Deleted>> HandleAsync(DeleteRoleCommand command, CancellationToken cancellationToken)
|
||||
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
|
||||
public async Task<Result<Deleted>> HandleAsync(DeleteRoleCommand command, CancellationToken cancellationToken)
|
||||
{
|
||||
List<Error> validationResult = _validator.Validate(command);
|
||||
if (validationResult.Count > 0)
|
||||
@@ -63,16 +63,16 @@ public class DeleteRoleCommandHandler : ICommandHandler<DeleteRoleCommand, Error
|
||||
|
||||
// check if a role with the requested Id exists
|
||||
IRoleRepository roleRepository = _unitOfWork.GetRepository<IRoleRepository>();
|
||||
ErrorOr<RoleEntity?> getExistingRoleResult = await roleRepository.GetByIdAsync(command.RoleId, cancellationToken).ConfigureAwait(false);
|
||||
if (getExistingRoleResult.IsError)
|
||||
Result<RoleEntity?> getExistingRoleResult = await roleRepository.GetByIdAsync(command.RoleId, cancellationToken).ConfigureAwait(false);
|
||||
if (getExistingRoleResult.IsFailure)
|
||||
return getExistingRoleResult.Errors;
|
||||
else if (getExistingRoleResult.Value is null)
|
||||
return Errors.Authorization.RoleNotFound;
|
||||
else if (getExistingRoleResult.Value.RoleName == "Admin")
|
||||
return Errors.Authorization.AdminRoleCannotBeDeleted;
|
||||
// delete the role and its permissions
|
||||
ErrorOr<Deleted> deleteRoleResult = await roleRepository.DeleteByIdAsync(command.RoleId, cancellationToken).ConfigureAwait(false);
|
||||
if (deleteRoleResult.IsError)
|
||||
Result<Deleted> deleteRoleResult = await roleRepository.DeleteByIdAsync(command.RoleId, cancellationToken).ConfigureAwait(false);
|
||||
if (deleteRoleResult.IsFailure)
|
||||
return deleteRoleResult.Errors;
|
||||
await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
|
||||
return deleteRoleResult.Value;
|
||||
|
||||
+8
-8
@@ -1,5 +1,5 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using ErrorOr;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using Lumina.Application.Common.DataAccess.Entities.Authorization;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Authorization;
|
||||
@@ -21,7 +21,7 @@ namespace Lumina.Application.Core.Admin.Authorization.Roles.Commands.UpdateRole;
|
||||
/// <summary>
|
||||
/// Handler for the command to update an authorization role.
|
||||
/// </summary>
|
||||
public class UpdateRoleCommandHandler : ICommandHandler<UpdateRoleCommand, ErrorOr<RolePermissionsResponse>>
|
||||
public class UpdateRoleCommandHandler : ICommandHandler<UpdateRoleCommand, Result<RolePermissionsResponse>>
|
||||
{
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly IAuthorizationService _authorizationService;
|
||||
@@ -49,9 +49,9 @@ public class UpdateRoleCommandHandler : ICommandHandler<UpdateRoleCommand, Error
|
||||
/// <param name="command">The request to be handled.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>
|
||||
/// An <see cref="ErrorOr{TValue}"/> containing either a successfully updated <see cref="RoleResponse"/>, or an error message.
|
||||
/// An <see cref="Result{TValue}"/> containing either a successfully updated <see cref="RoleResponse"/>, or an error message.
|
||||
/// </returns>
|
||||
public async Task<ErrorOr<RolePermissionsResponse>> HandleAsync(UpdateRoleCommand command, CancellationToken cancellationToken)
|
||||
public async Task<Result<RolePermissionsResponse>> HandleAsync(UpdateRoleCommand command, CancellationToken cancellationToken)
|
||||
{
|
||||
List<Error> validationResult = _validator.Validate(command);
|
||||
if (validationResult.Count > 0)
|
||||
@@ -78,13 +78,13 @@ public class UpdateRoleCommandHandler : ICommandHandler<UpdateRoleCommand, Error
|
||||
}).ToList()
|
||||
};
|
||||
// save the updated role in the repository
|
||||
ErrorOr<Updated> updateRoleResult = await roleRepository.UpdateAsync(newRole, cancellationToken).ConfigureAwait(false);
|
||||
if (updateRoleResult.IsError)
|
||||
Result<Updated> updateRoleResult = await 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
|
||||
ErrorOr<RoleEntity?> getRoleResult = await roleRepository.GetByIdAsync(command.RoleId, cancellationToken).ConfigureAwait(false);
|
||||
if (getRoleResult.IsError)
|
||||
Result<RoleEntity?> getRoleResult = await roleRepository.GetByIdAsync(command.RoleId, cancellationToken).ConfigureAwait(false);
|
||||
if (getRoleResult.IsFailure)
|
||||
return getRoleResult.Errors;
|
||||
if (getRoleResult.Value is null)
|
||||
return Errors.Persistence.ErrorPersistingAuthorizationRole;
|
||||
|
||||
+6
-6
@@ -1,5 +1,5 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using ErrorOr;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using Lumina.Application.Common.DataAccess.Entities.Authorization;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Authorization;
|
||||
@@ -20,7 +20,7 @@ namespace Lumina.Application.Core.Admin.Authorization.Roles.Queries.GetRolePermi
|
||||
/// <summary>
|
||||
/// Handler for the query to retrieve the list of authorization permissions for a role.
|
||||
/// </summary>
|
||||
public class GetRolePermissionsQueryHandler : IQueryHandler<GetRolePermissionsQuery, ErrorOr<RolePermissionsResponse>>
|
||||
public class GetRolePermissionsQueryHandler : IQueryHandler<GetRolePermissionsQuery, Result<RolePermissionsResponse>>
|
||||
{
|
||||
private readonly ICurrentUserService _currentUserService;
|
||||
private readonly IAuthorizationService _authorizationService;
|
||||
@@ -48,9 +48,9 @@ public class GetRolePermissionsQueryHandler : IQueryHandler<GetRolePermissionsQu
|
||||
/// <param name="request">The request to be handled.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>
|
||||
/// An <see cref="ErrorOr{TValue}"/> containing either a collection of <see cref="RoleResponse"/>, or an error message.
|
||||
/// An <see cref="Result{TValue}"/> containing either a collection of <see cref="RoleResponse"/>, or an error message.
|
||||
/// </returns>
|
||||
public async Task<ErrorOr<RolePermissionsResponse>> HandleAsync(GetRolePermissionsQuery request, CancellationToken cancellationToken)
|
||||
public async Task<Result<RolePermissionsResponse>> HandleAsync(GetRolePermissionsQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
List<Error> validationResult = _validator.Validate(request);
|
||||
if (validationResult.Count > 0)
|
||||
@@ -61,8 +61,8 @@ public class GetRolePermissionsQueryHandler : IQueryHandler<GetRolePermissionsQu
|
||||
if (!isAdmin)
|
||||
return Errors.Authorization.NotAuthorized;
|
||||
// get the role from the repository and return its role permissions
|
||||
ErrorOr<RoleEntity?> getRoleResult = await _roleRepository.GetByIdAsync(request.RoleId, cancellationToken).ConfigureAwait(false);
|
||||
if (getRoleResult.IsError)
|
||||
Result<RoleEntity?> getRoleResult = await _roleRepository.GetByIdAsync(request.RoleId, cancellationToken).ConfigureAwait(false);
|
||||
if (getRoleResult.IsFailure)
|
||||
return getRoleResult.Errors;
|
||||
else if (getRoleResult.Value is null)
|
||||
return Errors.Authorization.RoleNotFound;
|
||||
|
||||
+6
-6
@@ -1,5 +1,5 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using ErrorOr;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using Lumina.Application.Common.DataAccess.Entities.Authorization;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Authorization;
|
||||
@@ -19,7 +19,7 @@ namespace Lumina.Application.Core.Admin.Authorization.Roles.Queries.GetRoles;
|
||||
/// <summary>
|
||||
/// Handler for the query to retrieve the list of authorization roles.
|
||||
/// </summary>
|
||||
public class GetRolesQueryHandler : IQueryHandler<GetRolesQuery, ErrorOr<IEnumerable<RoleResponse>>>
|
||||
public class GetRolesQueryHandler : IQueryHandler<GetRolesQuery, Result<IEnumerable<RoleResponse>>>
|
||||
{
|
||||
private readonly ICurrentUserService _currentUserService;
|
||||
private readonly IAuthorizationService _authorizationService;
|
||||
@@ -44,15 +44,15 @@ public class GetRolesQueryHandler : IQueryHandler<GetRolesQuery, ErrorOr<IEnumer
|
||||
/// <param name="query">The request to be handled.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>
|
||||
/// An <see cref="ErrorOr{TValue}"/> containing either a collection of <see cref="RoleResponse"/>, or an error message.
|
||||
/// An <see cref="Result{TValue}"/> containing either a collection of <see cref="RoleResponse"/>, or an error message.
|
||||
/// </returns>
|
||||
public async Task<ErrorOr<IEnumerable<RoleResponse>>> HandleAsync(GetRolesQuery query, CancellationToken cancellationToken)
|
||||
public async Task<Result<IEnumerable<RoleResponse>>> HandleAsync(GetRolesQuery query, CancellationToken cancellationToken)
|
||||
{
|
||||
// only admins can see the list of authorization roles
|
||||
bool isAdmin = await _authorizationService.IsInRoleAsync(_currentUserService.UserId!.Value, "Admin", cancellationToken).ConfigureAwait(false);
|
||||
if (!isAdmin)
|
||||
return Errors.Authorization.NotAuthorized;
|
||||
ErrorOr<IEnumerable<RoleEntity>> getRolesResult = await _roleRepository.GetAllAsync(cancellationToken).ConfigureAwait(false);
|
||||
return getRolesResult.Match(value => ErrorOrFactory.From(value.ToResponses()), errors => errors);
|
||||
Result<IEnumerable<RoleEntity>> getRolesResult = await _roleRepository.GetAllAsync(cancellationToken).ConfigureAwait(false);
|
||||
return getRolesResult.Match(value => Result.From(value.ToResponses()), errors => errors);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user