PIbd-22_Gerimovich.I.M._Fur.../FurnitureAssembly/FurnitureAssemblyRestApi/Controllers/MainController.cs

90 lines
2.4 KiB
C#
Raw Normal View History

2024-05-08 11:24:46 +04:00
using FurnitureAssemblyContracts.BindingModels;
using FurnitureAssemblyContracts.BusinessLogicsContracts;
using FurnitureAssemblyContracts.SearchModels;
using FurnitureAssemblyContracts.ViewModels;
using Microsoft.AspNetCore.Mvc;
namespace FurnitureAssemblyRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class MainController : Controller
{
private readonly ILogger _logger;
2024-05-08 11:24:46 +04:00
private readonly IOrderLogic _order;
2024-05-08 11:24:46 +04:00
private readonly IFurnitureLogic _furniture;
2024-05-08 11:24:46 +04:00
public MainController(ILogger<MainController> logger, IOrderLogic order, IFurnitureLogic furniture)
{
_logger = logger;
_order = order;
_furniture = furniture;
}
2024-05-08 11:24:46 +04:00
[HttpGet]
public List<FurnitureViewModel>? GetFurnitureList()
{
try
{
return _furniture.ReadList(null);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка изделий");
2024-05-08 11:24:46 +04:00
throw;
}
}
2024-05-08 11:24:46 +04:00
[HttpGet]
public FurnitureViewModel? GetFurniture(int furnitureId)
{
try
{
return _furniture.ReadElement(new FurnitureSearchModel
{
Id = furnitureId
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения изделия по id={Id}", furnitureId);
2024-05-08 11:24:46 +04:00
throw;
}
}
2024-05-08 11:24:46 +04:00
[HttpGet]
public List<OrderViewModel>? GetOrders(int clientId)
{
try
{
return _order.ReadList(new OrderSearchModel
{
ClientId = clientId
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка заказов клиента id = {Id}", clientId);
2024-05-08 11:24:46 +04:00
throw;
}
}
2024-05-08 11:24:46 +04:00
[HttpPost]
public void CreateOrder(OrderBindingModel model)
{
try
{
_order.CreateOrder(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка создания заказа");
throw;
}
2024-05-08 11:24:46 +04:00
}
}
}