< Summary - Code Coverage

Information
Class: Plainquire.Filter.EntityFilterExtensions
Assembly: Plainquire.Filter
File(s): /home/runner/work/plainquire/plainquire/Plainquire.Filter/Plainquire.Filter/Extensions/EntityFilterExtensions.cs
Tag: 64_13932151703
Line coverage
100%
Covered lines: 67
Uncovered lines: 0
Coverable lines: 67
Total lines: 205
Line coverage: 100%
Branch coverage
97%
Covered branches: 39
Total branches: 40
Branch coverage: 97.5%
Method coverage
100%
Covered methods: 9
Total methods: 9
Method coverage: 100%

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
Add(...)100%22100%
Add(...)100%44100%
Add(...)100%11100%
Add(...)100%1010100%
Replace(...)100%22100%
Replace(...)100%44100%
Replace(...)100%11100%
Replace(...)100%1010100%
ToQueryParams(...)87.5%88100%

File(s)

/home/runner/work/plainquire/plainquire/Plainquire.Filter/Plainquire.Filter/Extensions/EntityFilterExtensions.cs

#LineLine coverage
 1using Plainquire.Filter.Abstractions;
 2using System;
 3using System.Collections.Generic;
 4using System.Linq;
 5using System.Linq.Expressions;
 6using System.Reflection;
 7using System.Web;
 8
 9namespace Plainquire.Filter;
 10
 11/// <summary>
 12/// Extension methods for <see cref="EntityFilter{TEntity}"/>
 13/// </summary>
 14public static class EntityFilterExtensions
 15{
 16    /// <summary>
 17    /// Adds a filter for the given property. Existing filters for the same property are preserved.
 18    /// </summary>
 19    /// <typeparam name="TEntity">The type of the entity.</typeparam>
 20    /// <typeparam name="TProperty">The type of the property.</typeparam>
 21    /// <param name="entityFilter">The entity filter.</param>
 22    /// <param name="property">The property to filter.</param>
 23    /// <param name="filterSyntax">Description of the filter using micro syntax.</param>
 24    public static EntityFilter<TEntity> Add<TEntity, TProperty>(this EntityFilter<TEntity> entityFilter, Expression<Func
 25    {
 7026        if (filterSyntax == null)
 127            return entityFilter;
 28
 6929        var filters = ValueFiltersFactory.Create(filterSyntax, entityFilter.Configuration);
 6930        entityFilter.Add(property, filters);
 6831        return entityFilter;
 32    }
 33
 34    /// <summary>
 35    /// Adds a filter for the given property using the default filter operator. Existing filters for the same property a
 36    /// </summary>
 37    /// <typeparam name="TEntity">The type of the entity.</typeparam>
 38    /// <typeparam name="TProperty">The type of the property.</typeparam>
 39    /// <typeparam name="TValue">The type of the value.</typeparam>
 40    /// <param name="entityFilter">The entity filter.</param>
 41    /// <param name="property">The property to filter.</param>
 42    /// <param name="values">The values to filter for. Multiple values are combined with conditional OR.</param>
 43    public static EntityFilter<TEntity> Add<TEntity, TProperty, TValue>(this EntityFilter<TEntity> entityFilter, Express
 44    {
 2345        if (values == null || values.Length == 0)
 446            return entityFilter;
 47
 1948        var valueFilters = values
 1949            .Select(value => ValueFilter.Create(
 1950                FilterOperator.Default,
 1951                value,
 1952                entityFilter.Configuration
 1953            ))
 1954            .ToArray();
 55
 1856        return entityFilter.Add(property, valueFilters);
 57    }
 58
 59    /// <summary>
 60    /// Adds a filter for the given property. Existing filters for the same property are preserved.
 61    /// </summary>
 62    /// <typeparam name="TEntity">The type of the entity.</typeparam>
 63    /// <typeparam name="TProperty">The type of the property.</typeparam>
 64    /// <param name="entityFilter">The entity filter.</param>
 65    /// <param name="property">The property to filter.</param>
 66    /// <param name="filterOperator">The filter operator to use.</param>
 67    public static EntityFilter<TEntity> Add<TEntity, TProperty>(this EntityFilter<TEntity> entityFilter, Expression<Func
 668        => entityFilter.Add(property, ValueFilter.Create(filterOperator, entityFilter.Configuration));
 69
 70    /// <summary>
 71    /// Adds a filter for the given property. Existing filters for the same property are preserved.
 72    /// </summary>
 73    /// <typeparam name="TEntity">The type of the entity.</typeparam>
 74    /// <typeparam name="TProperty">The type of the property.</typeparam>
 75    /// <typeparam name="TValue">The type of the value.</typeparam>
 76    /// <param name="entityFilter">The entity filter.</param>
 77    /// <param name="property">The property to filter.</param>
 78    /// <param name="filterOperator">The filter operator to use.</param>
 79    /// <param name="values">The values to filter for. Multiple values are combined with conditional OR.</param>
 80    public static EntityFilter<TEntity> Add<TEntity, TProperty, TValue>(this EntityFilter<TEntity> entityFilter, Express
 81    {
 1582        var isNullableFilterOperator = filterOperator is FilterOperator.IsNull or FilterOperator.NotNull;
 1583        if ((values == null || values.Length == 0) && !isNullableFilterOperator)
 284            return entityFilter;
 85
 1386        var valueFilters = values?
 1387            .Select(value => ValueFilter.Create(
 1388                filterOperator,
 1389                value,
 1390                entityFilter.Configuration
 1391            ))
 1392            .ToArray();
 93
 1394        entityFilter.Add(property, valueFilters);
 1395        return entityFilter;
 96    }
 97
 98    /// <summary>
 99    /// Replaces the filter for the given property. Existing filters for the same property are removed.
 100    /// </summary>
 101    /// <typeparam name="TEntity">The type of the entity.</typeparam>
 102    /// <typeparam name="TProperty">The type of the property.</typeparam>
 103    /// <param name="entityFilter">The entity filter.</param>
 104    /// <param name="property">The property to filter.</param>
 105    /// <param name="filterSyntax">Description of the filter using micro syntax.</param>
 106    public static EntityFilter<TEntity> Replace<TEntity, TProperty>(this EntityFilter<TEntity> entityFilter, Expression<
 107    {
 6504108        if (filterSyntax == null)
 1109            return entityFilter.Remove(property);
 110
 6503111        var valueFilters = ValueFiltersFactory.Create(filterSyntax, entityFilter.Configuration);
 6503112        entityFilter.Replace(property, valueFilters);
 6503113        return entityFilter;
 114    }
 115
 116    /// <summary>
 117    /// Replaces the filter for the given property using the default filter operator. Existing filters for the same prop
 118    /// </summary>
 119    /// <typeparam name="TEntity">The type of the entity.</typeparam>
 120    /// <typeparam name="TProperty">The type of the t property.</typeparam>
 121    /// <typeparam name="TValue">The type of the t value.</typeparam>
 122    /// <param name="entityFilter">The entity filter.</param>
 123    /// <param name="property">The property to filter.</param>
 124    /// <param name="values">The values to filter for. Multiple values are combined with conditional OR.</param>
 125    public static EntityFilter<TEntity> Replace<TEntity, TProperty, TValue>(this EntityFilter<TEntity> entityFilter, Exp
 126    {
 4127        if (values == null || values.Length == 0)
 2128            return entityFilter.Remove(property);
 129
 2130        var valueFilters = values
 2131            .Select(value => ValueFilter.Create(
 2132                FilterOperator.Default,
 2133                value,
 2134                entityFilter.Configuration
 2135                ))
 2136            .ToArray();
 137
 2138        return entityFilter.Replace(property, valueFilters);
 139    }
 140
 141    /// <summary>
 142    /// Replaces the filter for the given property. Existing filters for the same property are removed.
 143    /// </summary>
 144    /// <typeparam name="TEntity">The type of the entity.</typeparam>
 145    /// <typeparam name="TProperty">The type of the property.</typeparam>
 146    /// <param name="entityFilter">The entity filter.</param>
 147    /// <param name="property">The property to filter.</param>
 148    /// <param name="filterOperator">The filter operator to use.</param>
 149    public static EntityFilter<TEntity> Replace<TEntity, TProperty>(this EntityFilter<TEntity> entityFilter, Expression<
 6150        => entityFilter.Replace(property, ValueFilter.Create(filterOperator, entityFilter.Configuration));
 151
 152    /// <summary>
 153    /// Replaces the filter for the given property. Existing filters for the same property are removed.
 154    /// </summary>
 155    /// <typeparam name="TEntity">The type of the entity.</typeparam>
 156    /// <typeparam name="TProperty">The type of the property.</typeparam>
 157    /// <typeparam name="TValue">The type of the value.</typeparam>
 158    /// <param name="entityFilter">The entity filter.</param>
 159    /// <param name="property">The property to filter.</param>
 160    /// <param name="filterOperator">The filter operator to use.</param>
 161    /// <param name="values">The values to filter for. Multiple values are combined with conditional OR.</param>
 162    public static EntityFilter<TEntity> Replace<TEntity, TProperty, TValue>(this EntityFilter<TEntity> entityFilter, Exp
 163    {
 3549164        var isNullableFilterOperator = filterOperator is FilterOperator.IsNull or FilterOperator.NotNull;
 3549165        if ((values == null || values.Length == 0) && !isNullableFilterOperator)
 2166            return entityFilter.Remove(property);
 167
 3547168        var valueFilters = values?
 3547169            .Select(value => ValueFilter.Create(
 3547170                filterOperator,
 3547171                value,
 3547172                entityFilter.Configuration
 3547173                ))
 3547174            .ToArray();
 175
 3546176        entityFilter.Replace(property, valueFilters);
 3546177        return entityFilter;
 178    }
 179
 180    /// <summary>
 181    /// Converts an entity filter to it's corresponding HTTP query parameters.
 182    /// </summary>
 183    /// <typeparam name="TEntity">Type of the entity.</typeparam>
 184    /// <param name="entityFilter">The filter to act on.</param>
 185    public static string ToQueryParams<TEntity>(this EntityFilter<TEntity> entityFilter)
 186    {
 1187        var filteredType = typeof(TEntity);
 1188        var filterableProperties = filteredType.GetFilterableProperties();
 1189        var entityFilterAttribute = filteredType.GetCustomAttribute<EntityFilterAttribute>();
 190
 1191        var queryParams = new List<string>();
 8192        foreach (var property in filterableProperties)
 193        {
 3194            var parameterName = HttpUtility.UrlEncode(property.GetFilterParameterName(entityFilterAttribute?.Prefix));
 3195            var propertyFilters = entityFilter.PropertyFilters.Where(x => x.PropertyName.EqualsOrdinal(property.Name));
 14196            foreach (var filter in propertyFilters)
 197            {
 4198                var values = string.Join(',', filter.ValueFilters.Select(v => HttpUtility.UrlEncode(v.ToString())));
 4199                queryParams.Add($"{parameterName}={values}");
 200            }
 201        }
 202
 1203        return string.Join('&', queryParams);
 204    }
 205}