PIbd-22_Chernyshev_G.J._30_.../GarmentFactoryRestApi/Controllers/MainController.cs

83 lines
2.3 KiB
C#
Raw Normal View History

2024-04-11 13:43:22 +04:00
using Microsoft.AspNetCore.Mvc;
using GarmentFactoryContracts.BindingModels;
using GarmentFactoryContracts.BusinessLogicsContracts;
using GarmentFactoryContracts.SearchModels;
using GarmentFactoryContracts.ViewModels;
namespace GarmentFactoryRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class MainController : Controller
{
private readonly ILogger _logger;
private readonly IOrderLogic _order;
private readonly ITextileLogic _textile;
public MainController(ILogger<MainController> logger, IOrderLogic order, ITextileLogic textile)
{
_logger = logger;
_order = order;
_textile = textile;
}
[HttpGet]
public List<TextileViewModel>? GetTextileList()
{
try
{
return _textile.ReadList(null);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка продуктов");
throw;
}
}
[HttpGet]
public TextileViewModel? GetTextile(int TextileId)
{
try
{
return _textile.ReadElement(new TextileSearchModel { Id = TextileId });
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения продукта по id={Id}", TextileId);
throw;
}
}
[HttpGet]
public List<OrderViewModel>? GetOrders(int clientId)
{
try
{
return _order.ReadList(new OrderSearchModel { ClientId = 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;
}
}
}
}