0
0

Send files in Form

This commit is contained in:
Serghei Cebotari 2024-08-08 16:38:23 +03:00
parent 29d7096786
commit 0276a77b32
4 changed files with 19 additions and 29 deletions

View File

@ -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<Product>(p);
}
dbContext.AddRange(products);
dbContext.SaveChanges();
return Ok();
}

View File

@ -5,19 +5,13 @@ namespace RhSolutions.Api.Services
{
public class ClosedXMLParser : IPricelistParser
{
public List<Product> GetProducts(HttpContext context)
public IEnumerable<Product> GetProducts(IFormFile? file)
{
using (var memoryStream = new MemoryStream())
if (file == null)
{
if (context == null)
{
return new List<Product>();
yield break;
}
context.Request.Body.CopyToAsync(memoryStream).GetAwaiter().GetResult();
List<Product> products = new();
using (var wb = new XLWorkbook(memoryStream))
{
using XLWorkbook wb = new XLWorkbook(file.OpenReadStream());
var table = GetTable(wb);
var rows = table.DataRange.Rows();
foreach (var row in rows)
@ -25,13 +19,10 @@ namespace RhSolutions.Api.Services
if (ProductSku.TryParse(row.Field("Актуальный материал")
.GetString(), out _))
{
products.Add(ParseRow(row));
yield return ParseRow(row);
}
}
}
return products;
}
}
private IXLTable GetTable(XLWorkbook wb)
{
var ws = wb.Worksheets.First();

View File

@ -4,6 +4,6 @@ namespace RhSolutions.Api.Services
{
public interface IPricelistParser
{
public List<Product> GetProducts(HttpContext context);
public IEnumerable<Product> GetProducts(IFormFile? file);
}
}

View File

@ -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);