PIbd-22_Filippov_D.S._Bar/Bar/BarRestApi/Controllers/MainController.cs
2024-06-21 16:40:28 +04:00

87 lines
2.4 KiB
C#

using BarContracts.BindingModels;
using BarContracts.BusinessLogicContracts;
using BarContracts.SearchModels;
using BarContracts.ViewModels;
using Microsoft.AspNetCore.Mvc;
namespace BarRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class MainController : Controller
{
private readonly ILogger _logger;
private readonly IOrderLogic _order;
private readonly ICocktailLogic _cocktail;
public MainController(ILogger<MainController> Logger, IOrderLogic Order, ICocktailLogic Cocktail)
{
_logger = Logger;
_order = Order;
_cocktail = Cocktail;
}
[HttpGet]
public List<CocktailViewModel>? GetCocktailList()
{
try
{
return _cocktail.ReadList(null);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка продуктов");
throw;
}
}
[HttpGet]
public CocktailViewModel? GetCocktail(int CocktailId)
{
try
{
return _cocktail.ReadElement(new CocktailSearchModel
{
Id = CocktailId
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения продукта по id={Id}", CocktailId);
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;
}
}
}
}