| | | 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("MlAuto/[action]")] |
| | | 13 | | public sealed class MlAutoController : Controller |
| | | 14 | | { |
| | 0 | 15 | | private static readonly IReadOnlyDictionary<string, string> ReadOperations = new Dictionary<string, string>(StringCo |
| | 0 | 16 | | { |
| | 0 | 17 | | ["show-info"] = "/ShowInfo/", ["storages"] = "/ImportersSearch/", ["brands"] = "/GetBrands/", |
| | 0 | 18 | | ["article-search"] = "/ArticleSearch/", ["search"] = "/Search/", ["delivery-addresses"] = "/MyDeliveryAddress/", |
| | 0 | 19 | | ["price"] = "/Price/", ["order"] = "/GetOrderByNumber/", ["orders-by-ids"] = "/GetOrdersByIds/", |
| | 0 | 20 | | ["orders-by-date"] = "/GetOrdersByDateRange/", ["my-orders"] = "/GetMyOrders/", ["my-returns"] = "/GetMyReturns/ |
| | 0 | 21 | | ["return"] = "/GetReturnByNumber/" |
| | 0 | 22 | | }; |
| | | 23 | | private readonly MlAutoDataManager _mlAuto; |
| | | 24 | | private readonly ApiSupplierRequestLogStore _logs; |
| | | 25 | | |
| | 0 | 26 | | public MlAutoController(MlAutoDataManager mlAuto, ApiSupplierRequestLogStore logs) { _mlAuto = mlAuto; _logs = logs; |
| | | 27 | | |
| | | 28 | | [HttpGet, Route("~/MlAuto")] |
| | 0 | 29 | | public IActionResult Index() => View(); |
| | 0 | 30 | | [HttpGet] public IActionResult Logs() => Json(_logs.Get("MlAuto")); |
| | | 31 | | |
| | | 32 | | [HttpGet] |
| | | 33 | | public async Task<IActionResult> Ping(CancellationToken cancellationToken) |
| | | 34 | | { |
| | 0 | 35 | | try { return JsonToken(await _mlAuto.PingAsync(cancellationToken)); } |
| | 0 | 36 | | catch (Exception ex) { return Error(ex); } |
| | 0 | 37 | | } |
| | | 38 | | |
| | | 39 | | [HttpGet] |
| | | 40 | | public async Task<IActionResult> Read(string operation, CancellationToken cancellationToken) |
| | | 41 | | { |
| | 0 | 42 | | if (!ReadOperations.TryGetValue(operation, out var path)) return BadRequest(new { error = true, message = "Недоп |
| | 0 | 43 | | var parameters = Request.Query.Where(x => x.Key is not "operation").ToDictionary(x => x.Key, x => (string?)x.Val |
| | 0 | 44 | | try { return JsonToken(await _mlAuto.PostAsync(path, parameters, cancellationToken)); } |
| | 0 | 45 | | catch (Exception ex) { return Error(ex); } |
| | 0 | 46 | | } |
| | | 47 | | |
| | 0 | 48 | | private static IActionResult Error(Exception ex) => ex switch |
| | 0 | 49 | | { |
| | 0 | 50 | | MlAutoApiException api => new ObjectResult(new { error = true, message = api.Message }) { StatusCode = api.Statu |
| | 0 | 51 | | _ => new ObjectResult(new { error = true, message = "Не удалось выполнить запрос к ML-Auto." }) { StatusCode = S |
| | 0 | 52 | | }; |
| | | 53 | | |
| | 0 | 54 | | private static ContentResult JsonToken(JToken token) => new() { Content = token.ToString(Formatting.None), ContentTy |
| | | 55 | | } |