diff options
Diffstat (limited to 'RhSolutions.SkuParser.Api/Services')
-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 |
3 files changed, 22 insertions, 23 deletions
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);
-}
|