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
|
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()
|
|
||||||
{
|
|
||||||
["query"] = new StringValues(query)
|
|
||||||
};
|
|
||||||
QueryCollection collection = new(queryPair);
|
|
||||||
var modifier = _factory.GetModifier(productType);
|
var modifier = _factory.GetModifier(productType);
|
||||||
|
Assert.True(modifier.TryQueryModify(query, out var actual));
|
||||||
Assert.True(modifier.TryQueryModify(collection, out var actual));
|
Assert.That(actual, Is.EqualTo(expected));
|
||||||
string? result = HttpUtility.ParseQueryString(actual.ToString())["query"];
|
|
||||||
Assert.That(result, Is.EqualTo(modified));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,4 +1,5 @@
|
|||||||
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;
|
||||||
@ -20,9 +21,13 @@ public class QueryModifier
|
|||||||
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}
|
||||||
|
};
|
||||||
|
context.Request.QueryString = qb.ToQueryString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
await _next(context);
|
await _next(context);
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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)
|
||||||
|
@ -30,25 +30,10 @@ 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)
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user