summaryrefslogtreecommitdiff
path: root/RhSolutions.QueryModifiers/DrinkingWaterHeatingPipes/DrinkingWaterHeatingPipe.cs
blob: d5981f01755b08ed2f8ca0fa57e84b3e0eb9bc84 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;

namespace RhSolutions.QueryModifiers.DrinkingWaterHeatingPipes;

public class DrinkingWaterHeatingPipe : IProductQueryModifier
{
    protected static readonly Regex _diameter =
        new(@"([\b\D]|^)?(?<Diameter>16|20|25|32|40|50|63)([\b\D]|$)");
    protected static readonly Regex _type = 
        new(@"([\b\W])(?<Type>бухт|отр|штанг)([\b\w\.\s])");
    protected virtual string _title { get; } = string.Empty;

    protected virtual Dictionary<int, 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> _makeUp { get; } = new()
    {
        ["бухт"] = "бухта",
        ["штанг"] = "прям.отрезки",
        ["отр"] = "прям.отрезки"
    };

    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 diameterMatch = _diameter.Match(query);
        if (!diameterMatch.Success)
        {
            return null;
        }
        var diameter = int.Parse(diameterMatch.Groups["Diameter"].Value);
        var typeMatch = _type.Match(query);
        if (typeMatch.Success)
        {
            var type = typeMatch.Groups["Type"].Value;
            return $"Труба {_title} {_diameterNames[diameter]} {_makeUp[type]}";
        }
        else if (diameter < 32)
        {
            return $"Труба {_title} {_diameterNames[diameter]} {_makeUp["бухт"]}";
        }
        else
        {
            return $"Труба {_title} {_diameterNames[diameter]} {_makeUp["отр"]}";
        }
    }
}