48 lines
1.2 KiB
C#
48 lines
1.2 KiB
C#
|
using Microsoft.AspNetCore.Mvc;
|
||
|
using RhSolutions.SkuParser.Models;
|
||
|
using RhSolutions.SkuParser.Services;
|
||
|
|
||
|
namespace RhSolutions.SkuParser.Controllers;
|
||
|
|
||
|
[ApiController]
|
||
|
[Route("/api/[controller]")]
|
||
|
public class ProductsController : ControllerBase
|
||
|
{
|
||
|
private IServiceProvider _provider;
|
||
|
private Dictionary<Product, double> _result;
|
||
|
public ProductsController(IServiceProvider provider)
|
||
|
{
|
||
|
_provider = provider;
|
||
|
_result = new();
|
||
|
}
|
||
|
|
||
|
[HttpPost]
|
||
|
public IActionResult PostFiles()
|
||
|
{
|
||
|
IFormFileCollection files = Request.Form.Files;
|
||
|
try
|
||
|
{
|
||
|
foreach (var file in files)
|
||
|
{
|
||
|
ISkuParser parser = _provider.GetRequiredKeyedService<ISkuParser>(file.ContentType);
|
||
|
IEnumerable<ProductQuantity> productQuantities = parser.ParseProducts(file);
|
||
|
foreach (ProductQuantity pq in productQuantities)
|
||
|
{
|
||
|
if (_result.ContainsKey(pq.Product))
|
||
|
{
|
||
|
_result[pq.Product] += pq.Quantity;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
_result.Add(pq.Product, pq.Quantity);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
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 }));
|
||
|
}
|
||
|
}
|