summaryrefslogtreecommitdiff
path: root/RhSolutions.QueryModifiers/PipeQueryModifier.cs
diff options
context:
space:
mode:
authorSerghei Cebotari <serghei@cebotari.ru>2023-10-10 22:26:16 +0300
committerSerghei Cebotari <serghei@cebotari.ru>2023-10-10 22:26:16 +0300
commit6b9c0dfffc3ecfcfb642f696250aee8e4ed6031b (patch)
tree2d98f14ab3c194f6e5620fa375c4fd805c6b957e /RhSolutions.QueryModifiers/PipeQueryModifier.cs
parent942c60beeac8b89643dc66235db62d4c8f739a0a (diff)
Move Modifiers to own project
Diffstat (limited to 'RhSolutions.QueryModifiers/PipeQueryModifier.cs')
-rw-r--r--RhSolutions.QueryModifiers/PipeQueryModifier.cs59
1 files changed, 59 insertions, 0 deletions
diff --git a/RhSolutions.QueryModifiers/PipeQueryModifier.cs b/RhSolutions.QueryModifiers/PipeQueryModifier.cs
new file mode 100644
index 0000000..2126c55
--- /dev/null
+++ b/RhSolutions.QueryModifiers/PipeQueryModifier.cs
@@ -0,0 +1,59 @@
+using System.Text.RegularExpressions;
+using Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Http.Extensions;
+
+namespace RhSolutions.QueryModifiers;
+
+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;
+ }
+}