CourseWork_CompShop/ComputerShopProvider/ComputerShopRestApi/Controllers/MainController.cs

82 lines
2.5 KiB
C#

using ComputerShopContracts.BindingModels;
using ComputerShopContracts.BusinessLogicContracts;
using ComputerShopContracts.SearchModels;
using ComputerShopContracts.ViewModels;
using Microsoft.AspNetCore.Mvc;
namespace ComputerShopRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class MainController : Controller
{
private readonly ILogger _logger;
private readonly IPurchaseLogic _purchase;
private readonly IComponentLogic _component;
public MainController(ILogger<MainController> logger, IPurchaseLogic purchase, IComponentLogic component)
{
_logger = logger;
_purchase = purchase;
_component = component;
}
[HttpGet]
public List<ComponentViewModel>? GetComponentList()
{
try
{
return _component.ReadList(null);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка компонентов");
throw;
}
}
[HttpGet]
public ComponentViewModel? GetComponent(int componentId)
{
try
{
return _component.ReadElement(new ComponentSearchModel
{
Id = componentId
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения компонента по id={Id}", componentId);
throw;
}
}
[HttpGet]
public List<PurchaseViewModel>? GetPurchases(int clientId)
{
try
{
return _purchase.ReadList(new PurchaseSearchModel
{
ClientId = clientId
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка закупок клиента id ={ Id}", clientId);
throw;
}
}
[HttpPost]
public void CreatePurchase(PurchaseBindingModel model)
{
try
{
_purchase.CreatePurchase(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка создания закупки");
throw;
}
}
}
}