add query all for energy rate, change name of queryitem

This commit is contained in:
DavidGudEnough 2025-01-30 15:56:40 -05:00
parent e56d2579d5
commit 27c06ce445
Signed by: david.nguyen
GPG Key ID: 0B95DC36355BEB37
7 changed files with 63 additions and 17 deletions

View File

@ -41,7 +41,6 @@ builder.Services.AddDefaultCommandDiscovery();
builder.Services.AddDefaultQueryDiscovery(); builder.Services.AddDefaultQueryDiscovery();
builder.Services.AddFluentValidation(); builder.Services.AddFluentValidation();
builder.Services.AddModule<AppModule>(); builder.Services.AddModule<AppModule>();
builder.Services.AddHttpClient();
builder.Services.AddDefaultCommandDiscovery(); builder.Services.AddDefaultCommandDiscovery();
builder.Services.AddDefaultQueryDiscovery(); builder.Services.AddDefaultQueryDiscovery();
if (builder.Configuration.GetValue<bool>("Swagger:Enable")) if (builder.Configuration.GetValue<bool>("Swagger:Enable"))

View File

@ -40,14 +40,7 @@ public class UpdateEnergyRateCommandValidator : AbstractValidator<UpdateEnergyRa
{ {
RuleFor(command => command.Name) RuleFor(command => command.Name)
.NotEmpty() .NotEmpty()
.MinimumLength(3) .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.");
RuleFor(command => command.RateId) RuleFor(command => command.RateId)
.NotEmpty() .NotEmpty()

View File

@ -25,7 +25,7 @@ public class EnergyProviderQueryItem
public DateTime? UpdatedAt { get; set; } public DateTime? UpdatedAt { get; set; }
} }
public class EnergyRateQueryParams public class EnergyProviderQueryParams
{ {
} }
@ -46,8 +46,8 @@ public class EnergyProviderQueryableProvider(CHDbContext dbContext) : IQueryable
DisabledAt = energyProvider.DisabledAt, DisabledAt = energyProvider.DisabledAt,
CreatedAt = energyProvider.CreatedAt, CreatedAt = energyProvider.CreatedAt,
UpdatedAt = energyProvider.UpdatedAt UpdatedAt = energyProvider.UpdatedAt
})
}); .OrderBy(energyProvider => energyProvider.Id);
return result; return result;
} }

View File

@ -11,7 +11,8 @@ public class EnergyRateQueriesModule : IModule
{ {
services.AddDynamicQueryWithParams<EnergyRateQueryItem, EnergyRateQueryParams>() services.AddDynamicQueryWithParams<EnergyRateQueryItem, EnergyRateQueryParams>()
.AddQueryableProviderOverride<EnergyRateQueryItem, EnergyRateQueryableProvider>(); .AddQueryableProviderOverride<EnergyRateQueryItem, EnergyRateQueryableProvider>();
services.AddDynamicQuery<EnergyRateQueryAllItem>()
.AddQueryableProviderOverride<EnergyRateQueryAllItem, EnergyRateAllQueryableProvider>();
return services; return services;
} }
} }

View File

@ -1,5 +1,6 @@
using CH.CQRS.Query.EnergyProvider; 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; using OpenHarbor.CQRS.DynamicQuery.Abstractions;
@ -17,7 +18,8 @@ public class EnergyRateQueryItem
public required string Name { get; set; } public required string Name { get; set; }
public decimal? Rate { get; set; } public decimal? Rate { get; set; }
public bool Active { 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 CreatedAt { get; set; }
public DateTime? UpdatedAt { get; set; } public DateTime? UpdatedAt { get; set; }
} }
@ -54,10 +56,12 @@ public class EnergyRateQueryableProvider(CHDbContext dbContext) : IQueryableProv
Name = energyRate.Name, Name = energyRate.Name,
Rate = energyRate.Rate, Rate = energyRate.Rate,
Active = energyRate.Active, Active = energyRate.Active,
DiabledAt = energyRate.DisabledAt, Currency = energyRate.Currency,
DisabledAt = energyRate.DisabledAt,
CreatedAt = energyRate.CreatedAt, CreatedAt = energyRate.CreatedAt,
UpdatedAt = energyRate.UpdatedAt UpdatedAt = energyRate.UpdatedAt
}); })
.OrderBy(energyRate => energyRate.Id);
return result; return result;
} }

View File

@ -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<EnergyRateQueryAllItem>
{
public async Task<IQueryable<EnergyRateQueryAllItem>> 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;
}
}

View File

@ -60,13 +60,15 @@ public class EnergyRateExceptionQueryableProvider(CHDbContext dbContext) : IQuer
{ {
Id = energyRateException.Id, Id = energyRateException.Id,
Name = energyRateException.Name, Name = energyRateException.Name,
RateId = energyRateException.RateId,
EnergyThreshold = energyRateException.EnergyThreshold, EnergyThreshold = energyRateException.EnergyThreshold,
ResetType = energyRateException.ResetType, ResetType = energyRateException.ResetType,
StartedAt = energyRateException.StartedAt, StartedAt = energyRateException.StartedAt,
EndedAt = energyRateException.EndedAt, EndedAt = energyRateException.EndedAt,
CreatedAt = energyRateException.CreatedAt, CreatedAt = energyRateException.CreatedAt,
UpdatedAt = energyRateException.UpdatedAt, UpdatedAt = energyRateException.UpdatedAt,
}); })
.OrderBy(energyRateException => energyRateException.Id);
return result; return result;
} }
} }