PIbd_23_Kislitsa_E.D_Diner/Diner/DinerRestApi/Controllers/MainController.cs

89 lines
2.6 KiB
C#
Raw Permalink Normal View History

2024-04-02 14:15:39 +04:00
using DinerContracts.BindingModels;
using DinerContracts.BusinessLogicsContracts;
using DinerContracts.SearchModels;
using DinerContracts.ViewModels;
using Microsoft.AspNetCore.Mvc;
namespace DinerRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class MainController : Controller
{
private readonly ILogger _logger;
private readonly IOrderLogic _order;
private readonly ISnackLogic _snack;
public MainController(ILogger<MainController> logger, IOrderLogic order, ISnackLogic snack)
{
_logger = logger;
_order = order;
_snack = snack;
}
[HttpGet]
public List<SnackViewModel>? GetSnackList()
{
try
{
return _snack.ReadList(null);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка изделий");
throw;
}
}
[HttpGet]
public SnackViewModel? GetSnack(int snackId)
{
try
{
return _snack.ReadElement(new SnackSearchModel
{
Id = snackId
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения изделия по id={Id}", snackId);
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;
}
}
}
}