summaryrefslogtreecommitdiff
path: root/RhSolutions.QueryModifiers/CouplingModifier.cs
blob: e8a1739621e56d4a59e18e97ff8fa94ba7696e98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;

namespace RhSolutions.QueryModifiers;

public class CouplingModifier : IProductQueryModifier
{
	private string pattern { get; } = @"([\b\D]|^)?(?<Diameter>16|20|25|32|40|50|63)([\b\D]|$)?";
	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);
		if (matches.Count < 1)
		{
			return false;
		}
		else
		{
			QueryBuilder qb = new();
			if (matches.Count < 2 || matches.Count > 1 && matches[0].Groups["Diameter"].Value == matches[1].Groups["Diameter"].Value)
			{
				qb.Add("query", $"Муфта соединительная равнопроходная {matches[0].Groups["Diameter"].Value}");
			}
			else 
			{
				qb.Add("query", $"Муфта соединительная переходная {matches[0].Groups["Diameter"].Value}-{matches[1].Groups["Diameter"].Value}");
			}
			queryString = qb.ToQueryString();
			return true;
		}
	}
}