From a542bfb7f4ceee8b49cf8fcadf64ffb72cc97da5 Mon Sep 17 00:00:00 2001 From: Serghei Cebotari Date: Tue, 14 Jan 2025 14:01:01 +0000 Subject: Common Parsing Method --- .../Controllers/CommonParseController.cs | 48 ++++++++++++++++++++++ .../Controllers/ProductsController.cs | 48 ---------------------- 2 files changed, 48 insertions(+), 48 deletions(-) create mode 100644 RhSolutions.SkuParser.Api/Controllers/CommonParseController.cs delete mode 100644 RhSolutions.SkuParser.Api/Controllers/ProductsController.cs (limited to 'RhSolutions.SkuParser.Api/Controllers') diff --git a/RhSolutions.SkuParser.Api/Controllers/CommonParseController.cs b/RhSolutions.SkuParser.Api/Controllers/CommonParseController.cs new file mode 100644 index 0000000..c819851 --- /dev/null +++ b/RhSolutions.SkuParser.Api/Controllers/CommonParseController.cs @@ -0,0 +1,48 @@ +using Microsoft.AspNetCore.Mvc; +using RhSolutions.SkuParser.Models; +using RhSolutions.SkuParser.Abstractions; + +namespace RhSolutions.SkuParser.Controllers; + +[ApiController] +[Route("/api/[controller]")] +public class CommonParseController : ControllerBase +{ + private IServiceProvider _provider; + private Dictionary _result; + public CommonParseController(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(file.ContentType); + var dict = parser.ParseProducts(file); + foreach (var kvp in dict) + { + if (_result.ContainsKey(kvp.Key)) + { + _result[kvp.Key] += kvp.Value; + } + else + { + _result.Add(kvp.Key, kvp.Value); + } + } + } + } + catch (Exception ex) + { + return BadRequest(error: $"{ex.Message}\n\n{ex.Source}\n{ex.StackTrace}"); + } + return new JsonResult(_result.Select(x => new { x.Key.Sku, x.Value })); + } +} \ No newline at end of file diff --git a/RhSolutions.SkuParser.Api/Controllers/ProductsController.cs b/RhSolutions.SkuParser.Api/Controllers/ProductsController.cs deleted file mode 100644 index 77b277b..0000000 --- a/RhSolutions.SkuParser.Api/Controllers/ProductsController.cs +++ /dev/null @@ -1,48 +0,0 @@ -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 IActionResult PostFiles() - { - IFormFileCollection files = Request.Form.Files; - try - { - foreach (var file in files) - { - ISkuParser parser = _provider.GetRequiredKeyedService(file.ContentType); - IEnumerable 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 })); - } -} \ No newline at end of file -- cgit v1.2.3