Coursework_ComputerStore_Li.../ComputerStoreRestAPI/Controllers/MainController.cs

243 lines
6.6 KiB
C#
Raw Normal View History

using ComputerStoreContracts.BindingModels;
using ComputerStoreContracts.BusinessLogicContracts;
2023-05-13 22:45:29 +04:00
using ComputerStoreContracts.SearchModels;
using ComputerStoreContracts.ViewModels;
using Microsoft.AspNetCore.Mvc;
namespace ComputerStoreRestAPI.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class MainController : Controller
{
private readonly ILogger _logger;
private readonly IComponentLogic _componentLogic;
private readonly IPCLogic _pcLogic;
private readonly IProductLogic _productLogic;
2023-05-17 17:57:25 +04:00
private readonly IRequestComponentLogic _requestComponentLogic;
private readonly IEmployeeReportLogic _employeeReportLogic;
2023-05-17 17:57:25 +04:00
private readonly IRequestLogic _requestLogic;
2023-05-17 17:57:25 +04:00
public MainController(ILogger<MainController> logger, IComponentLogic componentLogic, IPCLogic pcLogic, IProductLogic productLogic, IEmployeeReportLogic employeeReportLogic, IRequestComponentLogic requestComponentLogic, IRequestLogic requestLogic)
{
_logger = logger;
_componentLogic = componentLogic;
_pcLogic = pcLogic;
_productLogic = productLogic;
_employeeReportLogic = employeeReportLogic;
2023-05-17 17:57:25 +04:00
_requestComponentLogic = requestComponentLogic;
_requestLogic = requestLogic;
}
[HttpGet]
public List<ComponentViewModel>? GetComponentsList()
{
try
{
return _componentLogic.ReadList(null);
}
catch(Exception ex)
{
_logger.LogError(ex, "Receiving list of components error.");
throw;
}
}
2023-05-13 22:45:29 +04:00
[HttpGet]
public ComponentViewModel? GetComponent(int id)
{
try
{
return _componentLogic.ReadElement(new ComponentSearchModel { ID = id } );
}
catch (Exception ex)
{
_logger.LogError(ex, "Receiving list of components error.");
throw;
}
}
2023-05-13 21:17:24 +04:00
[HttpDelete("{id}")]
public bool DeleteComponent(int id)
{
try
{
2023-05-13 21:17:24 +04:00
return _componentLogic.Delete(new ComponentBindingModel { ID = id });
}
catch(Exception ex)
{
_logger.LogError(ex, "Removing component error.");
throw;
}
}
[HttpPatch]
public bool UpdateComponent(ComponentBindingModel component)
{
try
{
return _componentLogic.Update(component);
}
catch (Exception ex)
{
_logger.LogError(ex, "Updating component error.");
throw;
}
}
[HttpPost]
public bool InsertComponent(ComponentBindingModel component)
{
try
{
return _componentLogic.Create(component);
}
catch(Exception ex)
{
_logger.LogError(ex, "Inserting component error.");
throw;
}
}
[HttpGet]
public List<ProductViewModel>? GetProductsList()
{
try
{
return _productLogic.ReadList(null);
}
catch (Exception ex)
{
_logger.LogError(ex, "Receiving list of products error.");
throw;
}
}
2023-05-13 22:16:58 +04:00
[HttpDelete("{id}")]
public bool DeleteProduct(int id)
{
try
{
2023-05-13 22:16:58 +04:00
return _productLogic.Delete(new ProductBindingModel { ID = id });
}
catch (Exception ex)
{
_logger.LogError(ex, "Removing product error.");
throw;
}
}
[HttpPatch]
public bool UpdateProduct(ProductBindingModel product)
{
try
{
return _productLogic.Update(product);
}
catch (Exception ex)
{
_logger.LogError(ex, "Updating product error.");
throw;
}
}
[HttpPost]
public bool InsertProduct(ProductBindingModel product)
{
try
{
return _productLogic.Create(product);
}
catch (Exception ex)
{
_logger.LogError(ex, "Inserting product error.");
throw;
}
}
[HttpGet]
public List<PCViewModel>? GetPCsList()
{
try
{
return _pcLogic.ReadList(null);
}
catch (Exception ex)
{
_logger.LogError(ex, "Receiving list of PCs error.");
throw;
}
}
2023-05-13 22:16:58 +04:00
[HttpDelete("{id}")]
public bool DeletePC(int id)
{
try
{
2023-05-13 22:16:58 +04:00
return _pcLogic.Delete(new PCBindingModel { ID = id});
}
catch (Exception ex)
{
_logger.LogError(ex, "Removing PC error.");
throw;
}
}
[HttpPatch]
public bool UpdateProduct(PCBindingModel pc)
{
try
{
return _pcLogic.Update(pc);
}
catch (Exception ex)
{
_logger.LogError(ex, "Updating PC error.");
throw;
}
}
[HttpPost]
public bool InsertProduct(PCBindingModel pc)
{
try
{
return _pcLogic.Create(pc);
}
catch (Exception ex)
{
_logger.LogError(ex, "Inserting PC error.");
throw;
}
}
2023-05-17 17:57:25 +04:00
[HttpGet]
public List<RequestComponentViewModel>? GetRequestComponentList(int? id)
{
try
{
return _requestComponentLogic.ReadList(new RequestSearchModel { ID = id});
}
catch (Exception ex)
{
_logger.LogError(ex, "Receiving list of requestcomponent error.");
throw;
}
}
[HttpGet]
public List<RequestViewModel>? GetRequestList()
{
try
{
return _requestLogic.ReadList(null);
}
catch (Exception ex)
{
_logger.LogError(ex, "Receiving list of requestcomponent error.");
throw;
}
}
}
}