2025-01-10 15:59:51 -05:00
|
|
|
using CH.CQRS.Service.Energy;
|
|
|
|
using CH.CQRS.Service.Energy.Options;
|
2025-01-13 19:19:03 -05:00
|
|
|
using CH.Dal;
|
|
|
|
using CH.Dal.DbEntity;
|
|
|
|
using CH.Dal.Validators;
|
2025-01-10 15:59:51 -05:00
|
|
|
using FluentValidation;
|
2025-01-13 19:19:03 -05:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2025-01-10 15:59:51 -05:00
|
|
|
using OpenHarbor.CQRS.Abstractions;
|
|
|
|
|
2025-01-09 16:14:34 -05:00
|
|
|
namespace CH.CQRS.Command.Energy;
|
|
|
|
|
|
|
|
public class CreateEnergyProviderCommand
|
|
|
|
{
|
2025-01-10 15:59:51 -05:00
|
|
|
public required string Name { get; set; }
|
|
|
|
public bool Active { get; set; }
|
|
|
|
}
|
|
|
|
public class CreateEnergyProviderCommandHandler(EnergyService energyService) : ICommandHandler<CreateEnergyProviderCommand>
|
|
|
|
{
|
|
|
|
public Task HandleAsync(CreateEnergyProviderCommand command, CancellationToken cancellationToken = new CancellationToken())
|
|
|
|
{
|
|
|
|
return energyService.CreateEnergyProviderAsync(new CreateEnergyProviderCommandOptions
|
|
|
|
{
|
|
|
|
Name = command.Name,
|
|
|
|
Active = command.Active
|
|
|
|
},cancellationToken);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class CreateEnergyProviderCommandValidator : AbstractValidator<CreateEnergyProviderCommand>
|
|
|
|
{
|
2025-01-13 19:19:03 -05:00
|
|
|
public CreateEnergyProviderCommandValidator(CHDbContext dbContext)
|
2025-01-10 15:59:51 -05:00
|
|
|
{
|
2025-01-13 19:19:03 -05:00
|
|
|
RuleFor(command => command.Name)
|
|
|
|
.NotEmpty()
|
|
|
|
.MinimumLength(3)
|
|
|
|
.MustAsync(async (name, cancellationToken) =>
|
|
|
|
{
|
|
|
|
var nameInUse = await dbContext.EnergyProviders.AnyAsync(energyProvider => energyProvider.Name == name, cancellationToken);
|
|
|
|
return false == nameInUse;
|
|
|
|
})
|
|
|
|
.WithMessage("This Name is already in use by another energy provider.");
|
2025-01-10 15:59:51 -05:00
|
|
|
}
|
2025-01-09 16:14:34 -05:00
|
|
|
}
|