From 8ac323447347e31875e1db53d899d13a4ac51c1d Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Wed, 21 Dec 2022 08:02:42 +0300 Subject: Implement fast product search by sku --- src/Services/RhDatabaseClient.cs | 43 ++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/Services/RhDatabaseClient.cs b/src/Services/RhDatabaseClient.cs index 1776731..8ce88f1 100644 --- a/src/Services/RhDatabaseClient.cs +++ b/src/Services/RhDatabaseClient.cs @@ -1,6 +1,7 @@ using Newtonsoft.Json; using RhSolutions.AddIn; using RhSolutions.Models; +using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; @@ -14,22 +15,48 @@ namespace RhSolutions.Services public static async Task GetProduct(string line) { - string request = @"https://rh.cebotari.ru/api/search?query=" + line; + string request = string.Empty; - string response = await httpClient.GetStringAsync(request); + if (line.IsRehauSku()) + { + request = @"https://rh.cebotari.ru/api/products/" + line; + } - var products = JsonConvert.DeserializeObject>(response); + else + { + request = @"https://rh.cebotari.ru/api/search?query=" + line; + } - var product = products.FirstOrDefault(); + var response = await httpClient.GetAsync(request); - if (product == null) + try { - return null; + response.EnsureSuccessStatusCode(); + string json = await response.Content.ReadAsStringAsync(); + var product = JsonConvert.DeserializeObject>(json) + .FirstOrDefault(); + + if (product == null) + { + return null; + } + else + { + if (line.IsRehauSku()) + { + return product.Name; + } + else + { + return $"{product.ProductSku} {product.Name}"; + } + } } - else + catch { - return $"{product.ProductSku} {product.Name}"; + return $"Ошибка сервера {response.StatusCode}"; } + } } } \ No newline at end of file -- cgit v1.2.3