2023-05-19 18:26:38 +04:00
|
|
|
|
using ComputerShopContracts.BindingModels;
|
|
|
|
|
using ComputerShopContracts.BusinessLogicContracts;
|
|
|
|
|
using ComputerShopContracts.SearchModels;
|
|
|
|
|
using ComputerShopContracts.ViewModels;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
|
|
namespace ComputerShopRestApi.Controllers
|
|
|
|
|
{
|
2023-05-20 01:31:46 +04:00
|
|
|
|
[Route("api/[controller]/[action]")]
|
|
|
|
|
[ApiController]
|
2023-05-19 18:26:38 +04:00
|
|
|
|
public class OrderController : Controller
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
2023-05-20 01:31:46 +04:00
|
|
|
|
private readonly IOrderLogic _order;
|
|
|
|
|
private readonly ISupplyLogic _supply;
|
2023-05-19 18:26:38 +04:00
|
|
|
|
|
2023-05-20 01:31:46 +04:00
|
|
|
|
public OrderController(ILogger<MainController> logger, IOrderLogic order, ISupplyLogic supply)
|
2023-05-19 18:26:38 +04:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
2023-05-20 01:31:46 +04:00
|
|
|
|
_order = order;
|
|
|
|
|
_supply = supply;
|
2023-05-19 18:26:38 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
2023-05-20 01:31:46 +04:00
|
|
|
|
public List<OrderViewModel>? GetOrderList(int clientId)
|
2023-05-19 18:26:38 +04:00
|
|
|
|
{
|
2023-05-20 01:31:46 +04:00
|
|
|
|
|
2023-05-19 18:26:38 +04:00
|
|
|
|
try
|
|
|
|
|
{
|
2023-05-20 01:31:46 +04:00
|
|
|
|
return _order.ReadList(new OrderSearchModel { ClientId = clientId });
|
2023-05-19 18:26:38 +04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2023-05-20 01:31:46 +04:00
|
|
|
|
_logger.LogError(ex, "Ошибка получения списка заказов");
|
2023-05-19 18:26:38 +04:00
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
[HttpGet]
|
2023-05-20 01:31:46 +04:00
|
|
|
|
public List<SupplyViewModel>? GetSuppliesList()
|
2023-05-19 18:26:38 +04:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-05-20 01:31:46 +04:00
|
|
|
|
return _supply.ReadList(null);
|
2023-05-19 18:26:38 +04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2023-05-20 01:31:46 +04:00
|
|
|
|
_logger.LogError(ex, "Ошибка получения списка компонентов");
|
2023-05-19 18:26:38 +04:00
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public List<OrderViewModel>? GetOrders(int clientId)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-05-20 01:31:46 +04:00
|
|
|
|
return _order.ReadList(new OrderSearchModel {});
|
2023-05-19 18:26:38 +04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка получения списка заказов клиента id={Id}", clientId);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public void CreateOrder(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-05-20 01:31:46 +04:00
|
|
|
|
_order.CreateOrder(model);
|
2023-05-19 18:26:38 +04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка создания заказа");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-20 01:31:46 +04:00
|
|
|
|
[HttpDelete]
|
|
|
|
|
public void DeleteOrder(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_order.Delete(model);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка при удалении заказа");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-19 18:26:38 +04:00
|
|
|
|
}
|
|
|
|
|
}
|