PIbd-23-Nasyrov-A.G.-Flower.../FlowerShopRestApi/Controllers/MainController.cs
2024-04-07 14:10:31 +04:00

85 lines
2.4 KiB
C#

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;
}
}
}
}