| | | 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.NikeiApi; |
| | | 7 | | |
| | | 8 | | namespace OzonOrdersWeb.Areas.Studio2.Controllers.Directories; |
| | | 9 | | |
| | | 10 | | /// <summary>Консоль ручных read-only вызовов Nikei.</summary> |
| | | 11 | | [Authorize(Roles = "User,Admin")] |
| | | 12 | | [Area("Studio2")] |
| | | 13 | | [Route("Nikei/[action]")] |
| | | 14 | | public sealed class NikeiController : Controller |
| | | 15 | | { |
| | | 16 | | private const string TestArticle = "9054"; |
| | | 17 | | private readonly NikeiDataManager _nikei; |
| | | 18 | | private readonly ApiSupplierRequestLogStore _logs; |
| | | 19 | | |
| | 0 | 20 | | public NikeiController(NikeiDataManager nikei, ApiSupplierRequestLogStore logs) => (_nikei, _logs) = (nikei, logs); |
| | | 21 | | |
| | | 22 | | [HttpGet, Route("~/Nikei")] |
| | 0 | 23 | | public IActionResult Index() => View(); |
| | | 24 | | |
| | | 25 | | [HttpGet] |
| | 0 | 26 | | public IActionResult Logs() => Json(_logs.Get("Nikei")); |
| | | 27 | | |
| | | 28 | | [HttpGet] |
| | | 29 | | public async Task<IActionResult> Ping(CancellationToken cancellationToken) |
| | | 30 | | { |
| | 0 | 31 | | try { await _nikei.PingAsync(cancellationToken); return Ok(new { isAvailable = true }); } |
| | 0 | 32 | | catch (Exception ex) { return Error(ex); } |
| | 0 | 33 | | } |
| | | 34 | | |
| | | 35 | | [HttpGet] |
| | | 36 | | public async Task<IActionResult> Read(string operation, CancellationToken cancellationToken) |
| | | 37 | | { |
| | | 38 | | try |
| | | 39 | | { |
| | 0 | 40 | | var result = operation?.ToLowerInvariant() switch |
| | 0 | 41 | | { |
| | 0 | 42 | | "brands" => await _nikei.SearchBrandsAsync(TestArticle, cancellationToken), |
| | 0 | 43 | | "offices" => await _nikei.GetOfficesAsync(cancellationToken), |
| | 0 | 44 | | "addresses" => await _nikei.GetClientAddressesAsync(cancellationToken), |
| | 0 | 45 | | "cart" => await _nikei.GetCartAsync(cancellationToken), |
| | 0 | 46 | | "orders" => await _nikei.GetOrdersAsync(cancellationToken), |
| | 0 | 47 | | "statuses" => await _nikei.GetOrderStatusesAsync(cancellationToken), |
| | 0 | 48 | | _ => null |
| | 0 | 49 | | }; |
| | 0 | 50 | | return result is null ? BadRequest(new { error = true, message = "Недопустимая read-операция." }) : JsonToke |
| | | 51 | | } |
| | 0 | 52 | | catch (Exception ex) { return Error(ex); } |
| | 0 | 53 | | } |
| | | 54 | | |
| | 0 | 55 | | private static IActionResult Error(Exception ex) => ex switch |
| | 0 | 56 | | { |
| | 0 | 57 | | NikeiApiException api => new ObjectResult(new { error = true, message = api.Message }) |
| | 0 | 58 | | { |
| | 0 | 59 | | StatusCode = api.StatusCode is >= 400 and < 600 ? api.StatusCode : StatusCodes.Status502BadGateway |
| | 0 | 60 | | }, |
| | 0 | 61 | | _ => new ObjectResult(new { error = true, message = "Не удалось выполнить запрос к Nikei." }) |
| | 0 | 62 | | { |
| | 0 | 63 | | StatusCode = StatusCodes.Status502BadGateway |
| | 0 | 64 | | } |
| | 0 | 65 | | }; |
| | | 66 | | |
| | 0 | 67 | | private static ContentResult JsonToken(JToken token) => new() { Content = token.ToString(Formatting.None), ContentTy |
| | | 68 | | } |