From f7a0febddb5aaf98d27d5f5679e302437bf7db77 Mon Sep 17 00:00:00 2001 From: Serghei Cebotari Date: Wed, 11 Oct 2023 22:35:19 +0300 Subject: [PATCH] Create Heating Fitting Base --- .../Heating/HeatingFittingBase.cs | 48 +++++++++++++++++++ .../Heating/SleeveQueryModifier.cs | 5 ++ .../SleeveQueryModifier.cs | 38 --------------- 3 files changed, 53 insertions(+), 38 deletions(-) create mode 100644 RhSolutions.QueryModifiers/Heating/HeatingFittingBase.cs create mode 100644 RhSolutions.QueryModifiers/Heating/SleeveQueryModifier.cs delete mode 100644 RhSolutions.QueryModifiers/SleeveQueryModifier.cs diff --git a/RhSolutions.QueryModifiers/Heating/HeatingFittingBase.cs b/RhSolutions.QueryModifiers/Heating/HeatingFittingBase.cs new file mode 100644 index 0000000..672bd6b --- /dev/null +++ b/RhSolutions.QueryModifiers/Heating/HeatingFittingBase.cs @@ -0,0 +1,48 @@ +using System.Text.RegularExpressions; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Extensions; + +namespace RhSolutions.QueryModifiers.Heating; + +public abstract class HeatingFittingBase : IProductQueryModifier +{ + protected static readonly Regex _diameter = + new(@"([\b\D]|^)(?16|20|25|32|40|50|63)([\b\D]|$)"); + protected static readonly Regex _angle = + new(@"([\b\D]|^)(?45|90)([\b\D]|$)"); + protected static readonly Regex _thread = + new(@"(\D|^)(?1\s+1/4|1\s+1/2|1/2|3/4|2|1)(\D|$)"); + + protected virtual string _title { get; } = string.Empty; + + public bool TryQueryModify(IQueryCollection collection, out QueryString queryString) + { + 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; + } + + protected virtual string? BuildRhSolutionsName(string query) + { + var match = _diameter.Match(query); + if (match.Success) + { + return $"{_title} {match.Groups["Diameter"]}"; + } + return null; + } +} \ No newline at end of file diff --git a/RhSolutions.QueryModifiers/Heating/SleeveQueryModifier.cs b/RhSolutions.QueryModifiers/Heating/SleeveQueryModifier.cs new file mode 100644 index 0000000..42fe898 --- /dev/null +++ b/RhSolutions.QueryModifiers/Heating/SleeveQueryModifier.cs @@ -0,0 +1,5 @@ +namespace RhSolutions.QueryModifiers.Heating; +public class SleeveQueryModifier : HeatingFittingBase +{ + protected override string _title => "Монтажная гильза"; +} diff --git a/RhSolutions.QueryModifiers/SleeveQueryModifier.cs b/RhSolutions.QueryModifiers/SleeveQueryModifier.cs deleted file mode 100644 index 788a3a2..0000000 --- a/RhSolutions.QueryModifiers/SleeveQueryModifier.cs +++ /dev/null @@ -1,38 +0,0 @@ -using System.Text.RegularExpressions; -using System.Text; -using Microsoft.AspNetCore.Http.Extensions; -using Microsoft.AspNetCore.Http; - -namespace RhSolutions.QueryModifiers; - -public class SleeveQueryModifier : IProductQueryModifier -{ - private readonly string pattern = @"\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); - StringBuilder sb = new(); - sb.Append("Монтажная гильза "); - if (matches.Count > 0) - { - sb.Append(matches.First()); - } - else - { - return false; - } - QueryBuilder qb = new() - { - {"query", sb.ToString() } - }; - queryString = qb.ToQueryString(); - return true; - } -}