| | | 1 | | using Microsoft.AspNetCore.Authorization; |
| | | 2 | | using Microsoft.AspNetCore.Mvc; |
| | | 3 | | using Microsoft.Extensions.Caching.Memory; |
| | | 4 | | using OzonDomains.Models; |
| | | 5 | | using Services.ApiServices.OzonApi; |
| | | 6 | | using Services.ApiServices.OzonApi.Filters; |
| | | 7 | | using Services.DataServices; |
| | | 8 | | |
| | | 9 | | namespace OzonOrdersWeb.Areas.Studio2.Controllers; |
| | | 10 | | |
| | | 11 | | [Area("Studio2")] |
| | | 12 | | [Authorize(Roles = "User,Admin")] |
| | | 13 | | public class OzonProductAttributesController : Controller |
| | | 14 | | { |
| | | 15 | | private readonly OrdersDataServices _ordersDataServices; |
| | | 16 | | private readonly OzonApiDataManager _ozonApiDataManager; |
| | | 17 | | private readonly OzonClientServices _ozonClientServices; |
| | | 18 | | private readonly IMemoryCache _cache; |
| | | 19 | | |
| | 0 | 20 | | public OzonProductAttributesController( |
| | 0 | 21 | | OrdersDataServices ordersDataServices, |
| | 0 | 22 | | OzonApiDataManager ozonApiDataManager, |
| | 0 | 23 | | OzonClientServices ozonClientServices, |
| | 0 | 24 | | IMemoryCache cache) |
| | | 25 | | { |
| | 0 | 26 | | _ordersDataServices = ordersDataServices; |
| | 0 | 27 | | _ozonApiDataManager = ozonApiDataManager; |
| | 0 | 28 | | _ozonClientServices = ozonClientServices; |
| | 0 | 29 | | _cache = cache; |
| | 0 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Получить описание характеристик товара Ozon по id заказа. |
| | | 34 | | /// Для фильтра используется `product_id`, равный `order.ProductInfo.OzonProductId`. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="orderId">Id заказа.</param> |
| | | 37 | | /// <param name="lastId">last_id для постраничной навигации (пусто для первого запроса).</param> |
| | | 38 | | /// <param name="limit">Количество элементов на странице.</param> |
| | | 39 | | /// <param name="sortBy">Параметр сортировки: sku / offer_id / id / title.</param> |
| | | 40 | | /// <param name="sortDir">Направление сортировки: ASC / DESC.</param> |
| | | 41 | | [HttpGet] |
| | | 42 | | public async Task<IActionResult> GetByOrderId( |
| | | 43 | | int orderId, |
| | | 44 | | string lastId = "", |
| | | 45 | | int limit = 100, |
| | | 46 | | string sortBy = "id", |
| | | 47 | | string sortDir = "ASC") |
| | | 48 | | { |
| | 0 | 49 | | if (orderId <= 0) |
| | | 50 | | { |
| | 0 | 51 | | return BadRequest("Parameter `orderId` must be > 0."); |
| | | 52 | | } |
| | | 53 | | |
| | 0 | 54 | | limit = Math.Clamp(limit, 1, 1000); |
| | 0 | 55 | | sortBy = string.IsNullOrWhiteSpace(sortBy) ? "id" : sortBy; |
| | 0 | 56 | | sortDir = string.IsNullOrWhiteSpace(sortDir) ? "ASC" : sortDir; |
| | | 57 | | |
| | 0 | 58 | | var order = await _ordersDataServices.GetOrder(orderId).ConfigureAwait(false); |
| | 0 | 59 | | if (order == null) |
| | | 60 | | { |
| | 0 | 61 | | return NotFound(); |
| | | 62 | | } |
| | | 63 | | |
| | 0 | 64 | | if (order.OzonClientId == null || order.OzonClientId.Value <= 0) |
| | | 65 | | { |
| | 0 | 66 | | return BadRequest("Order does not have a valid `OzonClientId`."); |
| | | 67 | | } |
| | | 68 | | |
| | 0 | 69 | | var ozonClient = await _ozonClientServices.GetOzonClientAsync(order.OzonClientId.Value).ConfigureAwait(false); |
| | 0 | 70 | | if (ozonClient == null) |
| | | 71 | | { |
| | 0 | 72 | | return BadRequest("Ozon client not found."); |
| | | 73 | | } |
| | | 74 | | |
| | 0 | 75 | | var clientId = ozonClient.DecryptClientId ?? ozonClient.ClientId ?? ""; |
| | 0 | 76 | | var apiKey = ozonClient.DecryptApiKey ?? ozonClient.ApiKey ?? ""; |
| | 0 | 77 | | if (string.IsNullOrWhiteSpace(clientId) || string.IsNullOrWhiteSpace(apiKey)) |
| | | 78 | | { |
| | 0 | 79 | | return BadRequest("Ozon client credentials are missing."); |
| | | 80 | | } |
| | | 81 | | |
| | 0 | 82 | | var ozonProductId = order.ProductInfo?.OzonProductId; |
| | 0 | 83 | | if (string.IsNullOrWhiteSpace(ozonProductId)) |
| | | 84 | | { |
| | 0 | 85 | | return BadRequest("Order does not have `ProductInfo.OzonProductId`."); |
| | | 86 | | } |
| | | 87 | | |
| | 0 | 88 | | _ozonApiDataManager.SetClient(clientId, apiKey); |
| | | 89 | | |
| | 0 | 90 | | var normalizedLastId = lastId ?? ""; |
| | 0 | 91 | | var cacheKey = string.Join("_", |
| | 0 | 92 | | "OzonProductAttributes", |
| | 0 | 93 | | ozonClient.Id, |
| | 0 | 94 | | ozonProductId, |
| | 0 | 95 | | normalizedLastId, |
| | 0 | 96 | | limit, |
| | 0 | 97 | | sortBy, |
| | 0 | 98 | | sortDir); |
| | | 99 | | |
| | | 100 | | try |
| | | 101 | | { |
| | 0 | 102 | | var response = await _cache.GetOrCreateAsync( |
| | 0 | 103 | | cacheKey, |
| | 0 | 104 | | async entry => |
| | 0 | 105 | | { |
| | 0 | 106 | | entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(1); |
| | 0 | 107 | | |
| | 0 | 108 | | return await _ozonApiDataManager.GetProductAttributesAsync( |
| | 0 | 109 | | productIds: [ozonProductId], |
| | 0 | 110 | | lastId: normalizedLastId, |
| | 0 | 111 | | limit: limit, |
| | 0 | 112 | | sortBy: sortBy, |
| | 0 | 113 | | sortDir: sortDir) |
| | 0 | 114 | | .ConfigureAwait(false); |
| | 0 | 115 | | }) |
| | 0 | 116 | | .ConfigureAwait(false); |
| | | 117 | | |
| | 0 | 118 | | return Ok(response); |
| | | 119 | | } |
| | 0 | 120 | | catch (Exception ex) |
| | | 121 | | { |
| | 0 | 122 | | return StatusCode(502, new { error = "Ошибка при запросе к Ozon API", details = ex.Message }); |
| | | 123 | | } |
| | 0 | 124 | | } |
| | | 125 | | } |
| | | 126 | | |