PIbd-21_RazubaevSM_Plumbing.../PlumbingRepair/PlumbingRepairRestApi/Controllers/MainController.cs

82 lines
2.2 KiB
C#
Raw Normal View History

2024-04-12 19:30:31 +04:00
using PlumbingRepairContracts.BindingModels;
using PlumbingRepairContracts.BusinessLogicsContracts;
using PlumbingRepairContracts.SearchModels;
using PlumbingRepairContracts.ViewModels;
using Microsoft.AspNetCore.Mvc;
namespace AbstractShopRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class MainController : Controller
{
private readonly ILogger _logger;
private readonly IOrderLogic _order;
private readonly IWorkLogic _work;
public MainController(ILogger<MainController> logger, IOrderLogic order, IWorkLogic work)
{
_logger = logger;
_order = order;
_work = work;
}
[HttpGet]
public List<WorkViewModel>? GetWorkList()
{
try
{
return _work.ReadList(null);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка продуктов");
throw;
}
}
[HttpGet]
public WorkViewModel? GetWork(int workId)
{
try
{
return _work.ReadElement(new WorkSearchModel { Id = workId });
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения продукта по id={Id}", workId);
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;
}
}
}
}