| | | 1 | | using Microsoft.AspNetCore.Authorization; |
| | | 2 | | using Microsoft.AspNetCore.Mvc; |
| | | 3 | | using Newtonsoft.Json; |
| | | 4 | | using Newtonsoft.Json.Linq; |
| | | 5 | | using Services.ApiServices.ApiSupplierRequestLog; |
| | | 6 | | using Services.ApiServices.ProfitLigaApi; |
| | | 7 | | |
| | | 8 | | namespace OzonOrdersWeb.Areas.Studio2.Controllers.Directories; |
| | | 9 | | |
| | | 10 | | /// <summary>Консоль ручных read-only вызовов «Профит-Лиги».</summary> |
| | | 11 | | [Authorize(Roles = "User,Admin")] |
| | | 12 | | [Area("Studio2")] |
| | | 13 | | [Route("ProfitLiga/[action]")] |
| | | 14 | | public 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 | | |
| | 0 | 22 | | public ProfitLigaController(ProfitLigaDataManager profitLiga, ApiSupplierRequestLogStore logs) |
| | | 23 | | { |
| | 0 | 24 | | _profitLiga = profitLiga; |
| | 0 | 25 | | _logs = logs; |
| | 0 | 26 | | } |
| | | 27 | | |
| | | 28 | | [HttpGet, Route("~/ProfitLiga")] |
| | 0 | 29 | | public IActionResult Index() => View(); |
| | | 30 | | |
| | | 31 | | [HttpGet] |
| | 0 | 32 | | public IActionResult Logs() => Json(_logs.Get("ProfitLiga")); |
| | | 33 | | |
| | | 34 | | [HttpGet] |
| | | 35 | | public async Task<IActionResult> Ping(CancellationToken cancellationToken) |
| | | 36 | | { |
| | 0 | 37 | | try { return JsonToken(await _profitLiga.GetWarehousesRawAsync(cancellationToken)); } |
| | 0 | 38 | | catch (Exception ex) { return Error(ex); } |
| | 0 | 39 | | } |
| | | 40 | | |
| | | 41 | | [HttpGet] |
| | | 42 | | public async Task<IActionResult> Read(string operation, CancellationToken cancellationToken) |
| | | 43 | | { |
| | | 44 | | try |
| | | 45 | | { |
| | 0 | 46 | | var result = operation?.ToLowerInvariant() switch |
| | 0 | 47 | | { |
| | 0 | 48 | | "warehouses" => await _profitLiga.GetWarehousesRawAsync(cancellationToken), |
| | 0 | 49 | | "params" => await _profitLiga.GetOrderParamsRawAsync(cancellationToken), |
| | 0 | 50 | | "products" => await _profitLiga.SearchProductsAsync(TestArticle, cancellationToken), |
| | 0 | 51 | | "items" => await _profitLiga.SearchItemsAsync(TestArticle, cancellationToken), |
| | 0 | 52 | | "crosses" => await _profitLiga.SearchCrossesAsync(TestArticle, TestBrand, false, cancellationToken), |
| | 0 | 53 | | // Замены доступны только здесь: в проценку они не попадают по контракту API-поставщиков. |
| | 0 | 54 | | "replaces" => await _profitLiga.SearchCrossesAsync(TestArticle, TestBrand, true, cancellationToken), |
| | 0 | 55 | | "cart" => await _profitLiga.GetCartRawAsync(cancellationToken), |
| | 0 | 56 | | "orders" => await _profitLiga.GetOrdersAsync(0, ct: cancellationToken), |
| | 0 | 57 | | _ => null |
| | 0 | 58 | | }; |
| | | 59 | | |
| | 0 | 60 | | return result is null |
| | 0 | 61 | | ? BadRequest(new { error = true, message = "Недопустимая read-операция." }) |
| | 0 | 62 | | : JsonToken(result); |
| | | 63 | | } |
| | 0 | 64 | | catch (Exception ex) { return Error(ex); } |
| | 0 | 65 | | } |
| | | 66 | | |
| | 0 | 67 | | private static IActionResult Error(Exception ex) => ex switch |
| | 0 | 68 | | { |
| | 0 | 69 | | ProfitLigaApiException api => new ObjectResult(new { error = true, message = api.Message }) |
| | 0 | 70 | | { |
| | 0 | 71 | | StatusCode = api.StatusCode is >= 400 and < 600 ? api.StatusCode : StatusCodes.Status502BadGateway |
| | 0 | 72 | | }, |
| | 0 | 73 | | _ => new ObjectResult(new { error = true, message = "Не удалось выполнить запрос к Профит-Лиге." }) |
| | 0 | 74 | | { |
| | 0 | 75 | | StatusCode = StatusCodes.Status502BadGateway |
| | 0 | 76 | | } |
| | 0 | 77 | | }; |
| | | 78 | | |
| | | 79 | | private static ContentResult JsonToken(JToken token) => |
| | 0 | 80 | | new() { Content = token.ToString(Formatting.None), ContentType = "application/json" }; |
| | | 81 | | } |