0
0

Implement KeyedServices

This commit is contained in:
Serghei Cebotari 2023-12-30 14:43:25 +03:00
parent 72465f655f
commit fb0fcda84c
7 changed files with 57 additions and 19 deletions

View File

@ -1,4 +1,4 @@
namespace RhSolutions.QueryModifiers;
namespace RhSolutions.Api.Tests;
public sealed class BypassQueryModifier : IProductQueryModifier
{

View File

@ -1,7 +1,7 @@
using RhSolutions.QueryModifiers.DrinkingWaterHeatingFittings;
using RhSolutions.QueryModifiers.DrinkingWaterHeatingPipes;
namespace RhSolutions.QueryModifiers;
namespace RhSolutions.Api.Tests;
public class ProductQueryModifierFactory
{
@ -40,7 +40,7 @@ public class ProductQueryModifierFactory
case "Муфта соединительная":
return new Coupling();
case "Угольник RAUTITAN":
return new ElbowModifier();
return new Elbow();
case "Фиксатор поворота отопление":
return new BendFormerHeating();
case "Фиксатор поворота водоснабжение":

View File

@ -1,3 +1,5 @@
namespace RhSolutions.Api.Tests;
public abstract class ProductQueryModifierTests
{
protected ProductQueryModifierFactory _factory;
@ -10,7 +12,7 @@ public abstract class ProductQueryModifierTests
public void Execute(string productType, string query, string expected)
{
var modifier = _factory.GetModifier(productType);
Assert.True(modifier.TryQueryModify(query, out var actual));
Assert.That(modifier.TryQueryModify(query, out var actual), Is.True);
Assert.That(actual, Is.EqualTo(expected));
}
}

View File

@ -7,21 +7,25 @@ namespace RhSolutions.Api.Middleware;
public class QueryModifier
{
private RequestDelegate _next;
private IServiceProvider _provider;
private IProductQueryModifier? _modifier;
public QueryModifier(RequestDelegate nextDelegate)
public QueryModifier(RequestDelegate nextDelegate, IServiceProvider provider)
{
_next = nextDelegate;
_provider = provider;
}
public async Task Invoke(HttpContext context, IProductTypePredicter typePredicter, ProductQueryModifierFactory productQueryModifierFactory)
public async Task Invoke(HttpContext context, IProductTypePredicter typePredicter)
{
if (context.Request.Method == HttpMethods.Get
&& context.Request.Path == "/api/search")
{
string query = context.Request.Query["query"].ToString();
var productType = typePredicter.GetPredictedProductType(query);
var modifier = productQueryModifierFactory.GetModifier(productType!);
if (modifier.TryQueryModify(query, out var modified))
_modifier = _provider.GetRequiredKeyedService<IProductQueryModifier>(productType);
if (_modifier == null) return;
if (_modifier.TryQueryModify(query, out var modified))
{
QueryBuilder qb = new()
{

View File

@ -14,8 +14,8 @@ public class RhSolutionsContext : DbContext
builder.Entity<Product>()
.Property(e => e.ProductSku)
.HasConversion(v => v.ToString(), v => new ProductSku(v));
builder.Entity<Product>()
.Property(e => e.DeprecatedSkus)
.HasPostgresArrayConversion<ProductSku, string>(v => v.ToString(), v => new ProductSku(v));
// builder.Entity<Product>()
// .Property(e => e.DeprecatedSkus)
// .HasPostgresArrayConversion<ProductSku, string>(v => v.ToString(), v => new ProductSku(v));
}
}

View File

@ -3,14 +3,16 @@ using RhSolutions.Models;
using RhSolutions.Api.Services;
using RhSolutions.Api.Middleware;
using RhSolutions.QueryModifiers;
using RhSolutions.QueryModifiers.DrinkingWaterHeatingFittings;
using RhSolutions.QueryModifiers.DrinkingWaterHeatingPipes;
var builder = WebApplication.CreateBuilder(args);
string dbHost = builder.Configuration["DB_HOST"],
dbPort = builder.Configuration["DB_PORT"],
dbName = builder.Configuration["DB_DATABASE"],
dbUser = builder.Configuration["DB_USER"],
dbPassword = builder.Configuration["DB_PASSWORD"];
string dbHost = builder.Configuration["DB_HOST"] ?? "localhost",
dbPort = builder.Configuration["DB_PORT"] ?? "5000",
dbName = builder.Configuration["DB_DATABASE"] ?? "rhsolutions",
dbUser = builder.Configuration["DB_USER"] ?? "chebser",
dbPassword = builder.Configuration["DB_PASSWORD"] ?? "Rehau-987";
string connectionString = builder.Configuration["ConnectionsStrings:RhSolutionsLocal"]
?? $"Host={dbHost};Port={dbPort};Database={dbName};Username={dbUser};Password={dbPassword}";
@ -24,10 +26,40 @@ builder.Services.AddDbContext<RhSolutionsContext>(opts =>
}
});
builder.Services.AddScoped<IPricelistParser, ClosedXMLParser>()
.AddScoped<IProductTypePredicter, ProductTypePredicter>()
.AddSingleton<ProductQueryModifierFactory>();
.AddScoped<IProductTypePredicter, ProductTypePredicter>();
builder.Services.AddControllers();
builder.Services.AddKeyedScoped<IProductQueryModifier, Sleeve>("Монтажная гильза")
.AddKeyedScoped<IProductQueryModifier, SupportingClip>("Желоб")
.AddKeyedScoped<IProductQueryModifier, TPiece>("Тройник RAUTITAN")
.AddKeyedScoped<IProductQueryModifier, ThreadTPieceExternal>("Тройник RAUTITAN резьбовой наружный")
.AddKeyedScoped<IProductQueryModifier, ThreadTPieceInternal>("Тройник RAUTITAN резьбовой внутренний")
.AddKeyedScoped<IProductQueryModifier, AdapterExternal>("Переходник на наружную резьбу")
.AddKeyedScoped<IProductQueryModifier, AdapterInternal>("Переходник на внутреннюю резьбу")
.AddKeyedScoped<IProductQueryModifier, AdapterScrewcap>("Переходник с накидной гайкой")
.AddKeyedScoped<IProductQueryModifier, ThreadElbowExternal>("Угольник с наружной резьбой")
.AddKeyedScoped<IProductQueryModifier, ThreadElbowInternal>("Угольник с внутренней резьбой")
.AddKeyedScoped<IProductQueryModifier, ThreadElbowWallExternal>("Угольник настенный наружный")
.AddKeyedScoped<IProductQueryModifier, ThreadElbowWallInternal>("Угольник настенный внутренний")
.AddKeyedScoped<IProductQueryModifier, ThreadElbowDoubleWallInternal>("Проточный настенный угольник")
.AddKeyedScoped<IProductQueryModifier, ScrewcapElbow>("Угольник с накидной гайкой")
.AddKeyedScoped<IProductQueryModifier, Coupling>("Муфта соединительная")
.AddKeyedScoped<IProductQueryModifier, Elbow>("Угольник RAUTITAN")
.AddKeyedScoped<IProductQueryModifier, BendFormerHeating>("Фиксатор поворота отопление")
.AddKeyedScoped<IProductQueryModifier, BendFormerSanitary>("Фиксатор поворота водоснабжение")
.AddKeyedScoped<IProductQueryModifier, ConnectionBend>("Трубка Г-образная")
.AddKeyedScoped<IProductQueryModifier, ConnectionTee>("Трубка Т-образная")
.AddKeyedScoped<IProductQueryModifier, EuroconeFlex>("Резьбозажимное flex")
.AddKeyedScoped<IProductQueryModifier, EuroconeStabil>("Резьбозажимное stabil")
.AddKeyedScoped<IProductQueryModifier, EuroconeAdapter>("Переходник на евроконус")
.AddKeyedScoped<IProductQueryModifier, EuroconeConnectionBend>("Резьбозажимное для трубки")
.AddKeyedScoped<IProductQueryModifier, Nippel>("Ниппель")
.AddKeyedScoped<IProductQueryModifier, DummyPlug>("Заглушка труб RAUTITAN")
.AddKeyedScoped<IProductQueryModifier, FlexPipe>("Flex")
.AddKeyedScoped<IProductQueryModifier, PinkPipe>("Pink")
.AddKeyedScoped<IProductQueryModifier, StabilPipe>("Stabil")
.AddKeyedScoped<IProductQueryModifier, BlackPipe>("Black");
var app = builder.Build();
app.MapControllers();

View File

@ -1,6 +1,6 @@
namespace RhSolutions.QueryModifiers.DrinkingWaterHeatingFittings;
public class ElbowModifier : DrinkingWaterHeatingFitting
public class Elbow : DrinkingWaterHeatingFitting
{
protected override string _title { get; } = "Угольник RAUTITAN -PLATINUM";