< Summary

Information
Class: Services.BitrixStockServices.BitrixStockFilterQueryParser
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/BitrixStockServices/BitrixStockFilterQueryParser.cs
Line coverage
100%
Covered lines: 69
Uncovered lines: 0
Coverable lines: 69
Total lines: 118
Line coverage: 100%
Branch coverage
96%
Covered branches: 29
Total branches: 30
Branch coverage: 96.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
FromQueryString(...)100%88100%
First(...)75%44100%
ParseInt(...)100%22100%
ParseDouble(...)100%44100%
ParseDecimal(...)100%44100%
ParseDate(...)100%22100%
HasTruthy(...)100%66100%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/AppRepository/BitrixStockServices/BitrixStockFilterQueryParser.cs

#LineLine coverage
 1using Microsoft.AspNetCore.WebUtilities;
 2using Microsoft.Extensions.Primitives;
 3using OzonOrdersWeb.Areas.Studio2.ViewModels.Bitrix;
 4
 5namespace Services.BitrixStockServices;
 6
 7public static class BitrixStockFilterQueryParser
 8{
 9    public static RemainingStockFilter FromQueryString(string? queryString)
 10    {
 3111        var filter = new RemainingStockFilter();
 3112        if (string.IsNullOrWhiteSpace(queryString))
 13        {
 314            return filter;
 15        }
 16
 2817        var q = queryString.Trim();
 2818        if (!q.StartsWith('?'))
 19        {
 120            q = "?" + q;
 21        }
 22
 2823        var parsed = QueryHelpers.ParseQuery(q);
 24
 2825        filter.ProductId = ParseInt(parsed, "ProductId");
 2826        filter.Article = First(parsed, "Article");
 2827        filter.PreviewText = First(parsed, "PreviewText");
 2828        filter.Active = First(parsed, "Active");
 2829        filter.Available = First(parsed, "Available");
 2830        filter.Supplier = First(parsed, "Supplier");
 2831        filter.QuantityFrom = ParseInt(parsed, "QuantityFrom");
 2832        filter.QuantityTo = ParseInt(parsed, "QuantityTo");
 2833        filter.DateFrom = ParseDate(parsed, "DateFrom");
 2834        filter.DateTo = ParseDate(parsed, "DateTo");
 2835        filter.PriceValueFrom = ParseDecimal(parsed, "PriceValueFrom");
 2836        filter.PriceValueTo = ParseDecimal(parsed, "PriceValueTo");
 2837        filter.RegistrationPriceFrom = ParseDecimal(parsed, "RegistrationPriceFrom");
 2838        filter.RegistrationPriceTo = ParseDecimal(parsed, "RegistrationPriceTo");
 2839        filter.StoreId = ParseInt(parsed, "StoreId");
 2840        filter.StoreTitle = First(parsed, "StoreTitle");
 2841        filter.AmountFrom = ParseDouble(parsed, "AmountFrom");
 2842        filter.AmountTo = ParseDouble(parsed, "AmountTo");
 2843        filter.OzonStoreTitle = First(parsed, "OzonStoreTitle");
 2844        filter.OzonAmountFrom = ParseDouble(parsed, "OzonAmountFrom");
 2845        filter.OzonAmountTo = ParseDouble(parsed, "OzonAmountTo");
 2846        filter.SelectedClientId = ParseInt(parsed, "SelectedClientId");
 2847        filter.Page = ParseInt(parsed, "Page") ?? 1;
 2848        filter.PageSize = ParseInt(parsed, "PageSize") ?? 10_000;
 2849        filter.SortColumn = First(parsed, "SortColumn");
 2850        filter.SortDirection = First(parsed, "SortDirection") ?? "asc";
 51
 2852        filter.LoadOzonWarehouses = HasTruthy(parsed, "LoadOzonWarehouses");
 2853        filter.LoadOneCStocks = HasTruthy(parsed, "LoadOneCStocks");
 2854        filter.DiffOzonWarehouses = HasTruthy(parsed, "DiffOzonWarehouses");
 2855        filter.LoadOzonPriceIndexes = HasTruthy(parsed, "LoadOzonPriceIndexes");
 2856        filter.RedOzonPriceIndexes = HasTruthy(parsed, "RedOzonPriceIndexes");
 2857        filter.OnlyInactive = HasTruthy(parsed, "OnlyInactive");
 2858        filter.OnlyActive = HasTruthy(parsed, "OnlyActive");
 2859        filter.DisablePagination = HasTruthy(parsed, "DisablePagination");
 60
 2861        if (parsed.TryGetValue("SelectedStoreTitles", out var storeValues))
 62        {
 163            filter.SelectedStoreTitles = storeValues
 664                .Where(v => !string.IsNullOrWhiteSpace(v))
 465                .Select(v => v!.Trim())
 166                .Distinct(StringComparer.OrdinalIgnoreCase)
 167                .ToList();
 68        }
 69
 2870        return filter;
 71    }
 72
 73    private static string? First(Dictionary<string, StringValues> parsed, string key) =>
 72874        parsed.TryGetValue(key, out var v) ? v.FirstOrDefault()?.Trim() : null;
 75
 76    private static int? ParseInt(Dictionary<string, StringValues> parsed, string key)
 77    {
 19678        var s = First(parsed, key);
 19679        return int.TryParse(s, out var n) ? n : null;
 80    }
 81
 82    private static double? ParseDouble(Dictionary<string, StringValues> parsed, string key)
 83    {
 11284        var s = First(parsed, key);
 11285        return double.TryParse(s?.Replace(',', '.'), System.Globalization.NumberStyles.Any,
 11286            System.Globalization.CultureInfo.InvariantCulture, out var n)
 11287            ? n
 11288            : null;
 89    }
 90
 91    private static decimal? ParseDecimal(Dictionary<string, StringValues> parsed, string key)
 92    {
 11293        var s = First(parsed, key);
 11294        return decimal.TryParse(s?.Replace(',', '.'), System.Globalization.NumberStyles.Any,
 11295            System.Globalization.CultureInfo.InvariantCulture, out var n)
 11296            ? n
 11297            : null;
 98    }
 99
 100    private static DateTime? ParseDate(Dictionary<string, StringValues> parsed, string key)
 101    {
 56102        var s = First(parsed, key);
 56103        return DateTime.TryParse(s, out var d) ? d : null;
 104    }
 105
 106    private static bool HasTruthy(Dictionary<string, StringValues> parsed, string key)
 107    {
 224108        if (!parsed.TryGetValue(key, out var values))
 109        {
 143110            return false;
 111        }
 112
 81113        return values.Any(v =>
 163114            string.Equals(v, "true", StringComparison.OrdinalIgnoreCase) ||
 163115            string.Equals(v, "on", StringComparison.OrdinalIgnoreCase) ||
 163116            v == "1");
 117    }
 118}