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

36 lines
961 B
C#

using Microsoft.AspNetCore.Http.Extensions;
using RhSolutions.Api.Services;
using RhSolutions.QueryModifiers;
namespace RhSolutions.Api.Middleware;
public class QueryModifier
{
private RequestDelegate _next;
public QueryModifier(RequestDelegate nextDelegate)
{
_next = nextDelegate;
}
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);
}
}