Edit TryQueryNodify types
This commit is contained in:
parent
931b08e00d
commit
020922d749
@ -1,6 +1,3 @@
|
||||
using System.Web;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
public abstract class ProductQueryModifierTests
|
||||
{
|
||||
protected ProductQueryModifierFactory _factory;
|
||||
@ -10,17 +7,10 @@ public abstract class ProductQueryModifierTests
|
||||
{
|
||||
_factory = new ProductQueryModifierFactory();
|
||||
}
|
||||
public void Execute(string productType, string query, string modified)
|
||||
public void Execute(string productType, string query, string expected)
|
||||
{
|
||||
Dictionary<string, StringValues> queryPair = new()
|
||||
{
|
||||
["query"] = new StringValues(query)
|
||||
};
|
||||
QueryCollection collection = new(queryPair);
|
||||
var modifier = _factory.GetModifier(productType);
|
||||
|
||||
Assert.True(modifier.TryQueryModify(collection, out var actual));
|
||||
string? result = HttpUtility.ParseQueryString(actual.ToString())["query"];
|
||||
Assert.That(result, Is.EqualTo(modified));
|
||||
var modifier = _factory.GetModifier(productType);
|
||||
Assert.True(modifier.TryQueryModify(query, out var actual));
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
}
|
@ -1,30 +1,35 @@
|
||||
using RhSolutions.Api.Services;
|
||||
using Microsoft.AspNetCore.Http.Extensions;
|
||||
using RhSolutions.Api.Services;
|
||||
using RhSolutions.QueryModifiers;
|
||||
|
||||
namespace RhSolutions.Api.Middleware;
|
||||
|
||||
public class QueryModifier
|
||||
{
|
||||
private RequestDelegate _next;
|
||||
private RequestDelegate _next;
|
||||
|
||||
public QueryModifier(RequestDelegate nextDelegate)
|
||||
{
|
||||
_next = nextDelegate;
|
||||
}
|
||||
public QueryModifier(RequestDelegate nextDelegate)
|
||||
{
|
||||
_next = nextDelegate;
|
||||
}
|
||||
|
||||
public async Task Invoke(HttpContext context, IProductTypePredicter typePredicter, ProductQueryModifierFactory productQueryModifierFactory)
|
||||
{
|
||||
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(context.Request.Query, out var newQuery))
|
||||
{
|
||||
context.Request.QueryString = newQuery;
|
||||
}
|
||||
}
|
||||
await _next(context);
|
||||
}
|
||||
public async Task Invoke(HttpContext context, IProductTypePredicter typePredicter, ProductQueryModifierFactory productQueryModifierFactory)
|
||||
{
|
||||
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))
|
||||
{
|
||||
QueryBuilder qb = new()
|
||||
{
|
||||
{"query", modified}
|
||||
};
|
||||
context.Request.QueryString = qb.ToQueryString();
|
||||
}
|
||||
}
|
||||
await _next(context);
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +1,10 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace RhSolutions.QueryModifiers;
|
||||
namespace RhSolutions.QueryModifiers;
|
||||
|
||||
public sealed class BypassQueryModifier : IProductQueryModifier
|
||||
{
|
||||
public bool TryQueryModify(IQueryCollection collection, out QueryString queryString)
|
||||
{
|
||||
queryString = QueryString.Empty;
|
||||
return false;
|
||||
}
|
||||
public bool TryQueryModify(string query, out string queryModified)
|
||||
{
|
||||
queryModified = string.Empty;
|
||||
return false;
|
||||
}
|
||||
}
|
@ -1,6 +1,4 @@
|
||||
using System.Text.RegularExpressions;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Http.Extensions;
|
||||
|
||||
namespace RhSolutions.QueryModifiers.DrinkingWaterHeatingFittings;
|
||||
|
||||
@ -15,25 +13,10 @@ public abstract class DrinkingWaterHeatingFitting : IProductQueryModifier
|
||||
|
||||
protected virtual string _title { get; } = string.Empty;
|
||||
|
||||
public bool TryQueryModify(IQueryCollection collection, out QueryString queryString)
|
||||
public bool TryQueryModify(string input, out string output)
|
||||
{
|
||||
queryString = QueryString.Empty;
|
||||
string query = collection["query"].ToString();
|
||||
if (string.IsNullOrEmpty(query))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
string? result = BuildRhSolutionsName(query);
|
||||
if (result != null)
|
||||
{
|
||||
QueryBuilder qb = new()
|
||||
{
|
||||
{ "query", result }
|
||||
};
|
||||
queryString = qb.ToQueryString();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
output = BuildRhSolutionsName(input) ?? string.Empty;
|
||||
return !string.IsNullOrEmpty(output);
|
||||
}
|
||||
|
||||
protected virtual string? BuildRhSolutionsName(string query)
|
||||
|
@ -30,26 +30,11 @@ public class DrinkingWaterHeatingPipe : IProductQueryModifier
|
||||
["отр"] = "прям.отрезки"
|
||||
};
|
||||
|
||||
public bool TryQueryModify(IQueryCollection collection, out QueryString queryString)
|
||||
{
|
||||
queryString = QueryString.Empty;
|
||||
string query = collection["query"].ToString();
|
||||
if (string.IsNullOrEmpty(query))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
string? result = BuildRhSolutionsName(query);
|
||||
if (result != null)
|
||||
{
|
||||
QueryBuilder qb = new()
|
||||
{
|
||||
{ "query", result }
|
||||
};
|
||||
queryString = qb.ToQueryString();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public bool TryQueryModify(string input, out string output)
|
||||
{
|
||||
output = BuildRhSolutionsName(input) ?? string.Empty;
|
||||
return !string.IsNullOrEmpty(output);
|
||||
}
|
||||
|
||||
protected virtual string? BuildRhSolutionsName(string query)
|
||||
{
|
||||
|
@ -4,5 +4,5 @@ namespace RhSolutions.QueryModifiers;
|
||||
|
||||
public interface IProductQueryModifier
|
||||
{
|
||||
public bool TryQueryModify(IQueryCollection collection, out QueryString queryString);
|
||||
public bool TryQueryModify(string query, out string queryModified);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user