PIbd-23-Radaev-A.V.-GiftShop/GiftShop/GiftShopRestApi/Controllers/MainController.cs

83 lines
2.3 KiB
C#
Raw Normal View History

2024-04-09 17:02:29 +04:00
using GiftShopContracts.BindingModels;
using GiftShopContracts.BusinessLogicsContracts;
using GiftShopContracts.SearchModels;
using GiftShopContracts.ViewModels;
using Microsoft.AspNetCore.Mvc;
namespace GiftShopRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class MainController : Controller
{
private readonly ILogger _logger;
private readonly IOrderLogic _order;
private readonly IGiftLogic _gift;
public MainController(ILogger<MainController> logger, IOrderLogic order, IGiftLogic gift)
{
_logger = logger;
_order = order;
_gift = gift;
}
[HttpGet]
public List<GiftViewModel>? GetGiftList()
{
try
{
return _gift.ReadList(null);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка продуктов");
throw;
}
}
[HttpGet]
public GiftViewModel? GetGift(int giftId)
{
try
{
return _gift.ReadElement(new GiftSearchModel { Id = giftId });
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения продукта по id={Id}", giftId);
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;
}
}
}
}