From 9501c4ed8be12373499622046f92f473b2ec0892 Mon Sep 17 00:00:00 2001 From: Serghei Cebotari Date: Sun, 8 Oct 2023 16:17:59 +0300 Subject: [PATCH] Update pipe modifiers --- .../ProductQueryModifierTests.cs | 11 +-- .../RautitanFittingsTests.cs | 2 +- RhSolutions.Api.Tests/RautitanPipesTests.cs | 30 ++++++++ .../Services/BlackPipeQueryModifier.cs | 4 +- .../Services/FlexPipeQueryModifier.cs | 74 +------------------ .../Services/PinkPipeQueryModifier.cs | 14 ++++ RhSolutions.Api/Services/PipeQueryModifier.cs | 59 +++++++++++++++ .../Services/ProductQueryModifierFactory.cs | 2 + .../Services/StabilPipeQueryModifier.cs | 4 +- 9 files changed, 117 insertions(+), 83 deletions(-) create mode 100644 RhSolutions.Api.Tests/RautitanPipesTests.cs create mode 100644 RhSolutions.Api/Services/PinkPipeQueryModifier.cs create mode 100644 RhSolutions.Api/Services/PipeQueryModifier.cs diff --git a/RhSolutions.Api.Tests/ProductQueryModifierTests.cs b/RhSolutions.Api.Tests/ProductQueryModifierTests.cs index 4bc394b..3875777 100644 --- a/RhSolutions.Api.Tests/ProductQueryModifierTests.cs +++ b/RhSolutions.Api.Tests/ProductQueryModifierTests.cs @@ -1,3 +1,4 @@ +using System.Web; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Primitives; using RhSolutions.Api.Services; @@ -16,11 +17,11 @@ public abstract class ProductQueryModifierTests { ["query"] = new StringValues(query) }; - QueryCollection collection = new(queryPair); - QueryString expected = new($"?query={Uri.EscapeDataString(modified)}"); + QueryCollection collection = new(queryPair); 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)); } } \ No newline at end of file diff --git a/RhSolutions.Api.Tests/RautitanFittingsTests.cs b/RhSolutions.Api.Tests/RautitanFittingsTests.cs index 7833dc8..f00e2bd 100644 --- a/RhSolutions.Api.Tests/RautitanFittingsTests.cs +++ b/RhSolutions.Api.Tests/RautitanFittingsTests.cs @@ -45,4 +45,4 @@ public class RautitanFittingsTests : ProductQueryModifierTests [TestCase("Тройник RAUTITAN RX+ с наружной резьбой 20-20-R 3/4", "Тройник RAUTITAN с наружной резьбой 20-20-R 3/4")] public void ThreadTPieceExternalTest(string query, string modified) => Execute(productType: "Тройник RAUTITAN резьбовой наружный", query, modified); -} \ No newline at end of file +} diff --git a/RhSolutions.Api.Tests/RautitanPipesTests.cs b/RhSolutions.Api.Tests/RautitanPipesTests.cs new file mode 100644 index 0000000..5e6bbbe --- /dev/null +++ b/RhSolutions.Api.Tests/RautitanPipesTests.cs @@ -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); +} \ No newline at end of file diff --git a/RhSolutions.Api/Services/BlackPipeQueryModifier.cs b/RhSolutions.Api/Services/BlackPipeQueryModifier.cs index f630f54..14dd636 100644 --- a/RhSolutions.Api/Services/BlackPipeQueryModifier.cs +++ b/RhSolutions.Api/Services/BlackPipeQueryModifier.cs @@ -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]|^)(?16|20|25)([\b\D]|$)"; protected override string pipeName => "Black"; protected override Dictionary diameterNames => new() { diff --git a/RhSolutions.Api/Services/FlexPipeQueryModifier.cs b/RhSolutions.Api/Services/FlexPipeQueryModifier.cs index 1dda435..a96ec80 100644 --- a/RhSolutions.Api/Services/FlexPipeQueryModifier.cs +++ b/RhSolutions.Api/Services/FlexPipeQueryModifier.cs @@ -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 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 { } } diff --git a/RhSolutions.Api/Services/PinkPipeQueryModifier.cs b/RhSolutions.Api/Services/PinkPipeQueryModifier.cs new file mode 100644 index 0000000..0ccfaba --- /dev/null +++ b/RhSolutions.Api/Services/PinkPipeQueryModifier.cs @@ -0,0 +1,14 @@ + +namespace RhSolutions.Api.Services +{ + public class PinkPipeQueryModifier : PipeQueryModifier + { + protected override string pipeName => "Pink+"; + protected override Dictionary makeUpNames => new() + { + ["бухт"] = "бухта", + ["штанг"] = "прямые отрезки", + ["отр"] = "прямые отрезки" + }; + } +} diff --git a/RhSolutions.Api/Services/PipeQueryModifier.cs b/RhSolutions.Api/Services/PipeQueryModifier.cs new file mode 100644 index 0000000..88c2275 --- /dev/null +++ b/RhSolutions.Api/Services/PipeQueryModifier.cs @@ -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]|^)(?16|20|25|32|40|50|63)([\b\D]|$)"; + protected virtual string typePattern { get; } = @"бухт|отр|штанг"; + protected virtual string pipeName { get; } = "Flex"; + protected virtual Dictionary 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 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; + } + } +} diff --git a/RhSolutions.Api/Services/ProductQueryModifierFactory.cs b/RhSolutions.Api/Services/ProductQueryModifierFactory.cs index dc03838..b0f7b85 100644 --- a/RhSolutions.Api/Services/ProductQueryModifierFactory.cs +++ b/RhSolutions.Api/Services/ProductQueryModifierFactory.cs @@ -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": diff --git a/RhSolutions.Api/Services/StabilPipeQueryModifier.cs b/RhSolutions.Api/Services/StabilPipeQueryModifier.cs index 42c8bf9..d345bb6 100644 --- a/RhSolutions.Api/Services/StabilPipeQueryModifier.cs +++ b/RhSolutions.Api/Services/StabilPipeQueryModifier.cs @@ -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]|^)(?16|20|25|32|40)([\b\D]|$)"; protected override string pipeName => "Stabil -PLATINUM"; protected override Dictionary diameterNames => new() {