-
Notifications
You must be signed in to change notification settings - Fork 0
/
FiltraCoreExtensions.cs
119 lines (94 loc) · 4.93 KB
/
FiltraCoreExtensions.cs
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
namespace HBH.FiltraCore
{
public static class FiltraCoreExtensions
{
public static IQueryable<T> ApplyAdvanceFilters<T, TInput>(this IQueryable<T> qry, TInput input)
{
return qry.ApplyTerm(input)
.ApplyFilters(input);
}
/// <summary>
/// this function reads the term field from input and apply the filter on the query base on TermBy field
/// </summary>
private static IQueryable<T> ApplyTerm<T, TInput>(this IQueryable<T> qry, TInput input)
{
var term = (string)input.GetType().GetProperty("Term")?.GetValue(input);
var termBy = (string)input.GetType().GetProperty("TermBy")?.GetValue(input);
if (!string.IsNullOrWhiteSpace(term) && !string.IsNullOrWhiteSpace(termBy))
{
var termByList = termBy.Split(',').ToList();
if (termByList != null && termByList.Count > 0)
{
var colsConditions = termByList.Aggregate("", (current, p) => current + $"{(!string.IsNullOrWhiteSpace(current) ? " || " : " ")} x.{p.FirstCharToUpper()}.ToLower().Contains(\"{term}\")");
var condition = $"x => {colsConditions}";
qry = qry.Where(condition);
}
}
return qry;
}
/// <summary>
/// this function reads the Filters from input and apply the filter on the query
/// </summary>
private static IQueryable<T> ApplyFilters<T, TInput>(this IQueryable<T> qry, TInput input)
{
var filtersStr = (string)input.GetType().GetProperty("Filters")?.GetValue(input);
var filters = JsonConvert.DeserializeObject<List<string[]>>(filtersStr ?? "[]") ??
new List<string[]>();
if (filters != null && filters.Count > 0)
{
filters.ForEach(p =>
{
var FILTER_NAME = p[0];
var FILTER_TYPE = p[1];
var FILTER_VALUE =
FILTER_TYPE == PropSearchFilterType.IsNull ||
FILTER_TYPE == PropSearchFilterType.IsNotNull ? "" : p[2];
switch (FILTER_TYPE)
{
case PropSearchFilterType.Contains:
qry = qry.Where($"x=> x.{FILTER_NAME}.ToLower().Contains(\"{FILTER_VALUE.ToLower()}\")");
break;
case PropSearchFilterType.NotContains:
qry = qry.Where($"x=> !x.{FILTER_NAME}.ToLower().Contains(\"{FILTER_VALUE.ToLower()}\")");
break;
case PropSearchFilterType.StartWith:
qry = qry.Where($"x=> x.{FILTER_NAME}.ToLower().StartsWith(\"{FILTER_VALUE.ToLower()}\")");
break;
case PropSearchFilterType.EndsWith:
qry = qry.Where($"x=> x.{FILTER_NAME}.ToLower().EndsWith(\"{FILTER_VALUE.ToLower()}\")");
break;
case PropSearchFilterType.Equal:
qry = qry.Where($"x=> x.{FILTER_NAME}.ToLower() == \"{FILTER_VALUE.ToLower()}\"");
break;
case PropSearchFilterType.NotEqual:
qry = qry.Where($"x=> x.{FILTER_NAME}.ToLower() != \"{FILTER_VALUE.ToLower()}\"");
break;
case PropSearchFilterType.LessThan:
qry = qry.Where($"x=> x.{FILTER_NAME}.ToLower() < \"{FILTER_VALUE.ToLower()}\"");
break;
case PropSearchFilterType.LessThanOrEqual:
qry = qry.Where($"x=> x.{FILTER_NAME}.ToLower() <= \"{FILTER_VALUE.ToLower()}\"");
break;
case PropSearchFilterType.GreaterThan:
qry = qry.Where($"x=> x.{FILTER_NAME}.ToLower() > \"{FILTER_VALUE.ToLower()}\"");
break;
case PropSearchFilterType.GreaterThanOrEqual:
qry = qry.Where($"x=> x.{FILTER_NAME}.ToLower() >= \"{FILTER_VALUE.ToLower()}\"");
break;
case PropSearchFilterType.IsNull:
qry = qry.Where($"x=> x.{FILTER_NAME} == null || x.{FILTER_NAME}.TrimEnd().TrimStart() == \"\" ");
break;
case PropSearchFilterType.IsNotNull:
qry = qry.Where($"x=> x.{FILTER_NAME} != null");
break;
}
});
}
return qry;
}
}
}