< Summary

Information
Class: OzonOrdersWeb.Areas.Studio2.Controllers.Directories.ProfitLigaController
Assembly: OzonOrdersWeb
File(s): /home/runner/work/Studio2Prod/Studio2Prod/OzonOrdersWeb/Areas/Studio2/Controllers/Directories/ProfitLigaController.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 39
Coverable lines: 39
Total lines: 81
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 46
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
Index()100%210%
Logs()100%210%
Ping()100%210%
Read()0%1482380%
Error(...)0%7280%
JsonToken(...)100%210%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/OzonOrdersWeb/Areas/Studio2/Controllers/Directories/ProfitLigaController.cs

#LineLine coverage
 1using Microsoft.AspNetCore.Authorization;
 2using Microsoft.AspNetCore.Mvc;
 3using Newtonsoft.Json;
 4using Newtonsoft.Json.Linq;
 5using Services.ApiServices.ApiSupplierRequestLog;
 6using Services.ApiServices.ProfitLigaApi;
 7
 8namespace OzonOrdersWeb.Areas.Studio2.Controllers.Directories;
 9
 10/// <summary>Консоль ручных read-only вызовов «Профит-Лиги».</summary>
 11[Authorize(Roles = "User,Admin")]
 12[Area("Studio2")]
 13[Route("ProfitLiga/[action]")]
 14public sealed class ProfitLigaController : Controller
 15{
 16    private const string TestArticle = "2382";
 17    private const string TestBrand = "NGK";
 18
 19    private readonly ProfitLigaDataManager _profitLiga;
 20    private readonly ApiSupplierRequestLogStore _logs;
 21
 022    public ProfitLigaController(ProfitLigaDataManager profitLiga, ApiSupplierRequestLogStore logs)
 23    {
 024        _profitLiga = profitLiga;
 025        _logs = logs;
 026    }
 27
 28    [HttpGet, Route("~/ProfitLiga")]
 029    public IActionResult Index() => View();
 30
 31    [HttpGet]
 032    public IActionResult Logs() => Json(_logs.Get("ProfitLiga"));
 33
 34    [HttpGet]
 35    public async Task<IActionResult> Ping(CancellationToken cancellationToken)
 36    {
 037        try { return JsonToken(await _profitLiga.GetWarehousesRawAsync(cancellationToken)); }
 038        catch (Exception ex) { return Error(ex); }
 039    }
 40
 41    [HttpGet]
 42    public async Task<IActionResult> Read(string operation, CancellationToken cancellationToken)
 43    {
 44        try
 45        {
 046            var result = operation?.ToLowerInvariant() switch
 047            {
 048                "warehouses" => await _profitLiga.GetWarehousesRawAsync(cancellationToken),
 049                "params" => await _profitLiga.GetOrderParamsRawAsync(cancellationToken),
 050                "products" => await _profitLiga.SearchProductsAsync(TestArticle, cancellationToken),
 051                "items" => await _profitLiga.SearchItemsAsync(TestArticle, cancellationToken),
 052                "crosses" => await _profitLiga.SearchCrossesAsync(TestArticle, TestBrand, false, cancellationToken),
 053                // Замены доступны только здесь: в проценку они не попадают по контракту API-поставщиков.
 054                "replaces" => await _profitLiga.SearchCrossesAsync(TestArticle, TestBrand, true, cancellationToken),
 055                "cart" => await _profitLiga.GetCartRawAsync(cancellationToken),
 056                "orders" => await _profitLiga.GetOrdersAsync(0, ct: cancellationToken),
 057                _ => null
 058            };
 59
 060            return result is null
 061                ? BadRequest(new { error = true, message = "Недопустимая read-операция." })
 062                : JsonToken(result);
 63        }
 064        catch (Exception ex) { return Error(ex); }
 065    }
 66
 067    private static IActionResult Error(Exception ex) => ex switch
 068    {
 069        ProfitLigaApiException api => new ObjectResult(new { error = true, message = api.Message })
 070        {
 071            StatusCode = api.StatusCode is >= 400 and < 600 ? api.StatusCode : StatusCodes.Status502BadGateway
 072        },
 073        _ => new ObjectResult(new { error = true, message = "Не удалось выполнить запрос к Профит-Лиге." })
 074        {
 075            StatusCode = StatusCodes.Status502BadGateway
 076        }
 077    };
 78
 79    private static ContentResult JsonToken(JToken token) =>
 080        new() { Content = token.ToString(Formatting.None), ContentType = "application/json" };
 81}