aboutsummaryrefslogtreecommitdiff
path: root/RhSolutions.SkuParser.Api/Controllers/ProductsController.cs
diff options
context:
space:
mode:
authorSerghei Cebotari <serghei@cebotari.ru>2024-07-21 16:25:59 +0300
committerSerghei Cebotari <serghei@cebotari.ru>2024-07-21 16:25:59 +0300
commit6fe0a5e92b071411a0408d59d54d0de78e55d75c (patch)
tree3100d0991b67c17434d131e509eefc85f6a71ff8 /RhSolutions.SkuParser.Api/Controllers/ProductsController.cs
parente0313b83a033040660f0669de6d9e77042e87026 (diff)
Squashed commit of the following:
commit 688c5426e8793b808b9c75c9a19733af0a402fcb Author: Serghei Cebotari <serghei@cebotari.ru> Date: Sun Jul 21 16:25:14 2024 +0300 Switch to port 8080 commit c39249f6528ec76686a9382d1dc375c07d1d5044 Author: Serghei Cebotari <serghei@cebotari.ru> Date: Sun Jul 21 16:24:59 2024 +0300 Switch to alpine image commit 5318d7ec3f4f3d205549cf6732fa5b066a1d0a36 Author: Serghei Cebotari <serghei@cebotari.ru> Date: Sun Jul 21 15:40:14 2024 +0300 Add docker commit b6cd60a973da26bc92cf1fb45b4d2396b7ce56ea Author: Serghei Cebotari <serghei@cebotari.ru> Date: Sun Jul 21 15:00:12 2024 +0300 Delete asynchrony commit 44a194e6d27312f3b8dd0b9c9c02d873e06e0b22 Author: Serghei Cebotari <serghei@cebotari.ru> Date: Sun Jul 21 14:59:29 2024 +0300 Add Equals and GetHasCode methods overrides to ProductQuantity class commit a274eadd313e12f11cc84d32e5030bbc5b187f8c Author: Serghei Cebotari <serghei@cebotari.ru> Date: Sun Jul 21 14:58:37 2024 +0300 Add parsers tests commit 4f969e70d9716d8ddb4f4efedd466846289d7e2b Author: Serghei Cebotari <serghei@cebotari.ru> Date: Sun Jul 21 14:57:55 2024 +0300 Update product tests commit 2485e20d0e93bed562f929055b6867dc2574a95b Author: Serghei Cebotari <serghei@cebotari.ru> Date: Sat Jul 20 19:34:19 2024 +0300 Implement Excel parser commit 30f2e28c87a4d961c1f1fc48fbd72334905bf4ed Author: Serghei Cebotari <serghei@cebotari.ru> Date: Sat Jul 20 16:58:35 2024 +0300 Implement csv parser commit 08e86b43c0829de341dc3d24fbe01aadbed2e173 Author: Serghei Cebotari <serghei@cebotari.ru> Date: Thu Jul 18 21:01:28 2024 +0300 Edit port number
Diffstat (limited to 'RhSolutions.SkuParser.Api/Controllers/ProductsController.cs')
-rw-r--r--RhSolutions.SkuParser.Api/Controllers/ProductsController.cs48
1 files changed, 48 insertions, 0 deletions
diff --git a/RhSolutions.SkuParser.Api/Controllers/ProductsController.cs b/RhSolutions.SkuParser.Api/Controllers/ProductsController.cs
new file mode 100644
index 0000000..d85b01b
--- /dev/null
+++ b/RhSolutions.SkuParser.Api/Controllers/ProductsController.cs
@@ -0,0 +1,48 @@
+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 IActionResult PostFiles()
+ {
+ IFormFileCollection files = Request.Form.Files;
+ try
+ {
+ foreach (var file in files)
+ {
+ ISkuParser parser = _provider.GetRequiredKeyedService<ISkuParser>(file.ContentType);
+ IEnumerable<ProductQuantity> 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