0
0

Update pipe modifiers

This commit is contained in:
Serghei Cebotari 2023-10-08 16:17:59 +03:00
parent 22f058d53c
commit 9501c4ed8b
9 changed files with 117 additions and 83 deletions

View File

@ -1,3 +1,4 @@
using System.Web;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;
using RhSolutions.Api.Services;
@ -17,10 +18,10 @@ public abstract class ProductQueryModifierTests
["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));
Assert.True(modifier.TryQueryModify(collection, out var actual));
string? result = HttpUtility.ParseQueryString(actual.ToString())["query"];
Assert.That(result, Is.EqualTo(modified));
}
}

View File

@ -0,0 +1,30 @@
namespace RhSolutions.Api.Tests;
public class RautitanPipesTests : ProductQueryModifierTests
{
[TestCase("Унив.труба RAUTITAN flex 16x2,2, бухта 100м", "Труба Flex 16x2,2 бухта")]
[TestCase("Труба flex 16", "Труба Flex 16x2,2 бухта")]
[TestCase("Унив.труба RAUTITAN flex 32x4,4, прям.отрезки 6м", "Труба Flex 32x4,4 прям.отрезки")]
[TestCase("Труба flex 32", "Труба Flex 32x4,4 прям.отрезки")]
public void FlexPipeTest(string query, string modified)
=> Execute(productType: "Flex", query, modified);
[TestCase("Унив. труба RAUTITAN pink+ 16х2,2 мм, бухта 120 м", "Труба Pink+ 16x2,2 бухта")]
[TestCase("труба pink 16", "Труба Pink+ 16x2,2 бухта")]
[TestCase("Унив. труба RAUTITAN pink+ 32х4,4 мм, бухта 50 м", "Труба Pink+ 32x4,4 бухта")]
[TestCase("труба pink 32", "Труба Pink+ 32x4,4 прямые отрезки")]
public void PinkPipeTest(string query, string modified)
=> Execute(productType: "Pink", query, modified);
[TestCase("Универсальн.труба RAUTITAN stabil 16,2х2,6 мм, бухта 100 м", "Труба Stabil -PLATINUM 16,2х2,6 бухта")]
[TestCase("труба stabil 16", "Труба Stabil -PLATINUM 16,2х2,6 бухта")]
[TestCase("Универсальн.труба RAUTITAN stabil 32х4,7 мм, прям.отрезки 5м", "Труба Stabil -PLATINUM 32х4,7 прям.отрезки")]
[TestCase("труба stabil 32", "Труба Stabil -PLATINUM 32х4,7 прям.отрезки")]
public void StabilPipeTest(string query, string modified)
=> Execute(productType: "Stabil", query, modified);
[TestCase("Отоп.труба РЕХАУ BLACK 16х2,2 мм, бухта 200 м", "Труба Black 16х2,2 бухта")]
[TestCase("труба BLACK 16", "Труба Black 16х2,2 бухта")]
public void BlackPipeTest(string query, string modified)
=> Execute(productType: "Black", query, modified);
}

View File

