PIbd-23_Elatomtsev_L.K._Con.../Confectionery/ConfectioneryRestApi/Controllers/MainController.cs

87 lines
2.5 KiB
C#
Raw Normal View History

2024-05-05 03:45:20 +04:00
using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.BusinessLogicsContracts;
using ConfectioneryContracts.SearchModels;
using ConfectioneryContracts.ViewModels;
using Microsoft.AspNetCore.Mvc;
namespace ConfectioneryRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class MainController : Controller
{
private readonly ILogger _logger;
private readonly IOrderLogic _order;
private readonly IPastryLogic _pastry;
public MainController(ILogger<MainController> logger,
IOrderLogic order, IPastryLogic pastry)
{
_logger = logger;
_order = order;
_pastry = pastry;
}
[HttpGet]
public List<PastryViewModel>? GetPastryList()
{
try
{
return _pastry.ReadList(null);
}
catch (Exception ex)
{
_logger.LogError(
ex, "Ошибка получения списка кондитерских изделий");
throw;
}
}
[HttpGet]
public PastryViewModel? GetPastry(int pastryId)
{
try
{
return _pastry.ReadElement(new PastrySearchModel
{
2024-05-08 11:57:33 +04:00
Id = pastryId
2024-05-05 03:45:20 +04:00
});
}
catch (Exception ex)
{
_logger.LogError(
2024-05-08 11:57:33 +04:00
ex, "Ошибка получения кондитерского изделия по id = {Id}",
pastryId);
2024-05-05 03:45:20 +04:00
throw;
}
}
[HttpGet]
public List<OrderViewModel>? GetOrders(int clientId)
{
try
{
return _order.ReadList(new OrderSearchModel
{
ClientId = clientId
});
}
catch (Exception ex)
{
_logger.LogError(
2024-05-08 11:57:33 +04:00
ex, "Ошибка получения списка заказов клиента id = {Id}",
2024-05-05 03:45:20 +04:00
clientId);
throw;
}
}
[HttpPost]
public void CreateOrder(OrderBindingModel model)
{
try
{
_order.CreateOrder(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка создания заказа");
throw;
}
}
}
}