Send files in Form
This commit is contained in:
parent
29d7096786
commit
0276a77b32
@ -50,7 +50,9 @@ namespace RhSolutions.Api.Controllers
|
|||||||
{
|
{
|
||||||
try
|
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)
|
.Select(g => new Product(g.Key)
|
||||||
{
|
{
|
||||||
Name = g.First().Name,
|
Name = g.First().Name,
|
||||||
@ -62,11 +64,7 @@ namespace RhSolutions.Api.Controllers
|
|||||||
Price = g.First().Price
|
Price = g.First().Price
|
||||||
});
|
});
|
||||||
|
|
||||||
foreach (var p in products)
|
dbContext.AddRange(products);
|
||||||
{
|
|
||||||
dbContext.Add<Product>(p);
|
|
||||||
}
|
|
||||||
|
|
||||||
dbContext.SaveChanges();
|
dbContext.SaveChanges();
|
||||||
return Ok();
|
return Ok();
|
||||||
}
|
}
|
||||||
|
@ -5,19 +5,13 @@ namespace RhSolutions.Api.Services
|
|||||||
{
|
{
|
||||||
public class ClosedXMLParser : IPricelistParser
|
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)
|
yield break;
|
||||||
{
|
|
||||||
return new List<Product>();
|
|
||||||
}
|
}
|
||||||
|
using XLWorkbook wb = new XLWorkbook(file.OpenReadStream());
|
||||||
context.Request.Body.CopyToAsync(memoryStream).GetAwaiter().GetResult();
|
|
||||||
List<Product> products = new();
|
|
||||||
using (var wb = new XLWorkbook(memoryStream))
|
|
||||||
{
|
|
||||||
var table = GetTable(wb);
|
var table = GetTable(wb);
|
||||||
var rows = table.DataRange.Rows();
|
var rows = table.DataRange.Rows();
|
||||||
foreach (var row in rows)
|
foreach (var row in rows)
|
||||||
@ -25,13 +19,10 @@ namespace RhSolutions.Api.Services
|
|||||||
if (ProductSku.TryParse(row.Field("Актуальный материал")
|
if (ProductSku.TryParse(row.Field("Актуальный материал")
|
||||||
.GetString(), out _))
|
.GetString(), out _))
|
||||||
{
|
{
|
||||||
products.Add(ParseRow(row));
|
yield return ParseRow(row);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return products;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
private IXLTable GetTable(XLWorkbook wb)
|
private IXLTable GetTable(XLWorkbook wb)
|
||||||
{
|
{
|
||||||
var ws = wb.Worksheets.First();
|
var ws = wb.Worksheets.First();
|
||||||
|
@ -4,6 +4,6 @@ namespace RhSolutions.Api.Services
|
|||||||
{
|
{
|
||||||
public interface IPricelistParser
|
public interface IPricelistParser
|
||||||
{
|
{
|
||||||
public List<Product> GetProducts(HttpContext context);
|
public IEnumerable<Product> GetProducts(IFormFile? file);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -7,6 +7,7 @@ public class RautitanPipesTests : ProductParsersTests
|
|||||||
[TestCase("Труба flex 16", "Труба РЕХАУ FLEX 16x2,2")]
|
[TestCase("Труба flex 16", "Труба РЕХАУ FLEX 16x2,2")]
|
||||||
[TestCase("Унив.труба RAUTITAN flex 32x4,4, прям.отрезки 6м", "Труба РЕХАУ FLEX 32x4,4")]
|
[TestCase("Унив.труба RAUTITAN flex 32x4,4, прям.отрезки 6м", "Труба РЕХАУ FLEX 32x4,4")]
|
||||||
[TestCase("Труба flex 32", "Труба РЕХАУ 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)
|
public void FlexPipeTest(string query, string modified)
|
||||||
=> Invoke(productType: "Flex", query, modified);
|
=> Invoke(productType: "Flex", query, modified);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user