Implement fast product search by sku
This commit is contained in:
parent
c91f231892
commit
8ac3234473
@ -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<object> 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<IEnumerable<Product>>(response);
|
||||
else
|
||||
{
|
||||
request = @"https://rh.cebotari.ru/api/search?query=" + line;
|
||||
}
|
||||
|
||||
var product = products.FirstOrDefault();
|
||||
var response = await httpClient.GetAsync(request);
|
||||
|
||||
try
|
||||
{
|
||||
response.EnsureSuccessStatusCode();
|
||||
string json = await response.Content.ReadAsStringAsync();
|
||||
var product = JsonConvert.DeserializeObject<IEnumerable<Product>>(json)
|
||||
.FirstOrDefault();
|
||||
|
||||
if (product == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (line.IsRehauSku())
|
||||
{
|
||||
return product.Name;
|
||||
}
|
||||
else
|
||||
{
|
||||
return $"{product.ProductSku} {product.Name}";
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
return $"Ошибка сервера {response.StatusCode}";
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user