0
0
mirror of https://github.com/alex289/CleanArchitecture.git synced 2025-06-30 02:31:08 +00:00
CleanArchitecture/CleanArchitecture.Domain/Commands/Tenants/CreateTenant/CreateTenantCommandHandler.cs
2023-09-02 12:32:36 +02:00

73 lines
2.2 KiB
C#

using System.Threading;
using System.Threading.Tasks;
using CleanArchitecture.Domain.Entities;
using CleanArchitecture.Domain.Enums;
using CleanArchitecture.Domain.Errors;
using CleanArchitecture.Domain.Interfaces;
using CleanArchitecture.Domain.Interfaces.Repositories;
using CleanArchitecture.Domain.Notifications;
using CleanArchitecture.Shared.Events.Tenant;
using MediatR;
namespace CleanArchitecture.Domain.Commands.Tenants.CreateTenant;
public sealed class CreateTenantCommandHandler : CommandHandlerBase,
IRequestHandler<CreateTenantCommand>
{
private readonly ITenantRepository _tenantRepository;
private readonly IUser _user;
public CreateTenantCommandHandler(
IMediatorHandler bus,
IUnitOfWork unitOfWork,
INotificationHandler<DomainNotification> notifications,
ITenantRepository tenantRepository,
IUser user) : base(bus, unitOfWork, notifications)
{
_tenantRepository = tenantRepository;
_user = user;
}
public async Task Handle(CreateTenantCommand request, CancellationToken cancellationToken)
{
if (!await TestValidityAsync(request))
{
return;
}
if (_user.GetUserRole() != UserRole.Admin)
{
await NotifyAsync(
new DomainNotification(
request.MessageType,
$"No permission to create tenant {request.AggregateId}",
ErrorCodes.InsufficientPermissions));
return;
}
if (await _tenantRepository.ExistsAsync(request.AggregateId))
{
await NotifyAsync(
new DomainNotification(
request.MessageType,
$"There is already a tenant with Id {request.AggregateId}",
DomainErrorCodes.Tenant.TenantAlreadyExists));
return;
}
var tenant = new Tenant(
request.AggregateId,
request.Name);
_tenantRepository.Add(tenant);
if (await CommitAsync())
{
await Bus.RaiseEventAsync(new TenantCreatedEvent(
tenant.Id,
tenant.Name));
}
}
}