Edit DatabaseClient

This commit is contained in:
Sergey Chebotar 2023-03-27 15:52:28 +03:00
parent 19007ff43e
commit 9846a3c35e
4 changed files with 58 additions and 49 deletions

View File

@ -0,0 +1,13 @@
using ExcelDna.Integration;
namespace RhSolutions.AddIn
{
public static class ResetBarFunction
{
[ExcelFunction]
public static void _ResetStatusBar()
{
RhSolutionsAddIn.Excel.StatusBar = false;
}
}
}

View File

@ -1,58 +1,57 @@
using ExcelDna.Integration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Office.Interop.Excel;
using RhSolutions.Models;
using RhSolutions.Services;
using System.Collections.Generic;
using System.Linq;
namespace RhSolutions.AddIn
{
public class Functions
public class RhSolutionsFunction
{
[ExcelFunction(Description = "Распознать артикул и попробовать найти его в прайс-листе")]
public static object RHSOLUTIONS([ExcelArgument(Name = "\"Строка с названием материала\"")] string line)
{
object result;
IDatabaseClient databaseClient = RhSolutionsAddIn.ServiceProvider.GetService<IDatabaseClient>();
result = ExcelAsyncUtil.Run("Database request", line, delegate
IEnumerable<Product> requestResult = ExcelAsyncUtil.Run("Database request", line, delegate
{
return databaseClient.GetProduct(line).GetAwaiter().GetResult();
});
return databaseClient.GetProducts(line)
.GetAwaiter()
.GetResult();
}) as IEnumerable<Product>;
string parsedSku = Sku.TryParse(line, out var skus)
? skus.First().ToString() : string.Empty;
Sku.TryParse(line, out var skus);
if (result == null)
if (requestResult == null)
{
if (string.IsNullOrEmpty(parsedSku))
if (skus.Count() > 0)
{
return ExcelError.ExcelErrorNA;
return $"{skus.First()} ...";
}
else
{
return skus.First().ToString();
}
}
if (result.Equals(ExcelError.ExcelErrorNA))
{
if (string.IsNullOrEmpty(parsedSku))
{
return "Загрузка...";
}
}
else
{
return skus.First().ToString();
}
}
return result;
}
[ExcelFunction]
public static void _ResetStatusBar()
if (requestResult.Count() == 0 && skus.Count() == 0)
{
RhSolutionsAddIn.Excel.StatusBar = false;
return ExcelError.ExcelErrorNA;
}
else if (requestResult.Count() == 0)
{
return $"{skus.First()}";
}
else
{
var firstProduct = requestResult.First();
return $"{firstProduct.ProductSku} {firstProduct.Name}";
}
}
}
}
}

View File

@ -1,9 +1,11 @@
using System.Threading.Tasks;
using RhSolutions.Models;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace RhSolutions.Services
{
public interface IDatabaseClient
{
public Task<object> GetProduct(string line);
public Task<IEnumerable<Product>> GetProducts(string query);
}
}

View File

@ -1,10 +1,12 @@
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using RhSolutions;
using RhSolutions.AddIn;
using RhSolutions.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
@ -12,14 +14,15 @@ namespace RhSolutions.Services
{
public class RhDatabaseClient : IDatabaseClient
{
private IServiceProvider provider;
private IServiceProvider serviceProvider;
public HttpStatusCode StatusCode { get; private set; }
public RhDatabaseClient(IServiceProvider provider)
{
this.provider = provider;
this.serviceProvider = provider;
}
public async Task<object> GetProduct(string line)
public async Task<IEnumerable<Product>> GetProducts(string line)
{
string request;
@ -33,30 +36,22 @@ namespace RhSolutions.Services
request = @"https://rh.cebotari.ru/api/search?query=" + line;
}
var client = provider.GetRequiredService<HttpClient>();
var client = serviceProvider.GetRequiredService<HttpClient>();
var response = await client.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
{
return $"{product.ProductSku} {product.Name}";
}
return JsonConvert.DeserializeObject<IEnumerable<Product>>(json) ?? Enumerable.Empty<Product>();
}
catch
{
return $"Ошибка сервера {response.StatusCode}";
}
StatusCode = response.StatusCode;
}
return Enumerable.Empty<Product>();
}
}
}