| | | 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.MlAutoApi; |
| | | 7 | | |
| | | 8 | | namespace OzonOrdersWeb.Areas.Studio2.Controllers.Directories; |
| | | 9 | | |
| | | 10 | | [Authorize(Roles = "User,Admin")] |
| | | 11 | | [Area("Studio2")] |
| | | 12 | | [Route("MlAutoRu/[action]")] |
| | | 13 | | public sealed class MlAutoRuController : Controller |
| | | 14 | | { |
| | | 15 | | private readonly MlAutoRuDataManager _mlAuto; |
| | | 16 | | private readonly ApiSupplierRequestLogStore _logs; |
| | | 17 | | |
| | 0 | 18 | | public MlAutoRuController(MlAutoRuDataManager mlAuto, ApiSupplierRequestLogStore logs) |
| | | 19 | | { |
| | 0 | 20 | | _mlAuto = mlAuto; |
| | 0 | 21 | | _logs = logs; |
| | 0 | 22 | | } |
| | | 23 | | |
| | | 24 | | [HttpGet, Route("~/MlAutoRu")] |
| | 0 | 25 | | public IActionResult Index() => View(); |
| | | 26 | | |
| | | 27 | | [HttpGet] |
| | 0 | 28 | | public IActionResult Logs() => Json(_logs.Get("MlAutoRu")); |
| | | 29 | | |
| | | 30 | | [HttpGet] |
| | | 31 | | public async Task<IActionResult> Ping(CancellationToken cancellationToken) |
| | | 32 | | { |
| | 0 | 33 | | try { return JsonToken(await _mlAuto.PingAsync(cancellationToken)); } |
| | 0 | 34 | | catch (Exception ex) { return Error(ex); } |
| | 0 | 35 | | } |
| | | 36 | | |
| | | 37 | | [HttpGet] |
| | | 38 | | public async Task<IActionResult> Read(string operation, CancellationToken cancellationToken) |
| | | 39 | | { |
| | | 40 | | try |
| | | 41 | | { |
| | 0 | 42 | | var result = operation.ToLowerInvariant() switch |
| | 0 | 43 | | { |
| | 0 | 44 | | "show-info" => await _mlAuto.ShowInfoAsync(cancellationToken), |
| | 0 | 45 | | "storages" => await _mlAuto.GetStoragesAsync(cancellationToken), |
| | 0 | 46 | | "search" => await _mlAuto.SearchAsync("1987302205", "Bosch", cancellationToken), |
| | 0 | 47 | | _ => null |
| | 0 | 48 | | }; |
| | 0 | 49 | | return result is null |
| | 0 | 50 | | ? BadRequest(new { error = true, message = "Недопустимая read-операция." }) |
| | 0 | 51 | | : JsonToken(result); |
| | | 52 | | } |
| | 0 | 53 | | catch (Exception ex) { return Error(ex); } |
| | 0 | 54 | | } |
| | | 55 | | |
| | 0 | 56 | | private static IActionResult Error(Exception ex) => ex switch |
| | 0 | 57 | | { |
| | 0 | 58 | | MlAutoApiException api => new ObjectResult(new { error = true, message = api.Message }) { StatusCode = api.Statu |
| | 0 | 59 | | _ => new ObjectResult(new { error = true, message = "Не удалось выполнить запрос к ML-Auto RU." }) { StatusCode |
| | 0 | 60 | | }; |
| | | 61 | | |
| | 0 | 62 | | private static ContentResult JsonToken(JToken token) => new() { Content = token.ToString(Formatting.None), ContentTy |
| | | 63 | | } |