Add coupling query modifier
This commit is contained in:
parent
aa330461c6
commit
fe96662905
@ -6,35 +6,40 @@ namespace RhSolutions.Api.Tests;
|
||||
|
||||
public class ProductQueryModifierTests
|
||||
{
|
||||
private ProductQueryModifierFactory _factory;
|
||||
private ProductQueryModifierFactory _factory;
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
_factory = new ProductQueryModifierFactory();
|
||||
}
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
_factory = new ProductQueryModifierFactory();
|
||||
}
|
||||
|
||||
[TestCase("Монтажная гильза", "Гильза 16", "Монтажная гильза 16")]
|
||||
[TestCase("Монтажная гильза", "Пресс-втулка 20", "Монтажная гильза 20")]
|
||||
public void SleeveTest(string productType, string query, string modified) =>
|
||||
Test(productType, query, modified);
|
||||
[TestCase("Монтажная гильза", "Гильза 16", "Монтажная гильза 16")]
|
||||
[TestCase("Монтажная гильза", "Пресс-втулка 20", "Монтажная гильза 20")]
|
||||
public void SleeveTest(string productType, string query, string modified) =>
|
||||
Test(productType, query, modified);
|
||||
|
||||
[TestCase("Тройник RAUTITAN", "Тройник 20-16-16", "Тройник RAUTITAN -PLATINUM 20-16-16")]
|
||||
[TestCase("Тройник RAUTITAN", "Тройник 20x16x16", "Тройник RAUTITAN -PLATINUM 20-16-16")]
|
||||
public void TPieceTest(string productType, string query, string modified) =>
|
||||
Test(productType, query, modified);
|
||||
[TestCase("Тройник RAUTITAN", "Тройник 20-16-16", "Тройник RAUTITAN -PLATINUM 20-16-16")]
|
||||
[TestCase("Тройник RAUTITAN", "Тройник 20x16x16", "Тройник RAUTITAN -PLATINUM 20-16-16")]
|
||||
public void TPieceTest(string productType, string query, string modified) =>
|
||||
Test(productType, query, modified);
|
||||
|
||||
[TestCase("Муфта соединительная", "муфта 20", "Муфта соединительная равнопроходная 20")]
|
||||
[TestCase("Муфта соединительная", "переходник 20-16", "Муфта соединительная переходная 20-16")]
|
||||
public void CouplingTest(string productType, string query, string modified) =>
|
||||
Test(productType, query, modified);
|
||||
|
||||
public void Test(string productType, string query, string modified)
|
||||
{
|
||||
Dictionary<string, StringValues> queryPair = new()
|
||||
{
|
||||
["query"] = new StringValues(query)
|
||||
};
|
||||
QueryCollection collection = new(queryPair);
|
||||
QueryString expected = new($"?query={Uri.EscapeDataString(modified)}");
|
||||
var modifier = _factory.GetModifier(productType);
|
||||
bool result = modifier.TryQueryModify(collection, out var actual);
|
||||
Assert.True(result);
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
public void Test(string productType, string query, string modified)
|
||||
{
|
||||
Dictionary<string, StringValues> queryPair = new()
|
||||
{
|
||||
["query"] = new StringValues(query)
|
||||
};
|
||||
QueryCollection collection = new(queryPair);
|
||||
QueryString expected = new($"?query={Uri.EscapeDataString(modified)}");
|
||||
var modifier = _factory.GetModifier(productType);
|
||||
bool result = modifier.TryQueryModify(collection, out var actual);
|
||||
Assert.True(result);
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
}
|
39
RhSolutions.Api/Services/CouplingModifier.cs
Normal file
39
RhSolutions.Api/Services/CouplingModifier.cs
Normal file
@ -0,0 +1,39 @@
|
||||
|
||||
using System.Text.RegularExpressions;
|
||||
using Microsoft.AspNetCore.Http.Extensions;
|
||||
|
||||
namespace RhSolutions.Api.Services
|
||||
{
|
||||
public class CouplingModifier : IProductQueryModifier
|
||||
{
|
||||
private string pattern { get; } = @"\b(16|20|25|32|40|50|63)\b";
|
||||
public bool TryQueryModify(IQueryCollection collection, out QueryString queryString)
|
||||
{
|
||||
queryString = QueryString.Empty;
|
||||
var query = collection["query"].ToString();
|
||||
if (string.IsNullOrEmpty(query))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
var matches = Regex.Matches(query, pattern);
|
||||
if (matches.Count < 1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
QueryBuilder qb = new();
|
||||
if (matches.Count < 2)
|
||||
{
|
||||
qb.Add("query", $"Муфта соединительная равнопроходная {matches[0]}");
|
||||
}
|
||||
else
|
||||
{
|
||||
qb.Add("query", $"Муфта соединительная переходная {matches[0]}-{matches[1]}");
|
||||
}
|
||||
queryString = qb.ToQueryString();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
namespace RhSolutions.Api.Services;
|
||||
namespace RhSolutions.Api.Services;
|
||||
|
||||
public class ProductQueryModifierFactory
|
||||
{
|
||||
@ -10,6 +10,8 @@ public class ProductQueryModifierFactory
|
||||
return new SleeveQueryModifier();
|
||||
case "Тройник RAUTITAN":
|
||||
return new TPieceQueryModifier();
|
||||
case "Муфта соединительная":
|
||||
return new CouplingModifier();
|
||||
case "Flex":
|
||||
return new FlexPipeQueryModifier();
|
||||
case "Stabil":
|
||||
|
Loading…
Reference in New Issue
Block a user