From be127319e27d630ce14c793fc50bccd576e5fb7b Mon Sep 17 00:00:00 2001 From: Serghei Cebotari Date: Wed, 15 Jan 2025 15:06:04 +0300 Subject: Implement BS parser --- .../Controllers/CommonParseController.cs | 48 ------------------ .../Controllers/ParseController.cs | 57 ++++++++++++++++++++++ 2 files changed, 57 insertions(+), 48 deletions(-) delete mode 100644 RhSolutions.SkuParser.Api/Controllers/CommonParseController.cs create mode 100644 RhSolutions.SkuParser.Api/Controllers/ParseController.cs (limited to 'RhSolutions.SkuParser.Api/Controllers') diff --git a/RhSolutions.SkuParser.Api/Controllers/CommonParseController.cs b/RhSolutions.SkuParser.Api/Controllers/CommonParseController.cs deleted file mode 100644 index c819851..0000000 --- a/RhSolutions.SkuParser.Api/Controllers/CommonParseController.cs +++ /dev/null @@ -1,48 +0,0 @@ -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/ParseController.cs b/RhSolutions.SkuParser.Api/Controllers/ParseController.cs new file mode 100644 index 0000000..ca1d0d4 --- /dev/null +++ b/RhSolutions.SkuParser.Api/Controllers/ParseController.cs @@ -0,0 +1,57 @@ +using Microsoft.AspNetCore.Mvc; +using RhSolutions.SkuParser.Models; +using RhSolutions.SkuParser.Abstractions; + +namespace RhSolutions.SkuParser.Controllers; + +[ApiController] +[Route("/api/[controller]")] +public class ParseController : ControllerBase +{ + private IServiceProvider _provider; + private Dictionary _result; + public ParseController(IServiceProvider provider) + { + _provider = provider; + _result = new(); + } + + [HttpPost] + public IActionResult PostFiles([FromQuery] bool bs = false) + { + IFormFileCollection files = Request.Form.Files; + try + { + foreach (var file in files) + { + ISkuParser parser = bs ? _provider.GetRequiredKeyedService("BS") + : _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.Key.ProductLine, + x.Key.Name, + x.Key.Price, + quantity = x.Value + })); + } +} \ No newline at end of file -- cgit v1.2.3