CourseWork_CompShop/ComputerShopProvider/ComputerShopRestApi/Controllers/SupplyController.cs
2023-05-20 08:34:33 +04:00

125 lines
3.5 KiB
C#

using ComputerShopContracts.BindingModels;
using ComputerShopContracts.BusinessLogicContracts;
using ComputerShopContracts.SearchModels;
using ComputerShopContracts.ViewModels;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace ComputerShopRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class SupplyController : Controller
{
private readonly ILogger _logger;
private readonly ISupplyLogic _supply;
private readonly IOrderLogic _order;
private readonly IEquipmentReceivingLogic _equipmentReceiving;
public SupplyController(ILogger<SupplyController> logger, ISupplyLogic supply, IOrderLogic order, IEquipmentReceivingLogic equipmentReceiving)
{
_logger = logger;
_supply = supply;
_order = order;
_equipmentReceiving = equipmentReceiving;
}
[HttpGet]
public List<EquipmentReceivingViewModel>? GetReceivingList()
{
try
{
return _equipmentReceiving.ReadList(null);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка компонентов");
throw;
}
}
[HttpGet]
public List<SupplyViewModel>? GetSupplyList(int componentid)
{
try
{
return _supply.ReadList(new SupplySearchModel { ComponentId = componentid });
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка сборок");
throw;
}
}
[HttpGet]
public List<OrderViewModel>? GetOrderList()
{
try
{
return _order.ReadList(null);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка заказов");
throw;
}
}
[HttpPost]
public void CreateAssembly(SupplyBindingModel model)
{
try
{
_supply.Create(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка создания поставки");
throw;
}
}
[HttpPost]
public void DeleteSupply(SupplyBindingModel model)
{
try
{
_supply.Delete(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка удаления поставки");
throw;
}
}
[HttpPost]
public void AddOrderToSupply(Tuple<SupplySearchModel, OrderSearchModel> model)
{
try
{
_supply.AddOrder(model.Item1, model.Item2);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка.");
throw;
}
}
[HttpPost]
public void EditOrder(OrderBindingModel model)
{
try
{
_order.Update(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка удаления заказа");
throw;
}
}
}
}