Computer_Hardware_Store/HardwareShop/HardwareShopRestApi/Controllers/ComponentController.cs

94 lines
2.5 KiB
C#

using HardwareShopContracts.BindingModels;
using HardwareShopContracts.BuisnessLogicsContracts;
using HardwareShopContracts.SearchModels;
using HardwareShopContracts.ViewModels;
using Microsoft.AspNetCore.Mvc;
namespace HardwareShopRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class ComponentController : Controller
{
private readonly ILogger _logger;
private readonly IComponentLogic _component;
public ComponentController(ILogger<ComponentController> logger, IComponentLogic component)
{
_logger = logger;
_component = component;
}
[HttpGet]
public List<ComponentViewModel>? GetComponents(int userId)
{
try
{
return _component.ReadList(new() { UserId = userId });
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка комплектующих");
throw;
}
}
[HttpGet]
public ComponentViewModel? GetComponent(int id)
{
try
{
return _component.ReadElement(new() { Id = id });
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения комплектующего");
throw;
}
}
[HttpPost]
public void CreateComponent(ComponentBindingModel model)
{
try
{
_component.Create(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка создания комплектующего");
throw;
}
}
[HttpPost]
public void UpdateData(ComponentBindingModel model)
{
try
{
_component.Update(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка обновления данных комплектующего");
throw;
}
}
[HttpPost]
public void DeleteComponent(ComponentBindingModel model)
{
try
{
_component.Delete(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка удаления комплектующего");
throw;
}
}
}
}