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
{
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));
Assert.True(modifier.TryQueryModify(query, out var actual));
Assert.That(actual, Is.EqualTo(expected));
}
}

View File

@ -1,4 +1,5 @@
using RhSolutions.Api.Services;
using Microsoft.AspNetCore.Http.Extensions;
using RhSolutions.Api.Services;
using RhSolutions.QueryModifiers;
namespace RhSolutions.Api.Middleware;
@ -20,9 +21,13 @@ public class QueryModifier
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))
if (modifier.TryQueryModify(query, out var modified))
{
context.Request.QueryString = newQuery;
QueryBuilder qb = new()
{
{"query", modified}
};
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 bool TryQueryModify(IQueryCollection collection, out QueryString queryString)
public bool TryQueryModify(string query, out string queryModified)
{
queryString = QueryString.Empty;
queryModified = string.Empty;
return false;
}
}

View File

@ -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)

View File

@ -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;
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)

View File

@ -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);
}