< Summary

Information
Class: Services.ApiServices.NikeiApi.NikeiCartAddResult
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/ApiServices/NikeiApi/NikeiDataManager.cs
Line coverage
100%
Covered lines: 4
Uncovered lines: 0
Coverable lines: 4
Total lines: 76
Line coverage: 100%
Branch coverage
25%
Covered branches: 1
Total branches: 4
Branch coverage: 25%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_IsSuccess()100%11100%
From(...)25%44100%

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
 10    public NikeiDataManager(NikeiHttpClient client) => _client = client;
 11
 12    public Task<JToken> SearchBrandsAsync(string article, CancellationToken cancellationToken = default) =>
 13        _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) =>
 17        _client.GetAsync($"parts/search/{Uri.EscapeDataString(article.Trim())}/{Uri.EscapeDataString(brand.Trim())}?filt
 18
 19    public Task<JToken> GetOfficesAsync(CancellationToken cancellationToken = default) =>
 20        _client.GetAsync("office/getOffices", cancellationToken);
 21
 22    public Task<JToken> GetClientAddressesAsync(CancellationToken cancellationToken = default) =>
 23        _client.GetAsync("office/getClientAddresses", cancellationToken);
 24
 25    public Task<JToken> GetCartAsync(CancellationToken cancellationToken = default) =>
 26        _client.GetAsync("account/cart", cancellationToken);
 27
 28    public Task<JToken> GetOrdersAsync(CancellationToken cancellationToken = default) =>
 29        _client.GetAsync("account/order", cancellationToken);
 30
 31    public Task<JToken> GetOrderStatusesAsync(CancellationToken cancellationToken = default) =>
 32        _client.GetAsync("misc/order_statuses", cancellationToken);
 33
 34    public async Task<NikeiCartAddResult> AddToCartAsync(string product, int quantity, string comment,
 35        CancellationToken cancellationToken = default)
 36    {
 37        var response = await _client.PostFormAsync("account/cart/add", new Dictionary<string, string>
 38        {
 39            ["product"] = product,
 40            ["quantity"] = quantity.ToString(System.Globalization.CultureInfo.InvariantCulture),
 41            ["comment"] = comment
 42        }, cancellationToken);
 43        return NikeiCartAddResult.From(response);
 44    }
 45
 46    public Task<JToken> RemoveFromCartAsync(IReadOnlyCollection<string> products, CancellationToken cancellationToken = 
 47        _client.PostFormAsync("account/cart/remove", products.Select((value, index) =>
 48            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    {
 53        var values = products.Select((value, index) => new KeyValuePair<string, string>($"products[{index}]", value))
 54            .ToDictionary(x => x.Key, x => x.Value);
 55        values["shipping_type"] = shippingType.ToString(System.Globalization.CultureInfo.InvariantCulture);
 56        values["shipping_address"] = shippingAddress.ToString(System.Globalization.CultureInfo.InvariantCulture);
 57        values["order_comment"] = comment;
 58        return _client.PostFormAsync("account/order/create", values, cancellationToken);
 59    }
 60
 61    public async Task PingAsync(CancellationToken cancellationToken = default)
 62    {
 63        var response = await SearchBrandsAsync("9054", cancellationToken);
 64        NikeiHttpClient.ThrowIfError(response, "/parts/search/9054");
 65    }
 66}
 67
 5568public sealed record NikeiCartAddResult(bool IsSuccess, string? ToOrder, string Message)
 69{
 70    public static NikeiCartAddResult From(JToken response)
 71    {
 1472        var success = string.Equals(response.Value<string>("status"), "ok", StringComparison.OrdinalIgnoreCase);
 1473        return new NikeiCartAddResult(success, response.Value<string>("data"),
 1474            response.Value<string>("message") ?? (success ? "Позиция добавлена." : "Nikei отклонил добавление в корзину.
 75    }
 76}