using CH.CQRS.Query.EnergyProvider; using CH.Dal; 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 DateTime? DiabledAt { 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 { public async Task> GetQueryableAsync(object query, CancellationToken cancellationToken = default) { long? energyProviderId = null; if (query is IDynamicQueryParams queryParams) { var parameters = queryParams.GetParams(); energyProviderId = parameters.EnergyProviderId; } if (false == energyProviderId.HasValue) return Enumerable.Empty().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, DiabledAt = energyRate.DisabledAt, CreatedAt = energyRate.CreatedAt, UpdatedAt = energyRate.UpdatedAt }); return result; } }