summaryrefslogtreecommitdiff
path: root/RhSolutions.QueryModifiers/ThreadTPieceWall.cs
blob: 8e11a917ea00cfb45412ac830d52d362e93d27cc (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;

namespace RhSolutions.QueryModifiers;

public class ThreadTPieceInternal : IProductQueryModifier
{
	private string diameterPattern = "16|20|25|32|40|50|63";
	private string threadPattern = @"(\D|^)(?<Thread>1\s+1/4|1\s+1/2|1/2|3/4|2|1)(\D|$)";	

	public bool TryQueryModify(IQueryCollection collection, out QueryString queryString)
	{
		queryString = QueryString.Empty;
		var query = collection["query"].ToString();
		if (string.IsNullOrEmpty(query))
		{
			return false;
		}
		var diameters = Regex.Matches(query, diameterPattern);
		if (diameters.Count == 0)
		{
			return false;
		}
		var thread = Regex.Match(query, threadPattern);
		if (!thread.Success)		
		{
			return false;
		}
		QueryBuilder qb = new()
		{
			{"query", ConstructName(diameters, thread)}
		};
		queryString = qb.ToQueryString();
		return true;
	}
	
	protected virtual string ConstructName(MatchCollection diameters, Match thread)	
	{
		Capture t = thread.Groups["Thread"].Captures.First();
		if (diameters.Count == 1)		
		{
			if (int.Parse(diameters[0].Value) < 25)
			{
				return $"Тройник настенный с внутренней резьбой {diameters[0]}-Rp{t}-{diameters[0]}";
			}
			else
			{
				return $"Тройник с внутр. резьбой на боков. проходе {diameters[0]}-Rp {t}-{diameters[0]}";
			}
		}
		else
		{
			if (int.Parse(diameters[0].Value) < 25)
			{
				return $"Тройник настенный с внутренней резьбой {diameters[0]}-Rp{t}-{diameters[1]}";
			}
			else
			{
				return $"Тройник с внутр. резьбой на боков. проходе {diameters[0]}-Rp {t}-{diameters[1]}";
			}
		}
	}
}