summaryrefslogtreecommitdiff
path: root/RhSolutions.QueryModifiers/TPieceQueryModifier.cs
blob: 87c5b616abc8b3dfcbafe36431143d9a3d8580dd (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
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
using System.Text;
using System.Text.RegularExpressions;

namespace RhSolutions.QueryModifiers;

public class TPieceQueryModifier : IProductQueryModifier
{
    private readonly string pattern = @"16|20|25|32|40|50|63";

    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("Тройник RAUTITAN -PLATINUM");
        if (matches.Count == 1)
        {
            sb.Append($" {matches.First().Value}-{matches.First().Value}-{matches.First().Value}");
        }
        else if (matches.Count >= 3)
        {
            sb.Append($" {matches[0].Value}-{matches[1].Value}-{matches[2].Value}");
        }
        else
        {
            return false;
        }
        QueryBuilder qb = new()
        {
            { "query", sb.ToString() }
        };
        queryString = qb.ToQueryString();
        return true;
    }
}