PIbd-22_Chernyshev_Shabunov.../ComputerShopRestApi/Controllers/ComponentController.cs
2024-05-29 01:02:20 +04:00

99 lines
2.1 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 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 ComponentController : Controller
{
private readonly ILogger _logger;
private readonly IComponentLogic _componentLogic;
public ComponentController(IComponentLogic Logic, ILogger<ComponentController> Logger)
{
_logger = Logger;
_componentLogic = Logic;
}
[HttpGet]
public ComponentViewModel? GetComponent(int Id)
{
try
{
return _componentLogic.ReadElement(new ComponentSearchModel
{
Id = Id
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения комплектующей");
throw;
}
}
[HttpGet]
public List<ComponentViewModel>? GetComponents(int? UserId)
{
try
{
return _componentLogic.ReadList(new ComponentSearchModel
{
UserId = UserId
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка комплектующих пользователя с Id = {Id}", UserId);
throw;
}
}
[HttpPost]
public void CreateComponent(ComponentBindingModel Model)
{
try
{
_componentLogic.Create(Model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка создания комплектующей");
throw;
}
}
[HttpPost]
public void UpdateComponent(ComponentBindingModel Model)
{
try
{
_componentLogic.Update(Model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка обновления комплектующей");
throw;
}
}
[HttpPost]
public void DeleteComponent(ComponentBindingModel Model)
{
try
{
_componentLogic.Delete(Model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка удаления комплектующей");
throw;
}
}
}
}