constellation-api/CH.CQRS/Command/Energy/EnableEnergyRateCommand.cs

34 lines
1016 B
C#

using CH.CQRS.Service.Energy;
using CH.Dal;
using CH.Dal.DbEntity;
using CH.Dal.Validators;
using FluentValidation;
using OpenHarbor.CQRS.Abstractions;
namespace CH.CQRS.Command.Energy;
public class EnableEnergyRateCommand
{
public long RateId { get; set; }
}
public class EnableEnergyRateCommandHandler(EnergyService energyService) : ICommandHandler<EnableEnergyRateCommand>
{
public Task HandleAsync(EnableEnergyRateCommand command, CancellationToken cancellationToken = new CancellationToken())
{
return energyService.EnableEnergyRateAsync(new EnableEnergyRateCommandOptions
{
RateId = command.RateId
}, cancellationToken);
}
}
public class EnableEnergyRateCommandValidator : AbstractValidator<EnableEnergyRateCommand>
{
public EnableEnergyRateCommandValidator(CHDbContext dbContext)
{
RuleFor(command => command.RateId)
.NotEmpty()
.SetValidator(new DbEntityExistValidator<EnergyRate,long>(dbContext))
.WithMessage("The provided Rate Id is invalid.");
}
}