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;
|
2024-02-07 16:32:34 +03:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2022-12-14 09:53:10 +03:00
|
|
|
|
|
|
|
namespace RhSolutions.Api.Controllers
|
|
|
|
{
|
2024-01-14 23:03:52 +03:00
|
|
|
[Route("api/[controller]")]
|
|
|
|
public class ProductsController : ControllerBase
|
|
|
|
{
|
|
|
|
private RhSolutionsContext dbContext;
|
|
|
|
private IPricelistParser parser;
|
2022-12-14 09:53:10 +03:00
|
|
|
|
2024-01-14 23:03:52 +03:00
|
|
|
public ProductsController(RhSolutionsContext dbContext, IPricelistParser parser)
|
|
|
|
{
|
|
|
|
this.dbContext = dbContext;
|
|
|
|
this.parser = parser;
|
|
|
|
}
|
2024-08-08 16:38:23 +03:00
|
|
|
|
2024-01-14 23:03:52 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Возвращает все продукты в базе данных
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
|
|
|
[HttpGet]
|
|
|
|
public IAsyncEnumerable<Product> GetProducts()
|
|
|
|
{
|
|
|
|
return dbContext.Products
|
|
|
|
.AsAsyncEnumerable();
|
|
|
|
}
|
2022-12-14 09:53:10 +03:00
|
|
|
|
2024-01-14 23:03:52 +03:00
|
|
|
/// <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));
|
|
|
|
}
|
2022-12-14 09:53:10 +03:00
|
|
|
|
2024-01-14 23:03:52 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Загрузка прайс-листа в формате xlsx в базу данных
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
|
|
|
[HttpPost]
|
2024-02-07 16:32:34 +03:00
|
|
|
[Authorize(AuthenticationSchemes = "Identity.Application, Bearer")]
|
2024-01-14 23:03:52 +03:00
|
|
|
public IActionResult PostProductsFromXls()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2024-08-08 16:38:23 +03:00
|
|
|
var file = Request.Form.Files.FirstOrDefault();
|
|
|
|
var products = parser.GetProducts(file)
|
|
|
|
.GroupBy(p => p.ProductSku)
|
2024-01-14 23:03:52 +03:00
|
|
|
.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
|
|
|
|
});
|
2022-12-14 09:53:10 +03:00
|
|
|
|
2024-08-08 16:38:23 +03:00
|
|
|
dbContext.AddRange(products);
|
2024-01-14 23:03:52 +03:00
|
|
|
dbContext.SaveChanges();
|
|
|
|
return Ok();
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
return BadRequest(ex.Message);
|
|
|
|
}
|
|
|
|
}
|
2022-12-14 09:53:10 +03:00
|
|
|
|
2024-01-14 23:03:52 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Удаление всех продуктов из базы данных
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
|
|
|
[HttpDelete]
|
2024-02-07 16:32:34 +03:00
|
|
|
[Authorize(AuthenticationSchemes = "Identity.Application, Bearer")]
|
2024-01-14 23:03:52 +03:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
2022-12-14 09:53:10 +03:00
|
|
|
}
|