PIbd-22_Smirnov_A.A._Securi.../SecuritySystem/SecuritySystemRestApi/Controllers/MainController.cs
2024-05-08 08:03:51 +04:00

95 lines
2.7 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 SecuritySystemContracts.BindingModels;
using SecuritySystemContracts.BusinessLogicsContracts;
using SecuritySystemContracts.SearchModels;
using SecuritySystemContracts.ViewModels;
using DocumentFormat.OpenXml.Office2010.Excel;
using Microsoft.AspNetCore.Mvc;
namespace SecuritySystemRestApi.Controllers
{
/// <summary>
/// в этом контроллере логика по заказам и изделиям
/// </summary>
//настройка у контроллера, так как снова используем несколько Post и Get запросов
[Route("api/[controller]/[action]")]
[ApiController]
public class MainController : Controller
{
private readonly ILogger _logger;
private readonly IOrderLogic _order;
private readonly ISecureLogic _secure;
public MainController(ILogger<MainController> logger, IOrderLogic order, ISecureLogic secure)
{
_logger = logger;
_order = order;
_secure = secure;
}
[HttpGet]
public List<SecureViewModel>? GetSecureList()
{
try
{
return _secure.ReadList(null);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка изделий");
throw;
}
}
[HttpGet]
public SecureViewModel? GetSecure(int secureId)
{
try
{
return _secure.ReadElement(new SecureSearchModel
{
Id = secureId
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения изделия по id={Id}", secureId);
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;
}
}
}
}