From 38e5eb039372b28af23b921e8496cd231bb7d36a Mon Sep 17 00:00:00 2001 From: Serghei Cebotari Date: Thu, 21 Sep 2023 08:20:13 +0300 Subject: [PATCH] Add Sleeve Query modifier --- .../Services/ProductQueryModifierFactory.cs | 2 + .../Services/SleeveQueryModifier.cs | 38 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 RhSolutions.Api/Services/SleeveQueryModifier.cs diff --git a/RhSolutions.Api/Services/ProductQueryModifierFactory.cs b/RhSolutions.Api/Services/ProductQueryModifierFactory.cs index 8f587a8..a62a0c3 100644 --- a/RhSolutions.Api/Services/ProductQueryModifierFactory.cs +++ b/RhSolutions.Api/Services/ProductQueryModifierFactory.cs @@ -6,6 +6,8 @@ public class ProductQueryModifierFactory { switch (productTypeName) { + case "Монтажная гильза": + return new SleeveQueryModifier(); case "Тройник RAUTITAN": return new TPieceQueryModifier(); default: diff --git a/RhSolutions.Api/Services/SleeveQueryModifier.cs b/RhSolutions.Api/Services/SleeveQueryModifier.cs new file mode 100644 index 0000000..2fc0e33 --- /dev/null +++ b/RhSolutions.Api/Services/SleeveQueryModifier.cs @@ -0,0 +1,38 @@ +using System.Text.RegularExpressions; +using System.Text; +using Microsoft.AspNetCore.Http.Extensions; + +namespace RhSolutions.Api.Services +{ + public class SleeveQueryModifier : IProductQueryModifier + { + private readonly string pattern = @"(\b16|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; + } + } +}