| | | 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.FavoritPartsApi; |
| | | 7 | | |
| | | 8 | | namespace OzonOrdersWeb.Areas.Studio2.Controllers.Directories; |
| | | 9 | | |
| | | 10 | | [Authorize(Roles = "User,Admin")] |
| | | 11 | | [Area("Studio2")] |
| | | 12 | | [Route("FavoritPartsIstra/[action]")] |
| | | 13 | | public sealed class FavoritPartsIstraController : Controller |
| | | 14 | | { |
| | | 15 | | private const string TestArticle = "01089"; |
| | | 16 | | private readonly FavoritPartsIstraDataManager _favoritParts; |
| | | 17 | | private readonly ApiSupplierRequestLogStore _logs; |
| | 0 | 18 | | public FavoritPartsIstraController(FavoritPartsIstraDataManager favoritParts, ApiSupplierRequestLogStore logs) { _fa |
| | | 19 | | |
| | | 20 | | [HttpGet, Route("~/FavoritPartsIstra")] |
| | 0 | 21 | | public IActionResult Index() => View(); |
| | | 22 | | [HttpGet] |
| | 0 | 23 | | public IActionResult Logs() => Json(_logs.Get("FavoritPartsIstra")); |
| | | 24 | | [HttpGet] |
| | | 25 | | public async Task<IActionResult> Ping(CancellationToken cancellationToken) |
| | | 26 | | { |
| | 0 | 27 | | try { return JsonToken(await _favoritParts.GetProfileAsync(cancellationToken)); } |
| | 0 | 28 | | catch (Exception ex) { return Error(ex); } |
| | 0 | 29 | | } |
| | | 30 | | [HttpGet] |
| | | 31 | | public async Task<IActionResult> Read(string operation, CancellationToken cancellationToken) |
| | | 32 | | { |
| | | 33 | | try |
| | | 34 | | { |
| | 0 | 35 | | var result = operation.ToLowerInvariant() switch |
| | 0 | 36 | | { |
| | 0 | 37 | | "profile" => await _favoritParts.GetProfileAsync(cancellationToken), |
| | 0 | 38 | | "status" => await _favoritParts.GetStatusesAsync(cancellationToken), |
| | 0 | 39 | | "price-search" => await _favoritParts.SearchGoodsAsync(TestArticle, null, false, cancellationToken), |
| | 0 | 40 | | "cart" => await _favoritParts.GetCartAsync(cancellationToken), |
| | 0 | 41 | | _ => null |
| | 0 | 42 | | }; |
| | 0 | 43 | | return result is null ? BadRequest(new { error = true, message = "Недопустимая read-операция." }) : JsonToke |
| | | 44 | | } |
| | 0 | 45 | | catch (Exception ex) { return Error(ex); } |
| | 0 | 46 | | } |
| | 0 | 47 | | private static IActionResult Error(Exception ex) => ex switch |
| | 0 | 48 | | { |
| | 0 | 49 | | FavoritPartsApiException api => new ObjectResult(new { error = true, message = api.Message }) { StatusCode = api |
| | 0 | 50 | | _ => new ObjectResult(new { error = true, message = "Не удалось выполнить запрос к Фаворит Истра." }) { StatusCo |
| | 0 | 51 | | }; |
| | 0 | 52 | | private static ContentResult JsonToken(JToken token) => new() { Content = token.ToString(Formatting.None), ContentTy |
| | | 53 | | } |