0
0
RhSolutions-Api/RhSolutions.Api/Services/FlexPipeQueryModifier.cs

77 lines
1.8 KiB
C#
Raw Normal View History

2023-09-29 11:20:05 +03:00
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Http.Extensions;
namespace RhSolutions.Api.Services
{
2023-09-29 16:25:28 +03:00
public class FlexPipeQueryModifier : IProductQueryModifier
2023-09-29 11:20:05 +03:00
{
2023-10-03 21:58:06 +03:00
protected virtual string diameterPattern { get; } = @"\b(16|20|25|32|40|50|63)\b";
protected virtual string typePattern { get; } = @"бухт|отр|штанг";
2023-09-29 11:20:05 +03:00
protected virtual string pipeName { get; } = "Flex";
protected virtual Dictionary<string, 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"
};
public bool TryQueryModify(IQueryCollection collection, out QueryString queryString)
{
queryString = QueryString.Empty;
StringBuilder sb = new();
string query = collection["query"].ToString();
if (string.IsNullOrEmpty(query))
{
return false;
}
sb.Append($"Труба {pipeName} ");
var diameterMatches = Regex.Matches(query, diameterPattern);
string diameter;
if (diameterMatches.Count > 0)
{
diameter = diameterMatches.First().Value;
sb.Append($"{diameterNames[diameter]} " );
}
else
{
return false;
}
var typeMatches = Regex.Matches(query, typePattern);
if (typeMatches.Count > 0)
{
var type = typeMatches.First().Value;
2023-10-03 21:58:06 +03:00
if (type.StartsWith("отр") || type.StartsWith("штанг"))
2023-09-29 11:20:05 +03:00
{
2023-10-03 21:58:06 +03:00
sb.Append("прям.отрезки");
2023-09-29 11:20:05 +03:00
}
else
{
2023-10-03 21:58:06 +03:00
sb.Append("бухта");
2023-09-29 11:20:05 +03:00
}
}
else if (int.Parse(diameter) < 32)
{
sb.Append("бухта");
}
else
{
sb.Append("прям.отрезки");
}
QueryBuilder qb = new()
{
{ "query", sb.ToString() }
};
queryString = qb.ToQueryString();
return true;
}
}
}