0
0
RhSolutions-Api/RhSolutions.Api/Middleware/QueryModifier.cs

36 lines
961 B
C#
Raw Normal View History

2023-10-20 22:35:44 +03:00
using Microsoft.AspNetCore.Http.Extensions;
using RhSolutions.Api.Services;
2023-10-10 22:26:16 +03:00
using RhSolutions.QueryModifiers;
2023-09-19 14:56:55 +03:00
namespace RhSolutions.Api.Middleware;
public class QueryModifier
{
2023-10-20 22:35:44 +03:00
private RequestDelegate _next;
2023-09-19 14:56:55 +03:00
2023-10-20 22:35:44 +03:00
public QueryModifier(RequestDelegate nextDelegate)
{
_next = nextDelegate;
}
2023-09-19 14:56:55 +03:00
2023-10-20 22:35:44 +03:00
public async Task Invoke(HttpContext context, IProductTypePredicter typePredicter, ProductQueryModifierFactory productQueryModifierFactory)
{
if (context.Request.Method == HttpMethods.Get
&& context.Request.Path == "/api/search")
{
string query = context.Request.Query["query"].ToString();
var productType = typePredicter.GetPredictedProductType(query);
var modifier = productQueryModifierFactory.GetModifier(productType!);
if (modifier.TryQueryModify(query, out var modified))
{
QueryBuilder qb = new()
{
{"query", modified}
};
context.Request.QueryString = qb.ToQueryString();
}
}
await _next(context);
}
2023-09-19 14:56:55 +03:00
}