0
0
RhSolutions-Api/RhSolutions.QueryModifiers/AdapterModifier.cs

40 lines
1.0 KiB
C#

using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
namespace RhSolutions.QueryModifiers
{
public abstract class AdapterModifier : IProductQueryModifier
{
protected string pattern { get; } =
@"(?<Diameter>\b16|20|25|32|40|50|63\b)\D+(?<Thread>\b1\s+1/4|1\s+1/2|1/2|3/4|2|1\b)";
protected virtual string name { get; } = string.Empty;
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 == 0)
{
return false;
}
else
{
var match = matches.First();
var diameter = match.Groups["Diameter"].Captures.First();
var thread = match.Groups["Thread"].Captures.First();
QueryBuilder qb = new()
{
{"query", $"{name} {diameter} {thread}"}
};
queryString = qb.ToQueryString();
return true;
}
}
}
}