45 lines
1.4 KiB
C#
45 lines
1.4 KiB
C#
using DigitalOps.Authority.Services;
|
|
using DigitalOps.Authority.Validators;
|
|
using DigitalOps.CQRS.Services.Client;
|
|
using DigitalOps.CQRS.Services.Client.Options;
|
|
using DigitalOps.Dal;
|
|
using DigitalOps.Dal.DbEntity;
|
|
using DigitalOps.Dal.Validators;
|
|
using FluentValidation;
|
|
using OpenHarbor.CQRS.Abstractions;
|
|
|
|
namespace DigitalOps.CQRS.Commands.Client;
|
|
|
|
public class AddClientCommand
|
|
{
|
|
public long OrganizationId { get; set; }
|
|
public required string Name { get; set; }
|
|
}
|
|
|
|
public class AddClientCommandHandler(ClientService clientService) : ICommandHandler<AddClientCommand>
|
|
{
|
|
public async Task HandleAsync(AddClientCommand command, CancellationToken cancellationToken = default)
|
|
{
|
|
await clientService.AddClientAsync(new AddClientOptions
|
|
{
|
|
Name = command.Name,
|
|
OrganizationId = command.OrganizationId
|
|
}, cancellationToken);
|
|
}
|
|
}
|
|
|
|
public class AddClientCommandValidator : AbstractValidator<AddClientCommand>
|
|
{
|
|
public AddClientCommandValidator(MainDbContext dbContext, UserIdentityService userIdentityService)
|
|
{
|
|
RuleFor(command => command.Name)
|
|
.NotEmpty()
|
|
.MinimumLength(3);
|
|
|
|
RuleFor(command => command.OrganizationId)
|
|
.Cascade(CascadeMode.Stop)
|
|
.NotEmpty()
|
|
.SetValidator(new DbEntityExistValidator<Dal.DbEntity.Organization, long>(dbContext))
|
|
.SetValidator(new HasAccessToOrganizationValidator(OrganizationRole.Member, dbContext, userIdentityService));
|
|
}
|
|
} |