0
0

Edit TryQueryNodify types

This commit is contained in:
Serghei Cebotari 2023-10-20 22:35:44 +03:00
parent 931b08e00d
commit 020922d749
6 changed files with 45 additions and 84 deletions

View File

@ -1,6 +1,3 @@
using System.Web;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;
public abstract class ProductQueryModifierTests public abstract class ProductQueryModifierTests
{ {
protected ProductQueryModifierFactory _factory; protected ProductQueryModifierFactory _factory;
@ -10,17 +7,10 @@ public abstract class ProductQueryModifierTests
{ {
_factory = new ProductQueryModifierFactory(); _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() var modifier = _factory.GetModifier(productType);
{ Assert.True(modifier.TryQueryModify(query, out var actual));
["query"] = new StringValues(query) Assert.That(actual, Is.EqualTo(expected));
};
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));
} }
} }

View File

@ -1,30 +1,35 @@
using RhSolutions.Api.Services; using Microsoft.AspNetCore.Http.Extensions;
using RhSolutions.Api.Services;
using RhSolutions.QueryModifiers; using RhSolutions.QueryModifiers;
namespace RhSolutions.Api.Middleware; namespace RhSolutions.Api.Middleware;
public class QueryModifier public class QueryModifier
{ {
private RequestDelegate _next; private RequestDelegate _next;
public QueryModifier(RequestDelegate nextDelegate) public QueryModifier(RequestDelegate nextDelegate)
{ {
_next = nextDelegate; _next = nextDelegate;
} }
public async Task Invoke(HttpContext context, IProductTypePredicter typePredicter, ProductQueryModifierFactory productQueryModifierFactory) public async Task Invoke(HttpContext context, IProductTypePredicter typePredicter, ProductQueryModifierFactory productQueryModifierFactory)
{ {
if (context.Request.Method == HttpMethods.Get if (context.Request.Method == HttpMethods.Get
&& context.Request.Path == "/api/search") && context.Request.Path == "/api/search")
{ {
string query = context.Request.Query["query"].ToString(); string query = context.Request.Query["query"].ToString();
var productType = typePredicter.GetPredictedProductType(query); var productType = typePredicter.GetPredictedProductType(query);
var modifier = productQueryModifierFactory.GetModifier(productType!); var modifier = productQueryModifierFactory.GetModifier(productType!);
if (modifier.TryQueryModify(context.Request.Query, out var newQuery)) if (modifier.TryQueryModify(query, out var modified))
{ {
context.Request.QueryString = newQuery; QueryBuilder qb = new()
} {
} {"query", modified}
await _next(context); };
} context.Request.QueryString = qb.ToQueryString();
}
}
await _next(context);
}
} }

View File

@ -1,12 +1,10 @@
using Microsoft.AspNetCore.Http; namespace RhSolutions.QueryModifiers;
namespace RhSolutions.QueryModifiers;
public sealed class BypassQueryModifier : IProductQueryModifier public sealed class BypassQueryModifier : IProductQueryModifier
{ {
public bool TryQueryModify(IQueryCollection collection, out QueryString queryString) public bool TryQueryModify(string query, out string queryModified)
{ {
queryString = QueryString.Empty; queryModified = string.Empty;
return false; return false;
} }
} }

View File

@ -1,6 +1,4 @@
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
namespace RhSolutions.QueryModifiers.DrinkingWaterHeatingFittings; namespace RhSolutions.QueryModifiers.DrinkingWaterHeatingFittings;
@ -15,25 +13,10 @@ public abstract class DrinkingWaterHeatingFitting : IProductQueryModifier
protected virtual string _title { get; } = string.Empty; 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; output = BuildRhSolutionsName(input) ?? string.Empty;
string query = collection["query"].ToString(); return !string.IsNullOrEmpty(output);
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;
} }
protected virtual string? BuildRhSolutionsName(string query) protected virtual string? BuildRhSolutionsName(string query)

View File

@ -30,26 +30,11 @@ public class DrinkingWaterHeatingPipe : IProductQueryModifier
["отр"] = "прям.отрезки" ["отр"] = "прям.отрезки"
}; };
public bool TryQueryModify(IQueryCollection collection, out QueryString queryString) public bool TryQueryModify(string input, out string output)
{ {
queryString = QueryString.Empty; output = BuildRhSolutionsName(input) ?? string.Empty;
string query = collection["query"].ToString(); return !string.IsNullOrEmpty(output);
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;
}
protected virtual string? BuildRhSolutionsName(string query) protected virtual string? BuildRhSolutionsName(string query)
{ {

View File

@ -4,5 +4,5 @@ namespace RhSolutions.QueryModifiers;
public interface IProductQueryModifier public interface IProductQueryModifier
{ {
public bool TryQueryModify(IQueryCollection collection, out QueryString queryString); public bool TryQueryModify(string query, out string queryModified);
} }