| | | 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("FavoritParts/[action]")] |
| | | 13 | | public sealed class FavoritPartsController : Controller |
| | | 14 | | { |
| | | 15 | | private const string TestArticle = "01089"; |
| | | 16 | | |
| | | 17 | | private readonly FavoritPartsDataManager _favoritParts; |
| | | 18 | | private readonly ApiSupplierRequestLogStore _logs; |
| | | 19 | | |
| | 0 | 20 | | public FavoritPartsController(FavoritPartsDataManager favoritParts, ApiSupplierRequestLogStore logs) |
| | | 21 | | { |
| | 0 | 22 | | _favoritParts = favoritParts; |
| | 0 | 23 | | _logs = logs; |
| | 0 | 24 | | } |
| | | 25 | | |
| | | 26 | | [HttpGet, Route("~/FavoritParts")] |
| | 0 | 27 | | public IActionResult Index() => View(); |
| | | 28 | | |
| | | 29 | | [HttpGet] |
| | 0 | 30 | | public IActionResult Logs() => Json(_logs.Get("FavoritParts")); |
| | | 31 | | |
| | | 32 | | [HttpGet] |
| | | 33 | | public async Task<IActionResult> Ping(CancellationToken cancellationToken) |
| | | 34 | | { |
| | 0 | 35 | | try { return JsonToken(await _favoritParts.GetProfileAsync(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 | | "profile" => await _favoritParts.GetProfileAsync(cancellationToken), |
| | 0 | 47 | | "status" => await _favoritParts.GetStatusesAsync(cancellationToken), |
| | 0 | 48 | | "price-search" => await _favoritParts.SearchGoodsAsync(TestArticle, null, analogues: false, cancellation |
| | 0 | 49 | | "cart" => await _favoritParts.GetCartAsync(cancellationToken), |
| | 0 | 50 | | _ => null |
| | 0 | 51 | | }; |
| | 0 | 52 | | return result is null |
| | 0 | 53 | | ? BadRequest(new { error = true, message = "Недопустимая read-операция." }) |
| | 0 | 54 | | : JsonToken(result); |
| | | 55 | | } |
| | 0 | 56 | | catch (Exception ex) { return Error(ex); } |
| | 0 | 57 | | } |
| | | 58 | | |
| | 0 | 59 | | private static IActionResult Error(Exception ex) => ex switch |
| | 0 | 60 | | { |
| | 0 | 61 | | FavoritPartsApiException api => new ObjectResult(new { error = true, message = api.Message }) { StatusCode = api |
| | 0 | 62 | | _ => new ObjectResult(new { error = true, message = "Не удалось выполнить запрос к FavoritParts." }) { StatusCod |
| | 0 | 63 | | }; |
| | | 64 | | |
| | 0 | 65 | | private static ContentResult JsonToken(JToken token) => new() { Content = token.ToString(Formatting.None), ContentTy |
| | | 66 | | } |