PIbd21_Makarov_DV_FlowerShop/FlowerShop/FlowerShopRestApi/Controllers/MainController.cs

83 lines
2.4 KiB
C#
Raw Normal View History

2024-04-04 18:22:29 +04:00
using DocumentFormat.OpenXml.Office2010.Excel;
using FlowerShopContracts.BindingModels;
using FlowerShopContracts.BusinessLogicsContracts;
using FlowerShopContracts.SearchModels;
using FlowerShopContracts.ViewModels;
using Microsoft.AspNetCore.Mvc;
namespace FlowerShopRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class MainController : Controller
{
private readonly ILogger _logger;
private readonly IOrderLogic _order;
private readonly IFlowerLogic _flower;
public MainController(ILogger<MainController> logger, IOrderLogic order, IFlowerLogic flower)
{
_logger = logger;
_order = order;
_flower = flower;
}
[HttpGet]
public List<FlowerViewModel>? GetFlowerList()
{
try
{
return _flower.ReadList(null);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка цветов");
throw;
}
}
[HttpGet]
public FlowerViewModel? GetFlower(int flowerId)
{
try
{
return _flower.ReadElement(new FlowerSearchModel
{
Id = flowerId
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения цветов по id = {Id}",flowerId);
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;
}
}
}
}