130 lines
3.5 KiB
C#
130 lines
3.5 KiB
C#
using ElectronicsShopContracts.BindingModels;
|
|
using ElectronicsShopContracts.BusinessLogicContracts;
|
|
using ElectronicsShopContracts.SearchModels;
|
|
using ElectronicsShopContracts.ViewModels;
|
|
using ElectronicsShopDataBaseImplement.Models;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace ElectronicsShopRestAPI.Controllers {
|
|
|
|
[Route("api/[controller]/[action]")]
|
|
[ApiController]
|
|
public class MainController : Controller {
|
|
|
|
private readonly ILogger _logger;
|
|
private readonly IProductLogic _product;
|
|
private readonly IOrderLogic _order;
|
|
|
|
|
|
public MainController(ILogger<MainController> logger, IProductLogic product,
|
|
IOrderLogic orderLogic) {
|
|
_logger = logger;
|
|
_product = product;
|
|
_order = orderLogic;
|
|
}
|
|
|
|
[HttpGet]
|
|
public List<ProductViewModel>? GetProducts() {
|
|
try {
|
|
return _product.ReadList(null);
|
|
}
|
|
catch (Exception ex) {
|
|
_logger.LogError(ex, $"Ошибка получения данных");
|
|
throw;
|
|
}
|
|
}
|
|
[HttpGet]
|
|
public ProductViewModel? GetPoduct(int productID)
|
|
{
|
|
try
|
|
{
|
|
return _product.ReadElement(new ProductSearchModel
|
|
{
|
|
ID = productID
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка получения суши по id={Id}", productID);
|
|
throw;
|
|
}
|
|
}
|
|
[HttpGet]
|
|
public List<OrderViewModel>? GetOrders(int _clientID) {
|
|
try
|
|
{
|
|
return _order.ReadList(new OrderSearchModel
|
|
{
|
|
ID = _clientID
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка получения списка заказов клиента id = {Id} ", _clientID);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
|
|
[HttpPost]
|
|
public void CreateOrder(OrderBindingModel model) {
|
|
try
|
|
{
|
|
_order.CreateOrder(model);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка создания заказа");
|
|
throw;
|
|
}
|
|
}
|
|
[HttpGet]
|
|
public List<OrderViewModel>? GetAddProduct(int _clientID,int _orderID)
|
|
{
|
|
try
|
|
{
|
|
return _order.ReadList(new OrderSearchModel
|
|
{
|
|
ID = _orderID,
|
|
ClientID = _clientID
|
|
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка получения списка заказов клиента id = {Id} ", _clientID);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
|
|
[HttpPost]
|
|
public void AddProduct(OrderBindingModel model)
|
|
{
|
|
try
|
|
{
|
|
if (_order.ReadElement(new OrderSearchModel { ID = model.ID })==null)
|
|
{
|
|
CreateOrder( new OrderBindingModel
|
|
{
|
|
ClientID = model.ClientID,
|
|
ProductList = model.ProductList,//Суда поступает пустой список
|
|
Sum = model.Sum
|
|
});
|
|
}
|
|
var order=_order.ReadElement(new OrderSearchModel { ID = model.ID });//возвращает null
|
|
if (model != null&& order!=null)
|
|
{
|
|
order.ProductList = model.ProductList;
|
|
order.Sum = model.Sum;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка создания заказа");
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
}
|