0
0
RhSolutions-Api/RhSolutions.Api/Controllers/ProductsController.cs

82 lines
2.4 KiB
C#
Raw Normal View History

2022-12-14 09:53:10 +03:00
using Microsoft.AspNetCore.Mvc;
2023-05-11 07:55:26 +03:00
using RhSolutions.Models;
2022-12-14 09:53:10 +03:00
using RhSolutions.Api.Services;
2023-05-11 07:55:26 +03:00
using System.Linq;
2022-12-14 09:53:10 +03:00
namespace RhSolutions.Api.Controllers
{
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
private RhSolutionsContext dbContext;
private IPricelistParser parser;
public ProductsController(RhSolutionsContext dbContext, IPricelistParser parser)
{
this.dbContext = dbContext;
this.parser = parser;
}
[HttpGet]
public IAsyncEnumerable<Product> GetProducts()
{
return dbContext.Products
.AsAsyncEnumerable();
}
[HttpGet("{id}")]
public IEnumerable<Product> GetProduct(string id)
{
return dbContext.Products
2023-05-12 07:45:27 +03:00
.Where(p => p.Id.Equals(id));
2022-12-14 09:53:10 +03:00
}
[HttpPost]
2023-05-11 07:55:26 +03:00
public IActionResult PostProductsFromXls()
2022-12-14 09:53:10 +03:00
{
try
{
2023-05-11 07:55:26 +03:00
var products = parser.GetProducts(HttpContext).GroupBy(p => p.ProductSku)
.Select(g => new Product(g.Key)
2022-12-14 09:53:10 +03:00
{
2023-05-11 07:55:26 +03:00
Name = g.First().Name,
DeprecatedSkus = g.SelectMany(p => p.DeprecatedSkus).Distinct().ToList(),
2023-05-11 07:55:26 +03:00
ProductLines = g.SelectMany(p => p.ProductLines).Distinct().ToList(),
IsOnWarehouse = g.Any(p => p.IsOnWarehouse == true),
ProductMeasure = g.First().ProductMeasure,
DeliveryMakeUp = g.First().DeliveryMakeUp,
Price = g.First().Price
});
foreach (var p in products)
{
dbContext.Add<Product>(p);
2022-12-14 09:53:10 +03:00
}
dbContext.SaveChanges();
2022-12-18 11:58:29 +03:00
return Ok();
2022-12-14 09:53:10 +03:00
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
[HttpDelete]
public IActionResult DeleteAllProducts()
{
List<Product> deleted = new();
if (dbContext.Products.Count() > 0)
{
foreach (Product p in dbContext.Products)
{
deleted.Add(p);
dbContext.Remove(p);
}
dbContext.SaveChanges();
return Ok(deleted);
}
else return Ok("Empty db");
}
}
}