Add RAUPRICE function. Refactoring.

This commit is contained in:
Sergey Chebotar 2021-12-03 22:25:20 +03:00
parent 915929fa9d
commit 355e2d3672
4 changed files with 69 additions and 68 deletions

View File

@ -1,10 +1,9 @@
using System;
namespace Rehau.Sku.Assist
namespace Rehau.Sku.Assist
{
interface IProduct
{
string Id { get; }
string Name { get; }
string Price { get; }
}
}

View File

@ -1,7 +1,9 @@
using AngleSharp.Dom;
using ExcelDna.Integration;
using Newtonsoft.Json;
using System;
using System.Linq;
using System.Runtime.Caching;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
@ -16,6 +18,13 @@ namespace Rehau.Sku.Assist
Series
}
public enum ProductField
{
Name,
Id,
Price
}
static class SkuAssist
{
public static async Task<IProduct> GetProduct(string request)
@ -43,10 +52,52 @@ namespace Rehau.Sku.Assist
IProduct product = storeResponse
.Ecommerce
.Impressions
.Where(i => Regex.IsMatch(i.Id, @"\d{11}", RegexOptions.None))
.Where(p => Regex.IsMatch(p.Id, @"\d{11}", RegexOptions.None))
.FirstOrDefault();
return product;
}
public static object GetProduct(string request, ProductField field)
{
IProduct product;
if (MemoryCache.Default.Contains(request))
{
product = MemoryCache.Default[request] as IProduct;
}
else
{
object result = ExcelAsyncUtil.Run("RauName", new[] { request },
delegate
{
Task<IProduct> p = Task.Run(() => GetProduct(request));
return p.Result;
});
if (result == null)
return "Не найдено";
if (result.Equals(ExcelError.ExcelErrorNA))
return "Загрузка...";
product = result as IProduct;
MemoryCache.Default.Add(request, product, DateTime.Now.AddMinutes(10));
return product.Name;
}
switch (field)
{
case ProductField.Name:
return product.Name;
case ProductField.Id:
return product.Id;
case ProductField.Price:
return product.Price;
default:
return ExcelError.ExcelErrorValue;
}
}
}
}

View File

@ -1,6 +1,4 @@
using ExcelDna.Integration;
using System.Runtime.Caching;
using System.Threading.Tasks;
namespace Rehau.Sku.Assist
{
@ -8,61 +6,14 @@ namespace Rehau.Sku.Assist
{
[ExcelFunction]
public static object RAUNAME(string request)
{
if (MemoryCache.Default.Contains(request))
{
IProduct product = MemoryCache.Default[request] as IProduct;
return product.Name;
}
else
{
object result = ExcelAsyncUtil.Run("RauName", new[] { request },
delegate
{
Task<IProduct> p = Task.Run(() => SkuAssist.GetProduct(request));
return p.Result;
});
if (result == null)
return "Не найдено";
if (result.Equals(ExcelError.ExcelErrorNA))
return "Загрузка...";
IProduct product = result as IProduct;
MemoryCache.Default.Add(request, product, System.DateTime.Now.AddMinutes(10));
return product.Name;
}
}
=> SkuAssist.GetProduct(request, ProductField.Name);
[ExcelFunction]
public static object RAUSKU(string request)
{
if (MemoryCache.Default.Contains(request))
{
IProduct product = MemoryCache.Default[request] as IProduct;
return product.Id;
}
else
{
object result = ExcelAsyncUtil.Run("RauSku", new[] { request },
delegate
{
Task<IProduct> p = Task.Run(() => SkuAssist.GetProduct(request));
return p.Result;
});
=> SkuAssist.GetProduct(request, ProductField.Id);
if (result == null)
return "Не найдено";
if (result.Equals(ExcelError.ExcelErrorNA))
return "Загрузка...";
IProduct product = result as IProduct;
MemoryCache.Default.Add(request, product, System.DateTime.Now.AddMinutes(10));
return product.Id;
}
}
[ExcelFunction]
public static object RAUPRICE(string request)
=> SkuAssist.GetProduct(request, ProductField.Price);
}
}

View File

@ -2,14 +2,14 @@
namespace Rehau.Sku.Assist.Tests
{
//[TestFixture]
//public class SkuAssistTests
//{
// [Test]
// public async void BaseTest()
// {
// var result = await Functions.RAUNAME("160001");
// Assert.AreEqual("Надвижная гильза REHAU RAUTITAN РХ (11600011001)", result);
// }
//}
[TestFixture]
public class SkuAssistTests
{
[Test]
public static void BaseTest()
{
var result = Functions.RAUNAME("160001");
Assert.AreEqual("Надвижная гильза REHAU RAUTITAN РХ (11600011001)", result);
}
}
}