101 lines
2.6 KiB
C#
101 lines
2.6 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using RhSolutions.Models;
|
|
using RhSolutions.Api.Services;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Возвращает все продукты в базе данных
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
public IAsyncEnumerable<Product> GetProducts()
|
|
{
|
|
return dbContext.Products
|
|
.AsAsyncEnumerable();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Возвращает продукт по номеру артикула
|
|
/// </summary>
|
|
/// <param name="id">Номер артикула</param>
|
|
/// <returns></returns>
|
|
[HttpGet("{id}")]
|
|
public IEnumerable<Product> GetProduct(string id)
|
|
{
|
|
return dbContext.Products
|
|
.Where(p => p.Id.Equals(id));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Загрузка прайс-листа в формате xlsx в базу данных
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
[Authorize(AuthenticationSchemes = "Identity.Application, Bearer")]
|
|
public IActionResult PostProductsFromXls()
|
|
{
|
|
try
|
|
{
|
|
var products = parser.GetProducts(HttpContext).GroupBy(p => p.ProductSku)
|
|
.Select(g => new Product(g.Key)
|
|
{
|
|
Name = g.First().Name,
|
|
DeprecatedSkus = g.SelectMany(p => p.DeprecatedSkus).Distinct().ToList(),
|
|
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);
|
|
}
|
|
|
|
dbContext.SaveChanges();
|
|
return Ok();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return BadRequest(ex.Message);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Удаление всех продуктов из базы данных
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpDelete]
|
|
[Authorize(AuthenticationSchemes = "Identity.Application, Bearer")]
|
|
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");
|
|
}
|
|
}
|
|
} |