| | | 1 | | using Microsoft.AspNetCore.Authorization; |
| | | 2 | | using Microsoft.AspNetCore.Mvc; |
| | | 3 | | using Services.ApiServices.ApiSupplierRequestLog; |
| | | 4 | | using Services.ApiServices.JapartsApi; |
| | | 5 | | |
| | | 6 | | namespace OzonOrdersWeb.Areas.Studio2.Controllers.Directories; |
| | | 7 | | |
| | | 8 | | [Authorize(Roles = "User,Admin")] |
| | | 9 | | [Area("Studio2")] |
| | | 10 | | [Route("Japarts/[action]")] |
| | 0 | 11 | | public sealed class JapartsController(JapartsDataManager japarts, ApiSupplierRequestLogStore logs) : Controller |
| | | 12 | | { |
| | 0 | 13 | | [HttpGet, Route("~/Japarts")] public IActionResult Index() => View(); |
| | 0 | 14 | | [HttpGet] public IActionResult Logs() => Json(logs.Get("Japarts")); |
| | | 15 | | [HttpGet] |
| | | 16 | | public async Task<IActionResult> Balance(CancellationToken ct) |
| | | 17 | | { |
| | 0 | 18 | | try { return Json(await japarts.GetBalanceAsync(ct)); } |
| | 0 | 19 | | catch (Exception ex) { return HandleError(ex); } |
| | 0 | 20 | | } |
| | | 21 | | |
| | | 22 | | [HttpGet] |
| | | 23 | | public async Task<IActionResult> Search(string code = "MB831042", string? brand = "Mitsubishi", CancellationToken ct |
| | | 24 | | { |
| | 0 | 25 | | try { return Json(await japarts.SearchAsync(code, brand, ct)); } |
| | 0 | 26 | | catch (Exception ex) { return HandleError(ex); } |
| | 0 | 27 | | } |
| | | 28 | | |
| | | 29 | | [HttpGet] |
| | | 30 | | public async Task<IActionResult> Basket(CancellationToken ct) |
| | | 31 | | { |
| | 0 | 32 | | try { return Json(await japarts.GetBasketAsync(ct)); } |
| | 0 | 33 | | catch (Exception ex) { return HandleError(ex); } |
| | 0 | 34 | | } |
| | | 35 | | |
| | | 36 | | [HttpGet] |
| | | 37 | | public async Task<IActionResult> Orders(int interval = 30, CancellationToken ct = default) |
| | | 38 | | { |
| | 0 | 39 | | try { return Json(await japarts.GetOrdersAsync(interval, ct)); } |
| | 0 | 40 | | catch (Exception ex) { return HandleError(ex); } |
| | 0 | 41 | | } |
| | | 42 | | |
| | 0 | 43 | | private IActionResult HandleError(Exception exception) => exception switch |
| | 0 | 44 | | { |
| | 0 | 45 | | JapartsApiException apiException => StatusCode(StatusCodes.Status502BadGateway, new { error = true, message = ap |
| | 0 | 46 | | _ => StatusCode(StatusCodes.Status500InternalServerError, new { error = true, message = exception.Message }) |
| | 0 | 47 | | }; |
| | | 48 | | } |