| | | 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.ForumAutoApi; |
| | | 7 | | |
| | | 8 | | namespace OzonOrdersWeb.Areas.Studio2.Controllers.Directories; |
| | | 9 | | |
| | | 10 | | [Authorize(Roles = "User,Admin")] |
| | | 11 | | [Area("Studio2")] |
| | | 12 | | [Route("ForumAutoInterparts/[action]")] |
| | | 13 | | public sealed class ForumAutoInterpartsController : Controller |
| | | 14 | | { |
| | | 15 | | private const string TestArticle = "OC47"; |
| | | 16 | | |
| | | 17 | | private readonly ForumAutoInterpartsDataManager _forumAuto; |
| | | 18 | | private readonly ApiSupplierRequestLogStore _logs; |
| | | 19 | | |
| | 0 | 20 | | public ForumAutoInterpartsController(ForumAutoInterpartsDataManager forumAuto, ApiSupplierRequestLogStore logs) |
| | | 21 | | { |
| | 0 | 22 | | _forumAuto = forumAuto; |
| | 0 | 23 | | _logs = logs; |
| | 0 | 24 | | } |
| | | 25 | | |
| | | 26 | | [HttpGet, Route("~/ForumAutoInterparts")] |
| | 0 | 27 | | public IActionResult Index() => View(); |
| | | 28 | | |
| | | 29 | | [HttpGet] |
| | 0 | 30 | | public IActionResult Logs() => Json(_logs.Get("ForumAutoInterparts")); |
| | | 31 | | |
| | | 32 | | [HttpGet] |
| | | 33 | | public async Task<IActionResult> Ping(CancellationToken cancellationToken) |
| | | 34 | | { |
| | 0 | 35 | | try { return JsonToken(await _forumAuto.GetClientInfoAsync(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 | | { |
| | | 42 | | try |
| | | 43 | | { |
| | 0 | 44 | | var result = operation.ToLowerInvariant() switch |
| | 0 | 45 | | { |
| | 0 | 46 | | "client-info" => await _forumAuto.GetClientInfoAsync(cancellationToken), |
| | 0 | 47 | | "list-brands" => await _forumAuto.GetBrandsAsync(cancellationToken), |
| | 0 | 48 | | "list-goods" => await _forumAuto.ListGoodsAsync(TestArticle, null, cross: false, ct: cancellationToken), |
| | 0 | 49 | | _ => null |
| | 0 | 50 | | }; |
| | 0 | 51 | | return result is null |
| | 0 | 52 | | ? BadRequest(new { error = true, message = "Недопустимая read-операция." }) |
| | 0 | 53 | | : JsonToken(result); |
| | | 54 | | } |
| | 0 | 55 | | catch (Exception ex) { return Error(ex); } |
| | 0 | 56 | | } |
| | | 57 | | |
| | 0 | 58 | | private static IActionResult Error(Exception ex) => ex switch |
| | 0 | 59 | | { |
| | 0 | 60 | | ForumAutoApiException api => new ObjectResult(new { error = true, message = api.Message }) { StatusCode = api.St |
| | 0 | 61 | | _ => new ObjectResult(new { error = true, message = "Не удалось выполнить запрос к Forum-Auto." }) { StatusCode |
| | 0 | 62 | | }; |
| | | 63 | | |
| | 0 | 64 | | private static ContentResult JsonToken(JToken token) => new() { Content = token.ToString(Formatting.None), ContentTy |
| | | 65 | | } |