2025-01-14 05:59:55 +00:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using RhSolutions.SkuParser.Models;
|
2025-01-14 14:01:01 +00:00
|
|
|
using RhSolutions.SkuParser.Abstractions;
|
2025-01-14 05:59:55 +00:00
|
|
|
|
|
|
|
namespace RhSolutions.SkuParser.Controllers;
|
|
|
|
|
|
|
|
[ApiController]
|
|
|
|
[Route("/api/[controller]")]
|
2025-01-14 14:01:01 +00:00
|
|
|
public class CommonParseController : ControllerBase
|
2025-01-14 05:59:55 +00:00
|
|
|
{
|
|
|
|
private IServiceProvider _provider;
|
|
|
|
private Dictionary<Product, double> _result;
|
2025-01-14 14:01:01 +00:00
|
|
|
public CommonParseController(IServiceProvider provider)
|
2025-01-14 05:59:55 +00:00
|
|
|
{
|
|
|
|
_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);
|
2025-01-14 14:01:01 +00:00
|
|
|
var dict = parser.ParseProducts(file);
|
|
|
|
foreach (var kvp in dict)
|
2025-01-14 05:59:55 +00:00
|
|
|
{
|
2025-01-14 14:01:01 +00:00
|
|
|
if (_result.ContainsKey(kvp.Key))
|
2025-01-14 05:59:55 +00:00
|
|
|
{
|
2025-01-14 14:01:01 +00:00
|
|
|
_result[kvp.Key] += kvp.Value;
|
2025-01-14 05:59:55 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2025-01-14 14:01:01 +00:00
|
|
|
_result.Add(kvp.Key, kvp.Value);
|
2025-01-14 05:59:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
return BadRequest(error: $"{ex.Message}\n\n{ex.Source}\n{ex.StackTrace}");
|
|
|
|
}
|
2025-01-14 14:01:01 +00:00
|
|
|
return new JsonResult(_result.Select(x => new { x.Key.Sku, x.Value }));
|
2025-01-14 05:59:55 +00:00
|
|
|
}
|
2024-07-21 16:25:59 +03:00
|
|
|
}
|