1
0
This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
RhSolutions.SkuParser/RhSolutions.SkuParser.Api/Controllers/ProductsController.cs

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 async Task<IActionResult> PostFiles()
{
IFormFileCollection files = Request.Form.Files;
try
{
foreach (var file in files)
{
ISkuParser parser = _provider.GetRequiredKeyedService<ISkuParser>(file.ContentType);
IEnumerable<ProductQuantity> productQuantities = await 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 }));
}
}