CourseWork_CompShop/ComputerShopProvider/ComputerShopRestApi/Controllers/ComponentController.cs

94 lines
2.7 KiB
C#
Raw Normal View History

using Microsoft.AspNetCore.Mvc;
using ComputerShopContracts.BindingModels;
using ComputerShopContracts.BusinessLogicContracts;
using ComputerShopContracts.SearchModels;
using ComputerShopContracts.ViewModels;
2023-05-17 13:52:06 +04:00
using System.Reflection;
namespace ComputerShopRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class ComponentController : Controller
{
private readonly ILogger _logger;
private readonly IComponentLogic _component;
2023-04-09 11:30:54 +04:00
public ComponentController(ILogger<ComponentController> logger, IComponentLogic component)
{
_logger = logger;
_component = component;
}
[HttpGet]
2023-05-17 13:52:06 +04:00
public List<ComponentViewModel>? GetComponentList(int clientId)
{
try
{
2023-05-17 13:52:06 +04:00
return _component.ReadList(new ComponentSearchModel
{
ClientId = clientId,
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка компонентов");
throw;
}
}
[HttpPost]
public void CreateComponent(ComponentBindingModel model)
{
try
{
_component.Create(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка создания компонента");
throw;
}
}
2023-05-17 17:28:31 +04:00
[HttpPost]
2023-04-09 12:04:14 +04:00
public void DeleteComponent(ComponentBindingModel model)
{
try
{
_component.Delete(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка удаления компонента");
throw;
}
}
2023-05-17 17:28:31 +04:00
[HttpPost]
public void EditComponent(ComponentBindingModel model)
{
try
{
_component.Update(model);
}
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;
}
}
2024-07-26 02:49:24 +04:00
}
}