• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

loresoft / MediatR.CommandQuery / 13903062696

17 Mar 2025 03:15PM UTC coverage: 59.879% (+0.8%) from 59.046%
13903062696

push

github

pwelter34
improve filter builder and data service

497 of 969 branches covered (51.29%)

Branch coverage included in aggregate %.

17 of 19 new or added lines in 2 files covered. (89.47%)

1476 of 2326 relevant lines covered (63.46%)

22.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.38
/src/MediatR.CommandQuery/Queries/EntityFilterBuilder.cs
1
using MediatR.CommandQuery.Definitions;
2

3
namespace MediatR.CommandQuery.Queries;
4

5
public static class EntityFilterBuilder
6
{
7
    public static EntityQuery? CreateSearchQuery<TModel>(string searchText, int page = 1, int pageSize = 20)
8
        where TModel : class, ISupportSearch
9
    {
10
        var filter = CreateSearchFilter<TModel>(searchText);
2✔
11
        var sort = CreateSort<TModel>();
2✔
12

13
        return new EntityQuery(filter, sort, page, pageSize);
2✔
14
    }
15

16
    public static EntitySelect? CreateSearchSelect<TModel>(string searchText)
17
        where TModel : class, ISupportSearch
18
    {
19
        var filter = CreateSearchFilter<TModel>(searchText);
2✔
20
        var sort = CreateSort<TModel>();
2✔
21

22
        return new EntitySelect(filter, sort);
2✔
23
    }
24

25
    public static EntityFilter? CreateSearchFilter<TModel>(string searchText)
26
        where TModel : class, ISupportSearch
27
    {
28
        return CreateSearchFilter(TModel.SearchFields(), searchText);
6✔
29
    }
30

31

32
    public static EntitySort? CreateSort<TModel>()
33
        where TModel : class, ISupportSearch
34
    {
35
        return new EntitySort { Name = TModel.SortField() };
6✔
36
    }
37

38
    public static EntitySort CreateSort(string field, string? direction = null)
NEW
39
        => new() { Name = field, Direction = direction };
×
40

41

42
    public static EntityFilter? CreateSearchFilter(IEnumerable<string> fields, string searchText)
43
    {
44
        if (fields is null || string.IsNullOrWhiteSpace(searchText))
10!
45
            return null;
×
46

47
        var groupFilter = new EntityFilter
10✔
48
        {
10✔
49
            Logic = EntityFilterLogic.Or,
10✔
50
            Filters = [],
10✔
51
        };
10✔
52

53
        foreach (var field in fields)
60✔
54
        {
55
            var filter = new EntityFilter
20✔
56
            {
20✔
57
                Name = field,
20✔
58
                Value = searchText,
20✔
59
                Operator = EntityFilterOperators.Contains,
20✔
60
            };
20✔
61
            groupFilter.Filters.Add(filter);
20✔
62
        }
63

64
        return groupFilter;
10✔
65
    }
66

67
    public static EntityFilter CreateFilter(string field, object? value, string? @operator = null)
NEW
68
        => new() { Name = field, Value = value, Operator = @operator };
×
69

70

71
    public static EntityFilter? CreateGroup(params IEnumerable<EntityFilter?> filters)
72
        => CreateGroup(EntityFilterLogic.And, filters);
6✔
73

74
    public static EntityFilter? CreateGroup(string logic, params IEnumerable<EntityFilter?> filters)
75
    {
76
        // check for any valid filters
77
        if (!filters.Any(f => f?.IsValid() == true))
24✔
78
            return null;
2✔
79

80
        var groupFilters = filters
8✔
81
            .Where(f => f?.IsValid() == true)
16✔
82
            .Select(f => f!)
12✔
83
            .ToList();
8✔
84

85
        // no need for group if only one filter
86
        if (groupFilters.Count == 1)
8✔
87
            return groupFilters[0];
4✔
88

89
        return new EntityFilter
4✔
90
        {
4✔
91
            Logic = logic,
4✔
92
            Filters = groupFilters,
4✔
93
        };
4✔
94
    }
95
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc