ISEbd-21.Gordeev.I.V.SushiB.../SushiBar/SushiBarRestApi/Controllers/MainController.cs

84 lines
2.7 KiB
C#
Raw Normal View History

2024-05-01 10:50:16 +04:00
using DocumentFormat.OpenXml.Office2010.Excel;
using Microsoft.AspNetCore.Mvc;
2024-05-01 13:12:40 +04:00
using SushiBarContracts.BindingModels;
2024-04-25 13:16:23 +04:00
using SushiBarContracts.BusinessLogicsContracts;
2024-05-01 13:12:40 +04:00
using SushiBarContracts.SearchModels;
2024-04-25 13:16:23 +04:00
using SushiBarContracts.ViewModels;
namespace SushiBarRestApi.Controllers
{
2024-05-12 19:04:40 +04:00
[Route("api/[controller]/[action]")]
[ApiController]
public class MainController : Controller
2024-04-25 13:16:23 +04:00
{
2024-05-12 19:04:40 +04:00
private readonly ILogger _logger;
private readonly IOrderLogic _order;
private readonly ISushiLogic _sushi;
public MainController(ILogger<MainController> logger, IOrderLogic order, ISushiLogic sushi)
2024-04-25 13:16:23 +04:00
{
2024-05-12 19:04:40 +04:00
_logger = logger;
_order = order;
_sushi = sushi;
2024-04-25 13:16:23 +04:00
}
2024-05-12 19:04:40 +04:00
[HttpGet]
public List<SushiViewModel>? GetSushiList()
2024-04-25 13:16:23 +04:00
{
2024-05-12 19:04:40 +04:00
try
{
return _sushi.ReadList(null);
}
catch (Exception ex)
2024-05-01 10:50:16 +04:00
{
2024-05-12 19:04:40 +04:00
_logger.LogError(ex, "Ошибка получения списка продуктов");
throw;
}
2024-04-25 13:16:23 +04:00
}
2024-05-12 19:04:40 +04:00
[HttpGet]
public SushiViewModel? GetSushi(int sushiId)
2024-04-25 13:16:23 +04:00
{
2024-05-12 19:04:40 +04:00
try
{
return _sushi.ReadElement(new SushiSearchModel
{
Id =
sushiId
});
}
catch (Exception ex)
2024-05-01 10:50:16 +04:00
{
2024-05-12 19:04:40 +04:00
_logger.LogError(ex, "Ошибка получения продукта по id={Id}", sushiId);
throw;
}
2024-04-25 13:16:23 +04:00
}
2024-05-12 19:04:40 +04:00
[HttpGet]
public List<OrderViewModel>? GetOrders(int clientId)
2024-04-25 13:16:23 +04:00
{
2024-05-12 19:04:40 +04:00
try
{
return _order.ReadList(new OrderSearchModel
{
ClientId = clientId
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка заказов id ={ Id} ", clientId);
2024-04-25 13:16:23 +04:00
throw;
2024-05-12 19:04:40 +04:00
}
2024-04-25 13:16:23 +04:00
}
2024-05-12 19:04:40 +04:00
[HttpPost]
public void CreateOrder(OrderBindingModel model)
2024-04-25 13:16:23 +04:00
{
2024-05-12 19:04:40 +04:00
try
{
_order.CreateOrder(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка создания заказа");
throw;
}
2024-04-25 13:16:23 +04:00
}
}
}