try fixup search

This commit is contained in:
honzapatCZ 2024-09-24 16:59:15 +02:00
parent f61f9e7cb1
commit 0346a448c4

View File

@ -55,34 +55,38 @@ public static class Extensions
} }
var extPar = Expression.Parameter(typeof(TType)); var extPar = Expression.Parameter(typeof(TType));
var binder = (TEntityBinder)typeof(TEntityBinder).GetConstructor(new[] { typeof(DbContext) })!.Invoke(new object?[] { null });
var idMatcher = binder.RouteValues.First(x => x.Primary).Expression;
var reducedIdMatcher = Expression.Lambda<Func<TType, bool>>(Expression.Invoke(idMatcher, extPar, Expression.Constant(search)).Reduce(), extPar);
//Console.Writeline(reducedIdMatcher);
searchers.Add(reducedIdMatcher);
// Create an expression that ORs all the searchers using (var scope = providers.CreateScope())
var agrPar = Expression.Parameter(typeof(TType));
var orExp = searchers.Aggregate((Expression)null, (current, searcher) =>
{ {
var body = Expression.Invoke(searcher, agrPar); var binder = scope.ServiceProvider.GetRequiredService<TEntityBinder>();
if (current == null) var idMatcher = binder.RouteValues.First(x => x.Primary).Expression;
var reducedIdMatcher = Expression.Lambda<Func<TType, bool>>(Expression.Invoke(idMatcher, extPar, Expression.Constant(search)).Reduce(), extPar);
//Console.Writeline(reducedIdMatcher);
searchers.Add(reducedIdMatcher);
// Create an expression that ORs all the searchers
var agrPar = Expression.Parameter(typeof(TType));
var orExp = searchers.Aggregate((Expression)null, (current, searcher) =>
{ {
return body; var body = Expression.Invoke(searcher, agrPar);
if (current == null)
{
return body;
}
return Expression.OrElse(current, body);
});
//reduce the epxression as much as possible
while (orExp.CanReduce)
{
orExp = orExp.Reduce();
} }
return Expression.OrElse(current, body);
});
//reduce the epxression as much as possible var orLambda = Expression.Lambda<Func<TType, bool>>(orExp, agrPar);
while (orExp.CanReduce) //Console.Writeline(orLambda);
{ query = query.Where(orLambda);
orExp = orExp.Reduce();
} }
var orLambda = Expression.Lambda<Func<TType, bool>>(orExp, agrPar);
//Console.Writeline(orLambda);
query = query.Where(orLambda);
} }
return await query.ApplyPaginationRes<TType, TResponseType>(providers, pag); return await query.ApplyPaginationRes<TType, TResponseType>(providers, pag);