@ -1,8 +1,8 @@
namespace RhSolutions.Api.Services
{
public class BlackPipeQueryModifier : FlexPipeQueryModifier
public class BlackPipeQueryModifier : PipeQueryModifier
{
protected override string diameterPattern => @"\b(16|20|25)\b";
protected override string diameterPattern =>@"([\b\D]|^)(?<Diameter>16|20|25)([\b\D]|$)";
protected override string pipeName => "Black";
protected override Dictionary<string, string> diameterNames => new()
{

View File

@ -1,76 +1,4 @@
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Http.Extensions;
namespace RhSolutions.Api.Services
{
public class FlexPipeQueryModifier : IProductQueryModifier
{
protected virtual string diameterPattern { get; } = @"\b(16|20|25|32|40|50|63)\b";
protected virtual string typePattern { get; } = @"бухт|отр|штанг";
protected virtual string pipeName { get; } = "Flex";
protected virtual Dictionary<string, string> diameterNames { get; } = new()
{
["16"] = "16x2,2",
["20"] = "20x2,8",
["25"] = "25x3,5",
["32"] = "32x4,4",
["40"] = "40x5,5",
["50"] = "50x6,9",
["63"] = "63x8,6"
};
public bool TryQueryModify(IQueryCollection collection, out QueryString queryString)
{
queryString = QueryString.Empty;
StringBuilder sb = new();
string query = collection["query"].ToString();
if (string.IsNullOrEmpty(query))
{
return false;
}
sb.Append($"Труба {pipeName} ");
var diameterMatches = Regex.Matches(query, diameterPattern);
string diameter;
if (diameterMatches.Count > 0)
{
diameter = diameterMatches.First().Value;
sb.Append($"{diameterNames[diameter]} " );
}
else
{
return false;
}
var typeMatches = Regex.Matches(query, typePattern);
if (typeMatches.Count > 0)
{
var type = typeMatches.First().Value;
if (type.StartsWith("отр") || type.StartsWith("штанг"))
{
sb.Append("прям.отрезки");
}
else
{
sb.Append("бухта");
}
}
else if (int.Parse(diameter) < 32)
{
sb.Append("бухта");
}
else
{
sb.Append("прям.отрезки");
}
QueryBuilder qb = new()
{
{ "query", sb.ToString() }
};
queryString = qb.ToQueryString();
return true;
}
}
public class FlexPipeQueryModifier : PipeQueryModifier { }
}

View File

@ -0,0 +1,14 @@
namespace RhSolutions.Api.Services
{
public class PinkPipeQueryModifier : PipeQueryModifier
{
protected override string pipeName => "Pink+";
protected override Dictionary<string, string> makeUpNames => new()
{
["бухт"] = "бухта",
["штанг"] = "прямые отрезки",
["отр"] = "прямые отрезки"
};
}
}

View File

@ -0,0 +1,59 @@
using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Http.Extensions;
namespace RhSolutions.Api.Services
{
public class PipeQueryModifier : IProductQueryModifier
{
protected virtual string diameterPattern { get; } = @"([\b\D]|^)(?<Diameter>16|20|25|32|40|50|63)([\b\D]|$)";
protected virtual string typePattern { get; } = @"бухт|отр|штанг";
protected virtual string pipeName { get; } = "Flex";
protected virtual Dictionary<string, string> diameterNames { get; } = new()
{
["16"] = "16x2,2",
["20"] = "20x2,8",
["25"] = "25x3,5",
["32"] = "32x4,4",
["40"] = "40x5,5",
["50"] = "50x6,9",
["63"] = "63x8,6"
};
protected virtual Dictionary<string, string> makeUpNames { get; } = new()
{
["бухт"] = "бухта",
["штанг"] = "прям.отрезки",
["отр"] = "прям.отрезки"
};
public bool TryQueryModify(IQueryCollection collection, out QueryString queryString)
{
queryString = QueryString.Empty;
string query = collection["query"].ToString();
if (string.IsNullOrEmpty(query))
{
return false;
}
var diameterMatches = Regex.Matches(query, diameterPattern);
if (diameterMatches.Count == 0)
{
return false;
}
var typeMatches = Regex.Matches(query, typePattern);
var diameter = diameterMatches.First().Groups["Diameter"].Value;
string? type = typeMatches.FirstOrDefault()?.Value;
string result =
$"Труба {pipeName} {diameterNames[diameter]} {(type != null ? makeUpNames[type] : int.Parse(diameter) < 32 ? makeUpNames["бухт"] : makeUpNames["отр"])}";
QueryBuilder qb = new()
{
{ "query", result }
};
queryString = qb.ToQueryString();
return true;
}
}
}

View File

@ -34,6 +34,8 @@ public class ProductQueryModifierFactory
return new ElbowModifier();
case "Flex":
return new FlexPipeQueryModifier();
case "Pink":
return new PinkPipeQueryModifier();
case "Stabil":
return new StabilPipeQueryModifier();
case "Black":

View File

@ -1,8 +1,8 @@
namespace RhSolutions.Api.Services
{
public class StabilPipeQueryModifier : FlexPipeQueryModifier
public class StabilPipeQueryModifier : PipeQueryModifier
{
protected override string diameterPattern => @"\b(16|20|25|32|40)\b";
protected override string diameterPattern => @"([\b\D]|^)(?<Diameter>16|20|25|32|40)([\b\D]|$)";
protected override string pipeName => "Stabil -PLATINUM";
protected override Dictionary<string, string> diameterNames => new()
{