PIbd-21_Belianin_N.N._Furni.../FurnitureAssembly/FurnitureAssemblyRestApi/Controllers/MainController.cs
2024-05-10 22:00:13 +04:00

93 lines
2.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using FurnitureAssemblyContracts.BindingModels;
using FurnitureAssemblyContracts.BusinessLogicsContracts;
using FurnitureAssemblyContracts.SearchModels;
using FurnitureAssemblyContracts.ViewModels;
using Microsoft.AspNetCore.Mvc;
namespace FurnitureAssemblyRestApi.Controllers
{
// Контроллер с логикой по заказам и изделиям
// Настройка контроллер для использования нескольких Post и Get запросов
[Route("api/[controller]/[action]")]
[ApiController]
public class MainController : Controller
{
private readonly ILogger _logger;
private readonly IOrderLogic _order;
private readonly IFurnitureLogic _furniture;
public MainController(ILogger<MainController> logger, IOrderLogic order, IFurnitureLogic furniture)
{
_logger = logger;
_order = order;
_furniture = furniture;
}
[HttpGet]
public List<FurnitureViewModel>? GetFurnitureList()
{
try
{
return _furniture.ReadList(null);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка изделий");
throw;
}
}
[HttpGet]
public FurnitureViewModel? GetFurniture(int furnitureId)
{
try
{
return _furniture.ReadElement(new FurnitureSearchModel
{
Id = furnitureId
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения изделия по id={Id}", furnitureId);
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;
}
}
}
}