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 _result; public ProductsController(IServiceProvider provider) { _provider = provider; _result = new(); } [HttpPost] public async Task PostFiles() { IFormFileCollection files = Request.Form.Files; try { foreach (var file in files) { ISkuParser parser = _provider.GetRequiredKeyedService(file.ContentType); IEnumerable 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 })); } }