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

31 lines
996 B
C#

using Microsoft.AspNetCore.Http.Extensions;
using RhSolutions.Api.Services;
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(context.Request.Query, out var newQuery))
{
context.Request.QueryString = newQuery;
}
}
await _next(context);
}
}