73 lines
1.9 KiB
C#
73 lines
1.9 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using RhSolutions.Api.Models;
|
|
using RhSolutions.Api.Services;
|
|
|
|
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
|
|
.Where(p => p.ProductSku!.Equals(id));
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<IActionResult> PostProductsFromXls()
|
|
{
|
|
try
|
|
{
|
|
var products = parser.GetProducts(HttpContext);
|
|
await foreach (var p in products)
|
|
{
|
|
using (p)
|
|
{
|
|
dbContext.Add<Product>(p);
|
|
}
|
|
}
|
|
|
|
dbContext.SaveChanges();
|
|
return Ok(products);
|
|
}
|
|
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");
|
|
}
|
|
}
|
|
} |