constellation-api/CH.CQRS/Service/Energy/EnergyService.cs

187 lines
6.3 KiB
C#
Raw Normal View History

using CH.CQRS.Command.Energy;
using CH.CQRS.Service.Energy.Options;
using CH.Dal;
using CH.Dal.DbEntity;
using CH.Enum;
using Microsoft.EntityFrameworkCore;
namespace CH.CQRS.Service.Energy;
public class EnergyService(CHDbContext dbContext)
{
public async Task CreateEnergyProviderAsync(CreateEnergyProviderCommandOptions options, CancellationToken cancellationToken)
{
var energyProvider = new EnergyProvider
{
Name = options.Name,
CreatedAt = DateTime.UtcNow,
Active = options.Active
};
await dbContext.AddAsync(energyProvider, cancellationToken);
await dbContext.SaveChangesAsync(cancellationToken);
}
public async Task CreateEnergyRateAsync(CreateEnergyRateCommandOptions options, CancellationToken cancellationToken)
{
var provider = await dbContext.EnergyProviders.FirstOrDefaultAsync(provider => provider.Id == options.ProviderId, cancellationToken);
var energyRate = new EnergyRate
{
Name = options.Name,
2025-01-13 19:19:03 -05:00
Currency = options.Currency,
Price = options.Price,
CreatedAt = DateTime.UtcNow,
Provider = provider,
Active = options.Active
};
await dbContext.AddAsync(energyRate, cancellationToken);
await dbContext.SaveChangesAsync(cancellationToken);
}
public async Task CreateEnergyRateExceptionAsync(CreateEnergyRateExceptionCommandOptions options,
CancellationToken cancellationToken)
{
var energyRate = await dbContext.EnergyRates.FirstOrDefaultAsync(energyRate => energyRate.Id == options.RateId,cancellationToken);
var energyRateException = new EnergyRateException
{
Name = options.Name,
CreatedAt = DateTime.UtcNow,
Rate = energyRate,
StartedAt = options.StartedAt,
EndedAt = options.EndedAt,
EnergyThreshold = options.EnergyThreshold,
2025-01-13 19:19:03 -05:00
ResetType = options.ResetType,
};
await dbContext.AddAsync(energyRateException, cancellationToken);
await dbContext.SaveChangesAsync(cancellationToken);
}
public async Task UpdateEnergyProviderAsync(UpdateEnergyProviderCommandOptions options,
CancellationToken cancellationToken)
{
2025-01-13 19:19:03 -05:00
var provider = await dbContext.EnergyProviders
.AsTracking()
.FirstOrDefaultAsync(provider => provider.Id == options.ProviderId, cancellationToken);
if (provider is null)
return;
provider.Name = options.Name;
provider.UpdatedAt = DateTime.UtcNow;
await dbContext.SaveChangesAsync(cancellationToken);
}
2025-01-13 19:19:03 -05:00
public async Task UpdateEnergyRateValueAsync(UpdateEnergyRateCommandOptions options, CancellationToken cancellationToken)
{
var energyRate = await dbContext.EnergyRates
.AsTracking()
.FirstOrDefaultAsync(energyRate => energyRate.Id == options.RateId, cancellationToken);
if (energyRate is null)
return;
if (options.Rate.HasValue)
energyRate.Price = options.Rate.Value;
if (false == string.IsNullOrWhiteSpace(options.Name))
energyRate.Name = options.Name;
energyRate.UpdatedAt = DateTime.UtcNow;
}
public async Task UpdateEnergyRateAsync(UpdateEnergyRateCommandOptions options, CancellationToken cancellationToken)
{
2025-01-13 19:19:03 -05:00
if (options.StartedAt.HasValue)
{
2025-01-13 19:19:03 -05:00
var energyRateUpdate = new EnergyRateUpdate
{
RateId = options.RateId,
Rate = options.Rate,
Name = options.Name ?? "",
StartedAt = options.StartedAt.Value,
};
await dbContext.AddAsync(energyRateUpdate, cancellationToken);
}
else
{
await UpdateEnergyRateValueAsync(options, cancellationToken);
}
await dbContext.SaveChangesAsync(cancellationToken);
}
public async Task UpdateEnergyRateExceptionAsync(UpdateEnergyRateExceptionCommandOptions options,
CancellationToken cancellationToken)
{
var energyRateException = await dbContext.EnergyRateExceptions
2025-01-13 19:19:03 -05:00
.AsTracking()
.FirstOrDefaultAsync(rateException => rateException.Id == options.EnergyRateExceptionId, cancellationToken);
2025-01-13 19:19:03 -05:00
if (energyRateException is null)
return;
if (options.EnergyThreshold.HasValue)
energyRateException.EnergyThreshold = options.EnergyThreshold;
2025-01-13 19:19:03 -05:00
if (options.StartedAt.HasValue)
energyRateException.StartedAt = options.StartedAt;
2025-01-13 19:19:03 -05:00
if (options.EndedAt.HasValue)
energyRateException.EndedAt = options.EndedAt;
2025-01-13 19:19:03 -05:00
energyRateException.UpdatedAt = DateTime.UtcNow;
if (energyRateException.ResetType != options.ResetType)
{
energyRateException.ResetType = options.ResetType;
}
2025-01-13 19:19:03 -05:00
await dbContext.SaveChangesAsync(cancellationToken);
}
public async Task DisableEnergyProviderAsync(DisableEnergyProviderCommandOptions options,
CancellationToken cancellationToken)
{
var energyProvider = await dbContext.EnergyProviders.FirstOrDefaultAsync(provider => provider.Id == options.ProviderId, cancellationToken);
if (null != energyProvider)
{
energyProvider.Active = false;
2025-01-13 19:19:03 -05:00
energyProvider.DisabledAt = options.DisabledAt ?? DateTime.UtcNow;
await dbContext.SaveChangesAsync(cancellationToken);
}
}
public async Task DisableEnergyRateAsync(DisableEnergyRateCommandOptions options, CancellationToken cancellationToken)
{
var energyRate = await dbContext.EnergyRates.FirstOrDefaultAsync(rate => rate.Id == options.RateId, cancellationToken);
if (null != energyRate)
{
energyRate.Active = false;
2025-01-13 19:19:03 -05:00
energyRate.DisabledAt = options.DisabledAt ?? DateTime.UtcNow;
await dbContext.SaveChangesAsync(cancellationToken);
}
}
public async Task EnableEnergyProviderAsync(EnableEnergyProviderCommandOptions options,
CancellationToken cancellationToken)
{
var energyProvider = await dbContext.EnergyProviders.FirstOrDefaultAsync(provider => provider.Id == options.ProviderId, cancellationToken);
if (null != energyProvider)
{
energyProvider.Active = true;
energyProvider.DisabledAt = null;
await dbContext.SaveChangesAsync(cancellationToken);
}
}
public async Task EnableEnergyRateAsync(EnableEnergyRateCommandOptions options, CancellationToken cancellationToken)
{
var energyRate = await dbContext.EnergyRates.FirstOrDefaultAsync(rate => rate.Id == options.RateId, cancellationToken);
if (null != energyRate)
{
energyRate.Active = true;
energyRate.DisabledAt = null;
await dbContext.SaveChangesAsync(cancellationToken);
}
}
}