diff --git a/RhSolutions.Api/Controllers/ProductsController.cs b/RhSolutions.Api/Controllers/ProductsController.cs index b2aa43e..6259bb5 100644 --- a/RhSolutions.Api/Controllers/ProductsController.cs +++ b/RhSolutions.Api/Controllers/ProductsController.cs @@ -16,7 +16,7 @@ namespace RhSolutions.Api.Controllers this.dbContext = dbContext; this.parser = parser; } - + /// /// Возвращает все продукты в базе данных /// @@ -50,7 +50,9 @@ namespace RhSolutions.Api.Controllers { try { - var products = parser.GetProducts(HttpContext).GroupBy(p => p.ProductSku) + var file = Request.Form.Files.FirstOrDefault(); + var products = parser.GetProducts(file) + .GroupBy(p => p.ProductSku) .Select(g => new Product(g.Key) { Name = g.First().Name, @@ -62,11 +64,7 @@ namespace RhSolutions.Api.Controllers Price = g.First().Price }); - foreach (var p in products) - { - dbContext.Add(p); - } - + dbContext.AddRange(products); dbContext.SaveChanges(); return Ok(); } diff --git a/RhSolutions.Api/Services/ClosedXMLParser.cs b/RhSolutions.Api/Services/ClosedXMLParser.cs index 3aafc90..2c614e0 100644 --- a/RhSolutions.Api/Services/ClosedXMLParser.cs +++ b/RhSolutions.Api/Services/ClosedXMLParser.cs @@ -5,31 +5,22 @@ namespace RhSolutions.Api.Services { public class ClosedXMLParser : IPricelistParser { - public List GetProducts(HttpContext context) + public IEnumerable GetProducts(IFormFile? file) { - using (var memoryStream = new MemoryStream()) + if (file == null) { - if (context == null) + yield break; + } + using XLWorkbook wb = new XLWorkbook(file.OpenReadStream()); + var table = GetTable(wb); + var rows = table.DataRange.Rows(); + foreach (var row in rows) + { + if (ProductSku.TryParse(row.Field("Актуальный материал") + .GetString(), out _)) { - return new List(); + yield return ParseRow(row); } - - context.Request.Body.CopyToAsync(memoryStream).GetAwaiter().GetResult(); - List products = new(); - using (var wb = new XLWorkbook(memoryStream)) - { - var table = GetTable(wb); - var rows = table.DataRange.Rows(); - foreach (var row in rows) - { - if (ProductSku.TryParse(row.Field("Актуальный материал") - .GetString(), out _)) - { - products.Add(ParseRow(row)); - } - } - } - return products; } } private IXLTable GetTable(XLWorkbook wb) diff --git a/RhSolutions.Api/Services/IPricelistParser.cs b/RhSolutions.Api/Services/IPricelistParser.cs index ee00f53..3edfd91 100644 --- a/RhSolutions.Api/Services/IPricelistParser.cs +++ b/RhSolutions.Api/Services/IPricelistParser.cs @@ -4,6 +4,6 @@ namespace RhSolutions.Api.Services { public interface IPricelistParser { - public List GetProducts(HttpContext context); + public IEnumerable GetProducts(IFormFile? file); } } \ No newline at end of file diff --git a/RhSolutions.Parsers.Tests/RautitanPipesTests.cs b/RhSolutions.Parsers.Tests/RautitanPipesTests.cs index 06b743f..08cfc82 100644 --- a/RhSolutions.Parsers.Tests/RautitanPipesTests.cs +++ b/RhSolutions.Parsers.Tests/RautitanPipesTests.cs @@ -7,6 +7,7 @@ public class RautitanPipesTests : ProductParsersTests [TestCase("Труба flex 16", "Труба РЕХАУ FLEX 16x2,2")] [TestCase("Унив.труба RAUTITAN flex 32x4,4, прям.отрезки 6м", "Труба РЕХАУ FLEX 32x4,4")] [TestCase("Труба flex 32", "Труба РЕХАУ FLEX 32x4,4")] + [TestCase("20 Труба PPRC SDR6 PN20 Дн32х5,4 ГОСТ 32415-2013 heisskraft м 10,0", "Труба РЕХАУ FLEX 32x4,4")] public void FlexPipeTest(string query, string modified) => Invoke(productType: "Flex", query, modified);