mirror of
https://github.com/alex289/CleanArchitecture.git
synced 2025-06-29 18:21:08 +00:00
68 lines
2.0 KiB
C#
68 lines
2.0 KiB
C#
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using CleanArchitecture.Domain.Enums;
|
|
using CleanArchitecture.Domain.Errors;
|
|
using CleanArchitecture.Domain.Events.User;
|
|
using CleanArchitecture.Domain.Interfaces;
|
|
using CleanArchitecture.Domain.Interfaces.Repositories;
|
|
using CleanArchitecture.Domain.Notifications;
|
|
using MediatR;
|
|
|
|
namespace CleanArchitecture.Domain.Commands.Users.DeleteUser;
|
|
|
|
public sealed class DeleteUserCommandHandler : CommandHandlerBase,
|
|
IRequestHandler<DeleteUserCommand>
|
|
{
|
|
private readonly IUser _user;
|
|
private readonly IUserRepository _userRepository;
|
|
|
|
public DeleteUserCommandHandler(
|
|
IMediatorHandler bus,
|
|
IUnitOfWork unitOfWork,
|
|
INotificationHandler<DomainNotification> notifications,
|
|
IUserRepository userRepository,
|
|
IUser user) : base(bus, unitOfWork, notifications)
|
|
{
|
|
_userRepository = userRepository;
|
|
_user = user;
|
|
}
|
|
|
|
public async Task Handle(DeleteUserCommand request, CancellationToken cancellationToken)
|
|
{
|
|
if (!await TestValidityAsync(request))
|
|
{
|
|
return;
|
|
}
|
|
|
|
var user = await _userRepository.GetByIdAsync(request.UserId);
|
|
|
|
if (user == null)
|
|
{
|
|
await NotifyAsync(
|
|
new DomainNotification(
|
|
request.MessageType,
|
|
$"There is no User with Id {request.UserId}",
|
|
ErrorCodes.ObjectNotFound));
|
|
|
|
return;
|
|
}
|
|
|
|
if (_user.GetUserId() != request.UserId && _user.GetUserRole() != UserRole.Admin)
|
|
{
|
|
await NotifyAsync(
|
|
new DomainNotification(
|
|
request.MessageType,
|
|
$"No permission to delete user {request.UserId}",
|
|
ErrorCodes.InsufficientPermissions));
|
|
|
|
return;
|
|
}
|
|
|
|
_userRepository.Remove(user);
|
|
|
|
if (await CommitAsync())
|
|
{
|
|
await _bus.RaiseEventAsync(new UserDeletedEvent(request.UserId));
|
|
}
|
|
}
|
|
} |