change energy rate energy rate exception and energy provider query to dynamic query to pass parameters

This commit is contained in:
DavidGudEnough 2025-01-27 18:58:42 -05:00
parent 0ba2e45b38
commit 6fee86170b
Signed by: david.nguyen
GPG Key ID: 0B95DC36355BEB37
11 changed files with 202 additions and 48 deletions

View File

@ -6,8 +6,7 @@ namespace CH.CQRS.Query.CryptoStat;
public class CryptoStatQuery public class CryptoStatQuery
{ {
// public required string CoinName { get; set; }
// public required string Currency { get; set; }
} }
public class CryptoStatQueryHandler(CoinMarketCapService coinMarketCapService) : IQueryHandler<CryptoStatQuery, CryptoStatQueryResult> public class CryptoStatQueryHandler(CoinMarketCapService coinMarketCapService) : IQueryHandler<CryptoStatQuery, CryptoStatQueryResult>
{ {

View File

@ -0,0 +1,16 @@
using CH.Dal;
using Microsoft.Extensions.DependencyInjection;
using OpenHarbor.CQRS.DynamicQuery;
using PoweredSoft.Module.Abstractions;
namespace CH.CQRS.Query.EnergyProvider;
public class EnergyProviderQueriesModule : IModule
{
public IServiceCollection ConfigureServices(IServiceCollection services)
{
services.AddDynamicQuery<EnergyProviderQueryItem>()
.AddQueryableProviderOverride<EnergyProviderQueryItem, EnergyProviderQueryableProvider>();
return services;
}
}

View File

@ -1,6 +1,7 @@
using CH.Dal; using CH.Dal;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using OpenHarbor.CQRS.Abstractions; using OpenHarbor.CQRS.Abstractions;
using PoweredSoft.DynamicLinq;
namespace CH.CQRS.Query.EnergyProvider; namespace CH.CQRS.Query.EnergyProvider;
@ -8,15 +9,46 @@ public class EnergyProviderQuery
{ {
} }
public class EnergyProviderQueryHandler(CHDbContext dbContext) : IQueryHandler<EnergyProviderQuery, EnergyProviderQueryResult>
public class EnergyProviderQueryItem
{ {
public async Task<EnergyProviderQueryResult> HandleAsync(EnergyProviderQuery query, CancellationToken cancellationToken = new CancellationToken()) public long Id { get; set; }
{
var energyProviders = await dbContext.EnergyProviders.ToListAsync(cancellationToken); public string Name { get; set; } = null!;
var energyProviderQueryResult = new EnergyProviderQueryResult
{ public bool Active { get; set; }
Data = energyProviders
}; public DateTime? DisabledAt { get; set; }
return energyProviderQueryResult;
public DateTime CreatedAt { get; set; }
public DateTime? UpdatedAt { get; set; }
}
public class EnergyRateQueryParams
{
}
public class EnergyProviderQueryableProvider(CHDbContext dbContext) : IQueryableProviderOverride<EnergyProviderQueryItem>
{
public async Task<IQueryable<EnergyProviderQueryItem>> GetQueryableAsync(object query, CancellationToken cancellationToken = default)
{
var queryable = dbContext.EnergyProviders
.AsNoTracking()
.AsQueryable();
var result = queryable
.Select(energyProvider => new EnergyProviderQueryItem
{
Id = energyProvider.Id,
Name = energyProvider.Name,
Active = energyProvider.Active,
DisabledAt = energyProvider.DisabledAt,
CreatedAt = energyProvider.CreatedAt,
UpdatedAt = energyProvider.UpdatedAt
});
return result;
} }
} }

View File

@ -1,6 +0,0 @@
namespace CH.CQRS.Query.EnergyProvider;
public class EnergyProviderQueryResult
{
public required List<Dal.DbEntity.EnergyProvider> Data { get; set; }
}

View File

@ -0,0 +1,17 @@
using CH.Dal;
using Microsoft.Extensions.DependencyInjection;
using OpenHarbor.CQRS.DynamicQuery;
using PoweredSoft.Module.Abstractions;
namespace CH.CQRS.Query.EnergyRate;
public class EnergyRateQueriesModule : IModule
{
public IServiceCollection ConfigureServices(IServiceCollection services)
{
services.AddDynamicQueryWithParams<EnergyRateQueryItem, EnergyRateQueryParams>()
.AddQueryableProviderOverride<EnergyRateQueryItem, EnergyRateQueryableProvider>();
return services;
}
}

View File

@ -2,22 +2,63 @@ using CH.CQRS.Query.EnergyProvider;
using CH.Dal; using CH.Dal;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using OpenHarbor.CQRS.Abstractions; using OpenHarbor.CQRS.Abstractions;
using OpenHarbor.CQRS.DynamicQuery.Abstractions;
namespace CH.CQRS.Query.EnergyRate; namespace CH.CQRS.Query.EnergyRate;
public class EnergyRateQuery public class EnergyRateQuery
{ {
} }
public class EnergyRateQueryHandler(CHDbContext dbContext) : IQueryHandler<EnergyRateQuery, EnergyRateQueryResult>
public class EnergyRateQueryItem
{ {
public async Task<EnergyRateQueryResult> HandleAsync(EnergyRateQuery query, CancellationToken cancellationToken = new CancellationToken()) 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<EnergyRateQueryItem>
{
public async Task<IQueryable<EnergyRateQueryItem>> GetQueryableAsync(object query, CancellationToken cancellationToken = default)
{ {
var energyRates = await dbContext.EnergyRates.ToListAsync(cancellationToken); long? energyProviderId = null;
var energyRateQueryResult = new EnergyRateQueryResult if (query is IDynamicQueryParams<EnergyRateQueryParams> queryParams)
{ {
Data = energyRates var parameters = queryParams.GetParams();
}; energyProviderId = parameters.EnergyProviderId;
return energyRateQueryResult; }
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,
DiabledAt = energyRate.DisabledAt,
CreatedAt = energyRate.CreatedAt,
UpdatedAt = energyRate.UpdatedAt
});
return result;
} }
} }

View File

@ -1,6 +0,0 @@
namespace CH.CQRS.Query.EnergyRate;
public class EnergyRateQueryResult
{
public required List<Dal.DbEntity.EnergyRate> Data { get; set; }
}

View File

@ -0,0 +1,16 @@
using CH.Dal;
using Microsoft.Extensions.DependencyInjection;
using OpenHarbor.CQRS.DynamicQuery;
using PoweredSoft.Module.Abstractions;
namespace CH.CQRS.Query.EnergyRateException;
public class EnergyRateExceptionQueriesModule : IModule
{
public IServiceCollection ConfigureServices(IServiceCollection services)
{
services.AddDynamicQueryWithParams<EnergyRateExceptionQueryItem, EnergyRateExceptionQueryParams>()
.AddQueryableProviderOverride<EnergyRateExceptionQueryItem, EnergyRateExceptionQueryableProvider>();
return services;
}
}

View File

@ -1,22 +1,72 @@
using CH.CQRS.Query.EnergyProvider;
using CH.Dal; using CH.Dal;
using CH.Enum;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using OpenHarbor.CQRS.Abstractions; using OpenHarbor.CQRS.Abstractions;
using OpenHarbor.CQRS.DynamicQuery.Abstractions;
namespace CH.CQRS.Query.EnergyRateException; namespace CH.CQRS.Query.EnergyRateException;
public class EnergyRateExceptionQuery public class EnergyRateExceptionQuery
{ {
} }
public class EnergyRateExceptionQueryHandler(CHDbContext dbContext) : IQueryHandler<EnergyRateExceptionQuery, EnergyRateExceptionQueryResult>
public class EnergyRateExceptionQueryItem
{ {
public async Task<EnergyRateExceptionQueryResult> HandleAsync(EnergyRateExceptionQuery query, CancellationToken cancellationToken = new CancellationToken()) 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)
{ {
var energyRateExceptions = await dbContext.EnergyRateExceptions.ToListAsync(cancellationToken); long? energyRateId = null;
var energyRateExceptionsQueryResult = new EnergyRateExceptionQueryResult if (query is IDynamicQueryParams<EnergyRateExceptionQueryParams> queryParams)
{ {
Data = energyRateExceptions, var parameters = queryParams.GetParams();
}; energyRateId = parameters.EnergyRateId;
return energyRateExceptionsQueryResult; }
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,
EnergyThreshold = energyRateException.EnergyThreshold,
ResetType = energyRateException.ResetType,
StartedAt = energyRateException.StartedAt,
EndedAt = energyRateException.EndedAt,
CreatedAt = energyRateException.CreatedAt,
UpdatedAt = energyRateException.UpdatedAt,
});
return result;
} }
} }

