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

115 lines
2.4 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 AssemblyController : Controller
{
private readonly ILogger _logger;
private readonly IAssemblyLogic _assemblyLogic;
private readonly IReportGuarantorLogic _reportGuarantorLogic;
public AssemblyController(IAssemblyLogic Logic, ILogger<AssemblyController> Logger, IReportGuarantorLogic reportGuarantorLogic)
{
_logger = Logger;
_assemblyLogic = Logic;
_reportGuarantorLogic = reportGuarantorLogic;
}
[HttpGet]
public AssemblyViewModel? GetAssembly(int Id)
{
try
{
return _assemblyLogic.ReadElement(new AssemblySearchModel
{
Id = Id
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения сборки");
throw;
}
}
[HttpGet]
public List<AssemblyViewModel>? GetAssemblies(int? UserId)
{
// Implementer should be able to get all assemblies for Request binding
if (UserId == null)
{
try
{
return _assemblyLogic.ReadList(null);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка всех сборок");
throw;
}
}
try
{
return _assemblyLogic.ReadList(new AssemblySearchModel
{
UserId = UserId
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка сборок пользователя с Id = {Id}", UserId);
throw;
}
}
[HttpPost]
public void CreateAssembly(AssemblyBindingModel Model)
{
try
{
_assemblyLogic.Create(Model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка создания сборки");
throw;
}
}
[HttpPost]
public void UpdateAssembly(AssemblyBindingModel Model)
{
try
{
_assemblyLogic.Update(Model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка обновления сборки");
throw;
}
}
[HttpPost]
public void DeleteAssembly(AssemblyBindingModel Model)
{
try
{
_assemblyLogic.Delete(Model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка удаления сборки");
throw;
}
}
}
}