< Summary

Information
Class: Services.ApiServices.NikeiApi.NikeiDataManager
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/ApiServices/NikeiApi/NikeiDataManager.cs
Line coverage
77%
Covered lines: 21
Uncovered lines: 6
Coverable lines: 27
Total lines: 76
Line coverage: 77.7%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
SearchBrandsAsync(...)100%210%
SearchPartsAsync(...)100%210%
GetOfficesAsync(...)100%11100%
GetClientAddressesAsync(...)100%11100%
GetCartAsync(...)100%11100%
GetOrdersAsync(...)100%11100%
GetOrderStatusesAsync(...)100%210%
AddToCartAsync()100%11100%
RemoveFromCartAsync(...)100%11100%
CreateOrderAsync(...)100%11100%
PingAsync()100%210%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/AppRepository/ApiServices/NikeiApi/NikeiDataManager.cs

#LineLine coverage
 1using Newtonsoft.Json.Linq;
 2
 3namespace Services.ApiServices.NikeiApi;
 4
 5/// <summary>Read-only методы Nikei, доступные для диагностики и последующего подключения проценки.</summary>
 6public sealed class NikeiDataManager
 7{
 8    private readonly NikeiHttpClient _client;
 9
 11610    public NikeiDataManager(NikeiHttpClient client) => _client = client;
 11
 12    public Task<JToken> SearchBrandsAsync(string article, CancellationToken cancellationToken = default) =>
 013        _client.GetAsync($"parts/search/{Uri.EscapeDataString(article.Trim())}", cancellationToken);
 14
 15    /// <summary>Точная проценка выбранного бренда. filterByArticle исключает аналоги уже на стороне Nikei.</summary>
 16    public Task<JToken> SearchPartsAsync(string article, string brand, CancellationToken cancellationToken = default) =>
 017        _client.GetAsync($"parts/search/{Uri.EscapeDataString(article.Trim())}/{Uri.EscapeDataString(brand.Trim())}?filt
 18
 19    public Task<JToken> GetOfficesAsync(CancellationToken cancellationToken = default) =>
 120        _client.GetAsync("office/getOffices", cancellationToken);
 21
 22    public Task<JToken> GetClientAddressesAsync(CancellationToken cancellationToken = default) =>
 123        _client.GetAsync("office/getClientAddresses", cancellationToken);
 24
 25    public Task<JToken> GetCartAsync(CancellationToken cancellationToken = default) =>
 126        _client.GetAsync("account/cart", cancellationToken);
 27
 28    public Task<JToken> GetOrdersAsync(CancellationToken cancellationToken = default) =>
 129        _client.GetAsync("account/order", cancellationToken);
 30
 31    public Task<JToken> GetOrderStatusesAsync(CancellationToken cancellationToken = default) =>
 032        _client.GetAsync("misc/order_statuses", cancellationToken);
 33
 34    public async Task<NikeiCartAddResult> AddToCartAsync(string product, int quantity, string comment,
 35        CancellationToken cancellationToken = default)
 36    {
 1537        var response = await _client.PostFormAsync("account/cart/add", new Dictionary<string, string>
 1538        {
 1539            ["product"] = product,
 1540            ["quantity"] = quantity.ToString(System.Globalization.CultureInfo.InvariantCulture),
 1541            ["comment"] = comment
 1542        }, cancellationToken);
 1443        return NikeiCartAddResult.From(response);
 1444    }
 45
 46    public Task<JToken> RemoveFromCartAsync(IReadOnlyCollection<string> products, CancellationToken cancellationToken = 
 647        _client.PostFormAsync("account/cart/remove", products.Select((value, index) =>
 2748            new KeyValuePair<string, string>($"products[{index}]", value)).ToDictionary(x => x.Key, x => x.Value), cance
 49
 50    public Task<JToken> CreateOrderAsync(IReadOnlyCollection<string> products, int shippingType, int shippingAddress,
 51        string comment, CancellationToken cancellationToken = default)
 52    {
 1953        var values = products.Select((value, index) => new KeyValuePair<string, string>($"products[{index}]", value))
 3054            .ToDictionary(x => x.Key, x => x.Value);
 855        values["shipping_type"] = shippingType.ToString(System.Globalization.CultureInfo.InvariantCulture);
 856        values["shipping_address"] = shippingAddress.ToString(System.Globalization.CultureInfo.InvariantCulture);
 857        values["order_comment"] = comment;
 858        return _client.PostFormAsync("account/order/create", values, cancellationToken);
 59    }
 60
 61    public async Task PingAsync(CancellationToken cancellationToken = default)
 62    {
 063        var response = await SearchBrandsAsync("9054", cancellationToken);
 064        NikeiHttpClient.ThrowIfError(response, "/parts/search/9054");
 065    }
 66}
 67
 68public sealed record NikeiCartAddResult(bool IsSuccess, string? ToOrder, string Message)
 69{
 70    public static NikeiCartAddResult From(JToken response)
 71    {
 72        var success = string.Equals(response.Value<string>("status"), "ok", StringComparison.OrdinalIgnoreCase);
 73        return new NikeiCartAddResult(success, response.Value<string>("data"),
 74            response.Value<string>("message") ?? (success ? "Позиция добавлена." : "Nikei отклонил добавление в корзину.
 75    }
 76}