< Summary

Information
Class: Fluorite.Strainer.Services.Sorting.SortingApplier
Assembly: Fluorite.Strainer
File(s): /builds/fluorite/strainer/src/Strainer/Services/Sorting/SortingApplier.cs
Line coverage
100%
Covered lines: 38
Uncovered lines: 0
Coverable lines: 38
Total lines: 82
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
TryApplySorting(...)100%1212100%

File(s)

/builds/fluorite/strainer/src/Strainer/Services/Sorting/SortingApplier.cs

#LineLine coverage
 1using Fluorite.Extensions;
 2using Fluorite.Strainer.Exceptions;
 3using Fluorite.Strainer.Models.Sorting.Terms;
 4using Fluorite.Strainer.Services.Metadata;
 5
 6namespace Fluorite.Strainer.Services.Sorting;
 7
 8public class SortingApplier : ISortingApplier
 9{
 10    private readonly ICustomSortingExpressionProvider _customSortingExpressionProvider;
 11    private readonly ISortExpressionProvider _sortExpressionProvider;
 12    private readonly IMetadataFacade _metadataFacade;
 13    private readonly IStrainerOptionsProvider _strainerOptionsProvider;
 14
 615    public SortingApplier(
 616        ICustomSortingExpressionProvider customSortingExpressionProvider,
 617        ISortExpressionProvider sortExpressionProvider,
 618        IMetadataFacade metadataFacade,
 619        IStrainerOptionsProvider strainerOptionsProvider)
 20    {
 621        _customSortingExpressionProvider = Guard.Against.Null(customSortingExpressionProvider);
 622        _sortExpressionProvider = Guard.Against.Null(sortExpressionProvider);
 623        _metadataFacade = Guard.Against.Null(metadataFacade);
 624        _strainerOptionsProvider = Guard.Against.Null(strainerOptionsProvider);
 625    }
 26
 27    public bool TryApplySorting<T>(IList<ISortTerm> sortTerms, IQueryable<T> source, out IQueryable<T>? sortedSource)
 28    {
 529        Guard.Against.Null(sortTerms);
 530        Guard.Against.Null(source);
 31
 532        var options = _strainerOptionsProvider.GetStrainerOptions();
 533        var isSubsequent = false;
 534        var isSortingApplied = false;
 535        sortedSource = null;
 36
 1637        foreach (var sortTerm in sortTerms)
 38        {
 439            var metadata = _metadataFacade.GetMetadata<T>(
 440                isSortableRequired: true,
 441                isFilterableRequired: false,
 442                name: sortTerm.Name);
 43
 444            if (metadata != null)
 45            {
 146                var sortExpression = _sortExpressionProvider.GetExpression<T>(metadata, sortTerm, isSubsequent);
 147                sortedSource = (sortedSource ?? source).OrderWithSortExpression(sortExpression);
 148                isSortingApplied = true;
 49            }
 50            else
 51            {
 352                if (_customSortingExpressionProvider.TryGetCustomExpression<T>(sortTerm, isSubsequent, out var sortExpre
 53                {
 154                    sortedSource = (sortedSource ?? source).OrderWithSortExpression(sortExpression!);
 155                    isSortingApplied = true;
 56                }
 57                else
 58                {
 259                    if (options.ThrowExceptions)
 60                    {
 161                        sortedSource = null;
 62
 163                        throw new StrainerSortNotFoundException(
 164                            sortTerm.Name,
 165                            $"Property or custom sorting method '{sortTerm.Name}' was not found.");
 66                    }
 67                    else
 68                    {
 69                        // Fail all terms since failing to find a correct method will affect sorting overall result.
 170                        sortedSource = null;
 71
 172                        return false;
 73                    }
 74                }
 75            }
 76
 277            isSubsequent = true;
 78        }
 79
 380        return isSortingApplied;
 181    }
 82}