diff options
Diffstat (limited to 'RhSolutions.SkuParser.Api')
-rw-r--r-- | RhSolutions.SkuParser.Api/Abstractions/ISkuParser.cs | 8 | ||||
-rw-r--r-- | RhSolutions.SkuParser.Api/Controllers/CommonParseController.cs (renamed from RhSolutions.SkuParser.Api/Controllers/ProductsController.cs) | 18 | ||||
-rw-r--r-- | RhSolutions.SkuParser.Api/Program.cs | 7 | ||||
-rw-r--r-- | RhSolutions.SkuParser.Api/Services/CommonCsvParser.cs (renamed from RhSolutions.SkuParser.Api/Services/CsvParser.cs) | 10 | ||||
-rw-r--r-- | RhSolutions.SkuParser.Api/Services/CommonExcelParser.cs (renamed from RhSolutions.SkuParser.Api/Services/ExcelParser.cs) | 28 | ||||
-rw-r--r-- | RhSolutions.SkuParser.Api/Services/ISkuParser.cs | 7 |
6 files changed, 43 insertions, 35 deletions
diff --git a/RhSolutions.SkuParser.Api/Abstractions/ISkuParser.cs b/RhSolutions.SkuParser.Api/Abstractions/ISkuParser.cs new file mode 100644 index 0000000..a1b2fbf --- /dev/null +++ b/RhSolutions.SkuParser.Api/Abstractions/ISkuParser.cs @@ -0,0 +1,8 @@ +using RhSolutions.SkuParser.Models;
+
+namespace RhSolutions.SkuParser.Abstractions;
+
+public interface ISkuParser
+{
+ public Dictionary<Product, double> ParseProducts(IFormFile file);
+}
diff --git a/RhSolutions.SkuParser.Api/Controllers/ProductsController.cs b/RhSolutions.SkuParser.Api/Controllers/CommonParseController.cs index 77b277b..c819851 100644 --- a/RhSolutions.SkuParser.Api/Controllers/ProductsController.cs +++ b/RhSolutions.SkuParser.Api/Controllers/CommonParseController.cs @@ -1,16 +1,16 @@ using Microsoft.AspNetCore.Mvc;
using RhSolutions.SkuParser.Models;
-using RhSolutions.SkuParser.Services;
+using RhSolutions.SkuParser.Abstractions;
namespace RhSolutions.SkuParser.Controllers;
[ApiController]
[Route("/api/[controller]")]
-public class ProductsController : ControllerBase
+public class CommonParseController : ControllerBase
{
private IServiceProvider _provider;
private Dictionary<Product, double> _result;
- public ProductsController(IServiceProvider provider)
+ public CommonParseController(IServiceProvider provider)
{
_provider = provider;
_result = new();
@@ -25,16 +25,16 @@ public class ProductsController : ControllerBase foreach (var file in files)
{
ISkuParser parser = _provider.GetRequiredKeyedService<ISkuParser>(file.ContentType);
- IEnumerable<ProductQuantity> productQuantities = parser.ParseProducts(file);
- foreach (ProductQuantity pq in productQuantities)
+ var dict = parser.ParseProducts(file);
+ foreach (var kvp in dict)
{
- if (_result.ContainsKey(pq.Product))
+ if (_result.ContainsKey(kvp.Key))
{
- _result[pq.Product] += pq.Quantity;
+ _result[kvp.Key] += kvp.Value;
}
else
{
- _result.Add(pq.Product, pq.Quantity);
+ _result.Add(kvp.Key, kvp.Value);
}
}
}
@@ -43,6 +43,6 @@ public class ProductsController : ControllerBase {
return BadRequest(error: $"{ex.Message}\n\n{ex.Source}\n{ex.StackTrace}");
}
- return new JsonResult(_result.Select(x => new { Sku = x.Key.ToString(), Quantity = x.Value }));
+ return new JsonResult(_result.Select(x => new { x.Key.Sku, x.Value }));
}
}
\ No newline at end of file diff --git a/RhSolutions.SkuParser.Api/Program.cs b/RhSolutions.SkuParser.Api/Program.cs index e0acec8..13e0b90 100644 --- a/RhSolutions.SkuParser.Api/Program.cs +++ b/RhSolutions.SkuParser.Api/Program.cs @@ -1,10 +1,11 @@ +using RhSolutions.SkuParser.Abstractions;
using RhSolutions.SkuParser.Services;
var builder = WebApplication.CreateBuilder(args);
builder.Services
- .AddKeyedScoped<ISkuParser, CsvParser>("text/csv")
- .AddKeyedScoped<ISkuParser, ExcelParser>("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
- .AddKeyedScoped<ISkuParser, ExcelParser>("application/vnd.ms-excel.sheet.macroenabled.12");
+ .AddKeyedScoped<ISkuParser, CommonCsvParser>("text/csv")
+ .AddKeyedScoped<ISkuParser, CommonExcelParser>("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
+ .AddKeyedScoped<ISkuParser, CommonExcelParser>("application/vnd.ms-excel.sheet.macroenabled.12");
builder.Services.AddControllers();
var app = builder.Build();
diff --git a/RhSolutions.SkuParser.Api/Services/CsvParser.cs b/RhSolutions.SkuParser.Api/Services/CommonCsvParser.cs index 2776721..e88ba25 100644 --- a/RhSolutions.SkuParser.Api/Services/CsvParser.cs +++ b/RhSolutions.SkuParser.Api/Services/CommonCsvParser.cs @@ -1,6 +1,7 @@ using System.Globalization;
using CsvHelper;
using CsvHelper.Configuration;
+using RhSolutions.SkuParser.Abstractions;
using RhSolutions.SkuParser.Models;
namespace RhSolutions.SkuParser.Services;
@@ -8,9 +9,9 @@ namespace RhSolutions.SkuParser.Services; /// <summary>
/// Парсер артикулов и их количества из файлов *.csv
/// </summary>
-public class CsvParser : ISkuParser
+public class CommonCsvParser : ISkuParser
{
- public IEnumerable<ProductQuantity> ParseProducts(IFormFile file)
+ public Dictionary<Product, double> ParseProducts(IFormFile file)
{
using StreamReader reader = new(file.OpenReadStream());
var config = new CsvConfiguration(CultureInfo.GetCultureInfo("ru-RU"))
@@ -18,7 +19,8 @@ public class CsvParser : ISkuParser HasHeaderRecord = false,
};
using CsvReader csvReader = new(reader, config);
-
- return csvReader.GetRecords<ProductQuantity>().ToList();
+
+ return csvReader.GetRecords<ProductQuantity>()
+ .ToDictionary(pq => new Product() { Sku = pq.Product.Sku }, pq => pq.Quantity);
}
}
diff --git a/RhSolutions.SkuParser.Api/Services/ExcelParser.cs b/RhSolutions.SkuParser.Api/Services/CommonExcelParser.cs index fec3885..206200d 100644 --- a/RhSolutions.SkuParser.Api/Services/ExcelParser.cs +++ b/RhSolutions.SkuParser.Api/Services/CommonExcelParser.cs @@ -1,11 +1,12 @@ using ClosedXML.Excel;
+using RhSolutions.SkuParser.Abstractions;
using RhSolutions.SkuParser.Models;
namespace RhSolutions.SkuParser.Services;
-public class ExcelParser : ISkuParser
+public class CommonExcelParser : ISkuParser
{
- public IEnumerable<ProductQuantity> ParseProducts(IFormFile file)
+ public Dictionary<Product, double> ParseProducts(IFormFile file)
{
using XLWorkbook workbook = new(file.OpenReadStream());
IXLWorksheet ws = workbook.Worksheet(1);
@@ -51,23 +52,26 @@ public class ExcelParser : ISkuParser throw new ArgumentException($"Столбец с количеством не определен: {file.FileName}");
}
- List<ProductQuantity> result = new();
+ Dictionary<Product, double> result = new();
var rows = quantityColumn.CellsUsed().Select(x => x.Address.RowNumber);
-
+
foreach (var row in rows)
{
var quantity = quantityColumn.Cell(row).Value;
var sku = skuColumn.Cell(row).Value;
-
- if (quantity.IsNumber
- && Product.TryParse(sku.ToString(), out Product? p))
+
+ if (quantity.IsNumber
+ && Product.TryParse(sku.ToString(), out Product? p)
+ && p != null)
{
- ProductQuantity pq = new()
+ if (result.ContainsKey(p))
+ {
+ result[p] += (double)quantity;
+ }
+ else
{
- Product = p!,
- Quantity = quantity.GetNumber()
- };
- result.Add(pq);
+ result.Add(p, (double)quantity);
+ }
}
}
diff --git a/RhSolutions.SkuParser.Api/Services/ISkuParser.cs b/RhSolutions.SkuParser.Api/Services/ISkuParser.cs deleted file mode 100644 index 4329135..0000000 --- a/RhSolutions.SkuParser.Api/Services/ISkuParser.cs +++ /dev/null @@ -1,7 +0,0 @@ -using RhSolutions.SkuParser.Models;
-
-namespace RhSolutions.SkuParser.Services;
-public interface ISkuParser
-{
- public IEnumerable<ProductQuantity> ParseProducts(IFormFile file);
-}
|