constellation-api/CH.CQRS/Query/EnergyRateException/EnergyRateExceptionQuery.cs

74 lines
2.2 KiB
C#

using CH.CQRS.Query.EnergyProvider;
using CH.Dal;
using CH.Enum;
using Microsoft.EntityFrameworkCore;
using OpenHarbor.CQRS.Abstractions;
using OpenHarbor.CQRS.DynamicQuery.Abstractions;
namespace CH.CQRS.Query.EnergyRateException;
public class EnergyRateExceptionQuery
{
}
public class EnergyRateExceptionQueryItem
{
public long Id { get; set; }
public long? RateId { get; set; }
public string? Name { get; set; }
public decimal? EnergyThreshold { get; set; }
public EnergyRateExceptionThresholdResetType ResetType { get; set; }
public DateTime? StartedAt { get; set; }
public DateTime? EndedAt { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime? UpdatedAt { get; set; }
}
public class EnergyRateExceptionQueryParams
{
public long EnergyRateId { get; set; }
}
public class EnergyRateExceptionQueryableProvider(CHDbContext dbContext) : IQueryableProviderOverride<EnergyRateExceptionQueryItem>
{
public async Task<IQueryable<EnergyRateExceptionQueryItem>> GetQueryableAsync(object query, CancellationToken cancellationToken = default)
{
long? energyRateId = null;
if (query is IDynamicQueryParams<EnergyRateExceptionQueryParams> queryParams)
{
var parameters = queryParams.GetParams();
energyRateId = parameters.EnergyRateId;
}
if(false == energyRateId.HasValue)
return Enumerable.Empty<EnergyRateExceptionQueryItem>().AsQueryable();
var queryable = dbContext.EnergyRateExceptions
.AsNoTracking()
.AsQueryable();
var result = queryable
.Where(energyRateException => energyRateException.RateId == energyRateId)
.Select(energyRateException => new EnergyRateExceptionQueryItem
{
Id = energyRateException.Id,
Name = energyRateException.Name,
RateId = energyRateException.RateId,
EnergyThreshold = energyRateException.EnergyThreshold,
ResetType = energyRateException.ResetType,
StartedAt = energyRateException.StartedAt,
EndedAt = energyRateException.EndedAt,
CreatedAt = energyRateException.CreatedAt,
UpdatedAt = energyRateException.UpdatedAt,
})
.OrderBy(energyRateException => energyRateException.Id);
return result;
}
}