blob: 810c7dd82598d7a76300f2f2fb2f1787ce8d5a8d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
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);
}
}
|