< Summary

Information
Class: Elsa.KeyValues.Models.KeyValueFilter
Assembly: Elsa.KeyValues
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.KeyValues/Models/KeyValueFilter.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 16
Coverable lines: 16
Total lines: 58
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 10
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_StartsWith()100%210%
get_Key()100%210%
get_Keys()100%210%
get_Take()100%210%
get_OrderByKey()100%210%
Apply(...)0%110100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.KeyValues/Models/KeyValueFilter.cs

#LineLine coverage
 1using Elsa.KeyValues.Entities;
 2
 3namespace Elsa.KeyValues.Models;
 4
 5public class KeyValueFilter
 6{
 7    /// <summary>
 8    /// Gets or sets whether the <see cref="Key"/> needs to match the beginning of the key found.
 9    /// </summary>
 010    public bool StartsWith { get; set; }
 11
 12    /// <summary>
 13    /// Gets or sets the key to filter for.
 14    /// </summary>
 015    public string? Key { get; set; }
 16
 17    /// <summary>
 18    /// Gets or sets the keys to filter for.
 19    /// </summary>
 020    public ICollection<string>? Keys { get; set; }
 21
 22    /// <summary>
 23    /// Gets or sets the maximum number of records to return. <c>null</c> means no limit; <c>0</c> returns no records.
 24    /// </summary>
 025    public int? Take { get; set; }
 26
 27    /// <summary>
 28    /// Gets or sets whether results should be ordered by the persisted key before applying <see cref="Take"/>.
 29    /// The persisted key is stored as <c>Id</c>; <see cref="SerializedKeyValuePair.Key"/> is an alias.
 30    /// </summary>
 031    public bool OrderByKey { get; set; }
 32
 33    /// <summary>
 34    /// Applies the filter to the specified queryable.
 35    /// </summary>
 36    /// <param name="queryable">The queryable.</param>
 37    /// <returns>The filtered queryable.</returns>
 38    public IQueryable<SerializedKeyValuePair> Apply(IQueryable<SerializedKeyValuePair> queryable)
 39    {
 040        var filter = this;
 041        if (filter.Key != null)
 42        {
 043            queryable = StartsWith
 044                ? queryable.Where(x => x.Id.StartsWith(filter.Key))
 045                : queryable.Where(x => x.Id == filter.Key);
 46        }
 47
 048        if (filter.Keys != null) queryable = queryable.Where(x => filter.Keys.Contains(x.Id));
 49
 050        if (filter.OrderByKey)
 051            queryable = queryable.OrderBy(x => x.Id);
 52
 053        if (filter.Take != null)
 054            queryable = queryable.Take(Math.Max(0, filter.Take.Value));
 55
 056        return queryable;
 57    }
 58}