From 2a0430b7ecaf658d37eab4bc16a61f0930a17413 Mon Sep 17 00:00:00 2001 From: Serghei Cebotari Date: Fri, 6 Oct 2023 15:06:42 +0300 Subject: [PATCH] Fix Unsuccess Status Code return --- RhSolutions.AddIn/Services/DatabaseClient.cs | 29 ++++++++++++-------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/RhSolutions.AddIn/Services/DatabaseClient.cs b/RhSolutions.AddIn/Services/DatabaseClient.cs index 28caa09..ac4dfa5 100644 --- a/RhSolutions.AddIn/Services/DatabaseClient.cs +++ b/RhSolutions.AddIn/Services/DatabaseClient.cs @@ -20,15 +20,12 @@ public class DatabaseClient : IDatabaseClient public async Task> GetProducts(string line) { - string request; - IEnumerable products; - if (ProductSku.TryParse(line, out var skus)) { ProductSku sku = skus.FirstOrDefault(); - request = @"https://rh.cebotari.ru/api/products/" + sku.ToString(); + string request = @"https://rh.cebotari.ru/api/products/" + sku.ToString(); - if (!_memoryCache.TryGetValue(sku, out products)) + if (!_memoryCache.TryGetValue(sku, out IEnumerable products)) { var response = await _httpClient.GetAsync(request); @@ -41,19 +38,25 @@ public class DatabaseClient : IDatabaseClient catch { StatusCode = response.StatusCode; + return Enumerable.Empty(); } var cacheEntryOptions = new MemoryCacheEntryOptions() .SetSlidingExpiration(TimeSpan.FromHours(1)); _memoryCache.Set(sku, products, cacheEntryOptions); + return products; + } + else + { + return products; } } else { - request = @"https://rh.cebotari.ru/api/search?query=" + line; + string request = @"https://rh.cebotari.ru/api/search?query=" + line; - if (!_memoryCache.TryGetValue(line, out products)) + if (!_memoryCache.TryGetValue(line, out IEnumerable products)) { var response = await _httpClient.GetAsync(request); @@ -66,18 +69,22 @@ public class DatabaseClient : IDatabaseClient catch { StatusCode = response.StatusCode; + return Enumerable.Empty(); } var cacheEntryOptions = new MemoryCacheEntryOptions() .SetSlidingExpiration(TimeSpan.FromHours(1)); _memoryCache.Set(line, products, cacheEntryOptions); - if (products.Count() > 0) + if (products.Any()) { _memoryCache.Set(products.First(), products, cacheEntryOptions); } + return products; } - } - - return products; + else + { + return products; + } + } } } \ No newline at end of file