constellation-api/CH.CQRS/Query/EnergyRate/EnergyRateQuery.cs

68 lines
2.0 KiB
C#
Raw Normal View History

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.EnergyRate;
public class EnergyRateQuery
{
}
public class EnergyRateQueryItem
{
public long Id { get; set; }
public long? ProviderId { get; set; }
public required string Name { get; set; }
public decimal? Rate { get; set; }
public bool Active { get; set; }
public Currency Currency { get; set; }
public DateTime? DisabledAt { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime? UpdatedAt { get; set; }
}
public class EnergyRateQueryParams
{
public long EnergyProviderId { get; set; }
}
public class EnergyRateQueryableProvider(CHDbContext dbContext) : IQueryableProviderOverride<EnergyRateQueryItem>
{
public async Task<IQueryable<EnergyRateQueryItem>> GetQueryableAsync(object query, CancellationToken cancellationToken = default)
{
long? energyProviderId = null;
if (query is IDynamicQueryParams<EnergyRateQueryParams> queryParams)
{
var parameters = queryParams.GetParams();
energyProviderId = parameters.EnergyProviderId;
}
if (false == energyProviderId.HasValue)
return Enumerable.Empty<EnergyRateQueryItem>().AsQueryable();
var queryable = dbContext.EnergyRates
.AsNoTracking()
.AsQueryable();
var result = queryable
.Where(energyRate => energyRate.ProviderId == energyProviderId)
.Select(energyRate => new EnergyRateQueryItem
{
Id = energyRate.Id,
ProviderId = energyRate.ProviderId,
Name = energyRate.Name,
Rate = energyRate.Rate,
Active = energyRate.Active,
Currency = energyRate.Currency,
DisabledAt = energyRate.DisabledAt,
CreatedAt = energyRate.CreatedAt,
UpdatedAt = energyRate.UpdatedAt
})
.OrderBy(energyRate => energyRate.Id);
return result;
}
}