| | | 1 | | using Microsoft.AspNetCore.Authorization; |
| | | 2 | | using Microsoft.AspNetCore.Mvc; |
| | | 3 | | using Newtonsoft.Json.Linq; |
| | | 4 | | using Newtonsoft.Json; |
| | | 5 | | using Services.ApiServices.MotexApi; |
| | | 6 | | using Services.ApiServices.ApiSupplierRequestLog; |
| | | 7 | | |
| | | 8 | | namespace OzonOrdersWeb.Areas.Studio2.Controllers.Directories; |
| | | 9 | | |
| | | 10 | | [Authorize(Roles = "User,Admin")] |
| | | 11 | | [Area("Studio2")] |
| | | 12 | | [Route("Motex/[action]")] |
| | | 13 | | public sealed class MotexController : Controller |
| | | 14 | | { |
| | 0 | 15 | | private static readonly IReadOnlyDictionary<string, string> ReadOperations = new Dictionary<string, string>(StringCo |
| | 0 | 16 | | { |
| | 0 | 17 | | ["by-code"] = "/api/v1/Articles/ByCode", |
| | 0 | 18 | | ["search"] = "/api/v1/Articles/Search", |
| | 0 | 19 | | ["stores"] = "/api/v1/Stores/GetStoreList", |
| | 0 | 20 | | ["user-info"] = "/api/v1/Users/GetUserInfo", |
| | 0 | 21 | | ["user-settings"] = "/api/v1/Users/GetUserSettings", |
| | 0 | 22 | | ["delivery-addresses"] = "/api/v1/Users/GetUserDeliveryAddresses", |
| | 0 | 23 | | ["order-details"] = "/api/v1/Orders/GetOrderDetails", |
| | 0 | 24 | | ["orders-by-date"] = "/api/v1/Orders/GetOrdersByDateRange" |
| | 0 | 25 | | }; |
| | 0 | 26 | | private static readonly IReadOnlyDictionary<string, string> SearchOperations = new Dictionary<string, string>(String |
| | 0 | 27 | | { |
| | 0 | 28 | | ["orders-by-ids"] = "/api/v1/Orders/GetOrdersByIds" |
| | 0 | 29 | | }; |
| | | 30 | | private readonly MotexDataManager _motex; |
| | | 31 | | private readonly ApiSupplierRequestLogStore _logs; |
| | | 32 | | |
| | 0 | 33 | | public MotexController(MotexDataManager motex, ApiSupplierRequestLogStore logs) |
| | | 34 | | { |
| | 0 | 35 | | _motex = motex; |
| | 0 | 36 | | _logs = logs; |
| | 0 | 37 | | } |
| | | 38 | | |
| | | 39 | | [HttpGet, Route("~/Motex")] |
| | 0 | 40 | | public IActionResult Index() => View(); |
| | | 41 | | |
| | | 42 | | [HttpGet] |
| | 0 | 43 | | public IActionResult Logs() => Json(_logs.Get("Motex")); |
| | | 44 | | |
| | | 45 | | [HttpGet] |
| | | 46 | | public async Task<IActionResult> Ping(CancellationToken cancellationToken) |
| | | 47 | | { |
| | 0 | 48 | | try { return JsonToken(await _motex.PingAsync(cancellationToken)); } |
| | 0 | 49 | | catch (Exception ex) { return Error(ex); } |
| | 0 | 50 | | } |
| | | 51 | | |
| | | 52 | | [HttpGet] |
| | | 53 | | public async Task<IActionResult> Read(string operation, CancellationToken cancellationToken) |
| | | 54 | | { |
| | 0 | 55 | | if (!ReadOperations.TryGetValue(operation, out var path)) return BadRequest(new { error = true, message = "Недоп |
| | 0 | 56 | | var query = Request.Query.Where(x => x.Key is not "operation").ToDictionary(x => x.Key, x => (string?)x.Value.To |
| | 0 | 57 | | try { return JsonToken(await _motex.GetAsync(path, query, cancellationToken)); } |
| | 0 | 58 | | catch (Exception ex) { return Error(ex); } |
| | 0 | 59 | | } |
| | | 60 | | |
| | | 61 | | [HttpGet] |
| | | 62 | | public async Task<IActionResult> Search(string operation, string payload, CancellationToken cancellationToken) |
| | | 63 | | { |
| | 0 | 64 | | if (!SearchOperations.TryGetValue(operation, out var path)) return BadRequest(new { error = true, message = "Нед |
| | 0 | 65 | | try { return JsonToken(await _motex.SearchAsync(path, JToken.Parse(payload), cancellationToken)); } |
| | 0 | 66 | | catch (Newtonsoft.Json.JsonReaderException) { return BadRequest(new { error = true, message = "Тело запроса долж |
| | 0 | 67 | | catch (Exception ex) { return Error(ex); } |
| | 0 | 68 | | } |
| | | 69 | | |
| | 0 | 70 | | private IActionResult Error(Exception ex) => ex switch |
| | 0 | 71 | | { |
| | 0 | 72 | | MotexApiException api => StatusCode(api.StatusCode is >= 400 and < 600 ? api.StatusCode.Value : 502, new { error |
| | 0 | 73 | | _ => StatusCode(500, new { error = true, message = "Не удалось выполнить запрос к МоТехС." }) |
| | 0 | 74 | | }; |
| | | 75 | | |
| | 0 | 76 | | private ContentResult JsonToken(JToken token) => Content(token.ToString(Formatting.None), "application/json"); |
| | | 77 | | } |