aboutsummaryrefslogtreecommitdiff
path: root/RhSolutions.SkuParser.Api/Controllers
diff options
context:
space:
mode:
Diffstat (limited to 'RhSolutions.SkuParser.Api/Controllers')
-rw-r--r--RhSolutions.SkuParser.Api/Controllers/ParseController.cs (renamed from RhSolutions.SkuParser.Api/Controllers/CommonParseController.cs)103
1 files changed, 56 insertions, 47 deletions
diff --git a/RhSolutions.SkuParser.Api/Controllers/CommonParseController.cs b/RhSolutions.SkuParser.Api/Controllers/ParseController.cs
index c819851..ca1d0d4 100644
--- a/RhSolutions.SkuParser.Api/Controllers/CommonParseController.cs
+++ b/RhSolutions.SkuParser.Api/Controllers/ParseController.cs
@@ -1,48 +1,57 @@
-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<Product, double> _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<ISkuParser>(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 }));
- }
+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<Product, double> _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<ISkuParser>("BS")
+ : _provider.GetRequiredKeyedService<ISkuParser>(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