From 27c06ce445c95e11477255ac072591882d90d692 Mon Sep 17 00:00:00 2001 From: DavidGudEnough Date: Thu, 30 Jan 2025 15:56:40 -0500 Subject: [PATCH] add query all for energy rate, change name of queryitem --- CH.Api/Program.cs | 1 - .../Command/Energy/UpdateEnergyRateCommand.cs | 9 +--- .../EnergyProvider/EnergyProviderQuery.cs | 6 +-- .../EnergyRate/EnergyRateQueriesModule.cs | 3 +- CH.CQRS/Query/EnergyRate/EnergyRateQuery.cs | 10 ++-- .../Query/EnergyRate/EnergyRateQueryAll.cs | 47 +++++++++++++++++++ .../EnergyRateExceptionQuery.cs | 4 +- 7 files changed, 63 insertions(+), 17 deletions(-) create mode 100644 CH.CQRS/Query/EnergyRate/EnergyRateQueryAll.cs diff --git a/CH.Api/Program.cs b/CH.Api/Program.cs index c49f50f..f9fdc3f 100644 --- a/CH.Api/Program.cs +++ b/CH.Api/Program.cs @@ -41,7 +41,6 @@ builder.Services.AddDefaultCommandDiscovery(); builder.Services.AddDefaultQueryDiscovery(); builder.Services.AddFluentValidation(); builder.Services.AddModule(); -builder.Services.AddHttpClient(); builder.Services.AddDefaultCommandDiscovery(); builder.Services.AddDefaultQueryDiscovery(); if (builder.Configuration.GetValue("Swagger:Enable")) diff --git a/CH.CQRS/Command/Energy/UpdateEnergyRateCommand.cs b/CH.CQRS/Command/Energy/UpdateEnergyRateCommand.cs index 96890f7..c8f07db 100644 --- a/CH.CQRS/Command/Energy/UpdateEnergyRateCommand.cs +++ b/CH.CQRS/Command/Energy/UpdateEnergyRateCommand.cs @@ -40,14 +40,7 @@ public class UpdateEnergyRateCommandValidator : AbstractValidator command.Name) .NotEmpty() - .MinimumLength(3) - .When(command => false == String.IsNullOrWhiteSpace(command.Name)) - .MustAsync(async (name, cancellationToken) => - { - var nameInUse = await dbContext.EnergyRates.AnyAsync(energyRate => energyRate.Name == name, cancellationToken); - return false == nameInUse; - }) - .WithMessage("This Name is already in use by another energy rate."); + .MinimumLength(3); RuleFor(command => command.RateId) .NotEmpty() diff --git a/CH.CQRS/Query/EnergyProvider/EnergyProviderQuery.cs b/CH.CQRS/Query/EnergyProvider/EnergyProviderQuery.cs index e7eb263..10403d4 100644 --- a/CH.CQRS/Query/EnergyProvider/EnergyProviderQuery.cs +++ b/CH.CQRS/Query/EnergyProvider/EnergyProviderQuery.cs @@ -25,7 +25,7 @@ public class EnergyProviderQueryItem public DateTime? UpdatedAt { get; set; } } -public class EnergyRateQueryParams +public class EnergyProviderQueryParams { } @@ -46,8 +46,8 @@ public class EnergyProviderQueryableProvider(CHDbContext dbContext) : IQueryable DisabledAt = energyProvider.DisabledAt, CreatedAt = energyProvider.CreatedAt, UpdatedAt = energyProvider.UpdatedAt - - }); + }) + .OrderBy(energyProvider => energyProvider.Id); return result; } diff --git a/CH.CQRS/Query/EnergyRate/EnergyRateQueriesModule.cs b/CH.CQRS/Query/EnergyRate/EnergyRateQueriesModule.cs index bb61dec..9bee3b9 100644 --- a/CH.CQRS/Query/EnergyRate/EnergyRateQueriesModule.cs +++ b/CH.CQRS/Query/EnergyRate/EnergyRateQueriesModule.cs @@ -11,7 +11,8 @@ public class EnergyRateQueriesModule : IModule { services.AddDynamicQueryWithParams() .AddQueryableProviderOverride(); - + services.AddDynamicQuery() + .AddQueryableProviderOverride(); return services; } } \ No newline at end of file diff --git a/CH.CQRS/Query/EnergyRate/EnergyRateQuery.cs b/CH.CQRS/Query/EnergyRate/EnergyRateQuery.cs index ff8c5fe..7c876c7 100644 --- a/CH.CQRS/Query/EnergyRate/EnergyRateQuery.cs +++ b/CH.CQRS/Query/EnergyRate/EnergyRateQuery.cs @@ -1,5 +1,6 @@ using CH.CQRS.Query.EnergyProvider; using CH.Dal; +using CH.Enum; using Microsoft.EntityFrameworkCore; using OpenHarbor.CQRS.Abstractions; using OpenHarbor.CQRS.DynamicQuery.Abstractions; @@ -17,7 +18,8 @@ public class EnergyRateQueryItem public required string Name { get; set; } public decimal? Rate { get; set; } public bool Active { get; set; } - public DateTime? DiabledAt { get; set; } + public Currency Currency { get; set; } + public DateTime? DisabledAt { get; set; } public DateTime CreatedAt { get; set; } public DateTime? UpdatedAt { get; set; } } @@ -54,10 +56,12 @@ public class EnergyRateQueryableProvider(CHDbContext dbContext) : IQueryableProv Name = energyRate.Name, Rate = energyRate.Rate, Active = energyRate.Active, - DiabledAt = energyRate.DisabledAt, + Currency = energyRate.Currency, + DisabledAt = energyRate.DisabledAt, CreatedAt = energyRate.CreatedAt, UpdatedAt = energyRate.UpdatedAt - }); + }) + .OrderBy(energyRate => energyRate.Id); return result; } diff --git a/CH.CQRS/Query/EnergyRate/EnergyRateQueryAll.cs b/CH.CQRS/Query/EnergyRate/EnergyRateQueryAll.cs new file mode 100644 index 0000000..e841c3c --- /dev/null +++ b/CH.CQRS/Query/EnergyRate/EnergyRateQueryAll.cs @@ -0,0 +1,47 @@ +using CH.Dal; +using CH.Enum; +using Microsoft.EntityFrameworkCore; + +namespace CH.CQRS.Query.EnergyRate; + +public class EnergyRateQueryAll +{ + +} +public class EnergyRateQueryAllItem +{ + 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 EnergyRateAllQueryableProvider(CHDbContext dbContext) : IQueryableProviderOverride +{ + public async Task> GetQueryableAsync(object query, CancellationToken cancellationToken = default) + { + var queryable = dbContext.EnergyRates + .AsNoTracking() + .AsQueryable(); + + var result = queryable + .Select(energyRate => new EnergyRateQueryAllItem + { + 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.Name); + return result; + } +} \ No newline at end of file diff --git a/CH.CQRS/Query/EnergyRateException/EnergyRateExceptionQuery.cs b/CH.CQRS/Query/EnergyRateException/EnergyRateExceptionQuery.cs index 258c5ad..f5d1558 100644 --- a/CH.CQRS/Query/EnergyRateException/EnergyRateExceptionQuery.cs +++ b/CH.CQRS/Query/EnergyRateException/EnergyRateExceptionQuery.cs @@ -60,13 +60,15 @@ public class EnergyRateExceptionQueryableProvider(CHDbContext dbContext) : IQuer { 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; } } \ No newline at end of file