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

49 lines
1.7 KiB
C#

using CH.CQRS.Service.Energy;
using CH.CQRS.Service.Energy.Options;
using CH.Dal;
using CH.Dal.DbEntity;
using CH.Dal.Validators;
using CH.Enum;
using FluentValidation;
using Microsoft.EntityFrameworkCore;
using OpenHarbor.CQRS.Abstractions;
namespace CH.CQRS.Command.Energy;
public class UpdateEnergyRateExceptionCommand
{
public long EnergyRateExceptionId { get; set; }
public decimal? EnergyThreshold { get; set; }
public EnergyRateExceptionThresholdResetType ResetType { get; set; }
public DateTime? StartedAt { get; set; }
public DateTime? EndedAt { get; set; }
}
public class UpdateEnergyRateExceptionCommandHandler(EnergyService energyService) : ICommandHandler<UpdateEnergyRateExceptionCommand>
{
public Task HandleAsync(UpdateEnergyRateExceptionCommand command,
CancellationToken cancellationToken = new CancellationToken())
{
return energyService.UpdateEnergyRateExceptionAsync(new UpdateEnergyRateExceptionCommandOptions
{
EnergyRateExceptionId = command.EnergyRateExceptionId,
EnergyThreshold = command.EnergyThreshold,
ResetType = command.ResetType,
StartedAt = command.StartedAt,
EndedAt = command.EndedAt
},cancellationToken);
}
}
public class UpdateEnergyRateExceptionCommandValidator : AbstractValidator<UpdateEnergyRateExceptionCommand>
{
public UpdateEnergyRateExceptionCommandValidator(CHDbContext dbContext)
{
RuleFor(command => command.EnergyRateExceptionId)
.NotEmpty()
.SetValidator(new DbEntityExistValidator<EnergyRateException,long>(dbContext));
RuleFor(command => command.EnergyThreshold)
.GreaterThan(0)
.When(command => command.EnergyThreshold.HasValue);
}
}