mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 11:11:10 +00:00
Returns the unit of work save changes result
This commit is contained in:
1 parent
f3e465255e
commit
64ef3a1e22
141 files changed
+1388
-1151
No files matched your search
+10
-6
@@ -58,18 +58,18 @@ public class AddRoleCommandHandler : ICommandHandler<AddRoleCommand, Result<Role
|
||||
if (validationResult.Count > 0)
|
||||
return validationResult;
|
||||
|
||||
// an authenticated request must always carry a user identity
|
||||
// An authenticated request must always carry a user identity.
|
||||
Guid? currentUserId = _currentUserService.UserId;
|
||||
if (currentUserId is null)
|
||||
return Errors.Authorization.NotAuthorized;
|
||||
Guid userId = currentUserId.Value;
|
||||
|
||||
// only admins can create authorization roles
|
||||
// Only admins can create authorization roles.
|
||||
bool isAdmin = await _authorizationService.IsInRoleAsync(userId, "Admin", cancellationToken).ConfigureAwait(false);
|
||||
if (!isAdmin)
|
||||
return Errors.Authorization.NotAuthorized;
|
||||
|
||||
// create the new role, with its permissions
|
||||
// Create the new role, with its permissions.
|
||||
RoleEntity newRole = new()
|
||||
{
|
||||
RoleName = request.RoleName,
|
||||
@@ -81,12 +81,16 @@ public class AddRoleCommandHandler : ICommandHandler<AddRoleCommand, Result<Role
|
||||
RoleId = default
|
||||
})]
|
||||
};
|
||||
// save the new role in the repository
|
||||
// Save the new role in the repository.
|
||||
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<Success> saveChangesResult = await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
|
||||
if (saveChangesResult.IsFailure)
|
||||
return saveChangesResult.Errors;
|
||||
|
||||
// Retrieve the newly saved authorization role from the persistence medium and return it.
|
||||
Result<RoleEntity?> getRoleResult = await _unitOfWork.RoleRepository.GetByNameAsync(request.RoleName, cancellationToken).ConfigureAwait(false);
|
||||
if (getRoleResult.IsFailure)
|
||||
return getRoleResult.Errors;
|
||||
|
||||
+13
-12
@@ -1,20 +1,16 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using Lumina.Application.Common.DataAccess.Entities.Authorization;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Authorization;
|
||||
using Lumina.Application.Common.DataAccess.UoW;
|
||||
using Lumina.Application.Common.Errors;
|
||||
using Lumina.Application.Common.Infrastructure.Authentication;
|
||||
using Lumina.Application.Common.Infrastructure.Authorization;
|
||||
using Lumina.Application.Common.Infrastructure.Validation;
|
||||
using Lumina.Contracts.Responses.Authorization;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Lumina.Application.Common.Mapping.Authorization;
|
||||
using Lumina.Application.Common.Infrastructure.Authorization;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Core.Admin.Authorization.Roles.Commands.DeleteRole;
|
||||
@@ -56,7 +52,7 @@ public class DeleteRoleCommandHandler : ICommandHandler<DeleteRoleCommand, Resul
|
||||
if (validationResult.Count > 0)
|
||||
return validationResult;
|
||||
|
||||
// an authenticated request must always carry a user identity
|
||||
// An authenticated request must always carry a user identity.
|
||||
Guid? currentUserId = _currentUserService.UserId;
|
||||
if (currentUserId is null)
|
||||
return Errors.Authorization.NotAuthorized;
|
||||
@@ -67,19 +63,24 @@ public class DeleteRoleCommandHandler : ICommandHandler<DeleteRoleCommand, Resul
|
||||
if (!isAdmin)
|
||||
return Errors.Authorization.NotAuthorized;
|
||||
|
||||
// check if a role with the requested Id exists
|
||||
Result<RoleEntity?> getExistingRoleResult = await _unitOfWork.RoleRepository.GetByIdAsync(command.RoleId, cancellationToken).ConfigureAwait(false);
|
||||
// Check if a role with the requested Id exists.
|
||||
Result<RoleEntity?> getExistingRoleResult = await _unitOfWork.RoleRepository.GetByIdAsync(command.RoleId, cancellationToken: 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
|
||||
|
||||
// Delete the role and its permissions.
|
||||
Result<Deleted> deleteRoleResult = await _unitOfWork.RoleRepository.DeleteByIdAsync(command.RoleId, cancellationToken).ConfigureAwait(false);
|
||||
if (deleteRoleResult.IsFailure)
|
||||
return deleteRoleResult.Errors;
|
||||
await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
|
||||
return deleteRoleResult.Value;
|
||||
|
||||
Result<Success> saveChangesResult = await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
|
||||
if (saveChangesResult.IsFailure)
|
||||
return saveChangesResult.Errors;
|
||||
|
||||
return Result.Deleted;
|
||||
}
|
||||
}
|
||||
+14
-11
@@ -1,8 +1,6 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Application.Common.CQRS;
|
||||
using Lumina.Application.Common.DataAccess.Entities.Authorization;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Authorization;
|
||||
using Lumina.Application.Common.DataAccess.UoW;
|
||||
using Lumina.Application.Common.Errors;
|
||||
using Lumina.Application.Common.Infrastructure.Authentication;
|
||||
@@ -10,6 +8,7 @@ using Lumina.Application.Common.Infrastructure.Authorization;
|
||||
using Lumina.Application.Common.Infrastructure.Validation;
|
||||
using Lumina.Application.Common.Mapping.Authorization;
|
||||
using Lumina.Contracts.Responses.Authorization;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@@ -58,37 +57,41 @@ public class UpdateRoleCommandHandler : ICommandHandler<UpdateRoleCommand, Resul
|
||||
if (validationResult.Count > 0)
|
||||
return validationResult;
|
||||
|
||||
// an authenticated request must always carry a user identity
|
||||
// An authenticated request must always carry a user identity.
|
||||
Guid? currentUserId = _currentUserService.UserId;
|
||||
if (currentUserId is null)
|
||||
return Errors.Authorization.NotAuthorized;
|
||||
Guid userId = currentUserId.Value;
|
||||
|
||||
// only admins can update authorization roles
|
||||
// Only admins can update authorization roles.
|
||||
bool isAdmin = await _authorizationService.IsInRoleAsync(userId, "Admin", cancellationToken).ConfigureAwait(false);
|
||||
if (!isAdmin)
|
||||
return Errors.Authorization.NotAuthorized;
|
||||
|
||||
// update the role and its permissions
|
||||
// Update the role and its permissions.
|
||||
RoleEntity newRole = new()
|
||||
{
|
||||
Id = command.RoleId,
|
||||
RoleName = command.RoleName,
|
||||
RolePermissions = command.Permissions.Select(permissionId => new RolePermissionEntity()
|
||||
RolePermissions = [.. command.Permissions.Select(permissionId => new RolePermissionEntity()
|
||||
{
|
||||
PermissionId = permissionId,
|
||||
Permission = null!,
|
||||
Role = null!,
|
||||
RoleId = default
|
||||
}).ToList()
|
||||
})]
|
||||
};
|
||||
// save the updated role in the repository
|
||||
// Save the updated role in the repository.
|
||||
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 _unitOfWork.RoleRepository.GetByIdAsync(command.RoleId, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
Result<Success> saveChangesResult = await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
|
||||
if (saveChangesResult.IsFailure)
|
||||
return saveChangesResult.Errors;
|
||||
|
||||
// Retrieve the updated authorization role from the persistence medium and return it.
|
||||
Result<RoleEntity?> getRoleResult = await _unitOfWork.RoleRepository.GetByIdAsync(command.RoleId, cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
if (getRoleResult.IsFailure)
|
||||
return getRoleResult.Errors;
|
||||
if (getRoleResult.Value is null)
|
||||
|
||||
Reference in new issue
Block a user