View File

@ -1,6 +0,0 @@
namespace CH.CQRS.Query.EnergyRateException;
public class EnergyRateExceptionQueryResult
{
public required List<Dal.DbEntity.EnergyRateException> Data { get; set; }
}

View File

@ -18,10 +18,11 @@ public class QueryModule : IModule
public IServiceCollection ConfigureServices(IServiceCollection services) public IServiceCollection ConfigureServices(IServiceCollection services)
{ {
services.AddQuery<HealthQuery, HealthQueryResult, HealthQueryHandler>(); services.AddQuery<HealthQuery, HealthQueryResult, HealthQueryHandler>();
services.AddQuery<EnergyProviderQuery, EnergyProviderQueryResult, EnergyProviderQueryHandler>();
services.AddQuery<EnergyRateQuery, EnergyRateQueryResult, EnergyRateQueryHandler>();
services.AddQuery<EnergyRateExceptionQuery, EnergyRateExceptionQueryResult, EnergyRateExceptionQueryHandler>();
services.AddQuery<CryptoStatQuery, CryptoStatQueryResult, CryptoStatQueryHandler>(); services.AddQuery<CryptoStatQuery, CryptoStatQueryResult, CryptoStatQueryHandler>();
services.AddModule<EnergyProviderQueriesModule>();
services.AddModule<EnergyRateQueriesModule>();
services.AddModule<EnergyRateExceptionQueriesModule>();
return services; return services;
} }
} }