CourseWork_CompShop/ComputerShopProvider/ComputerShopRestApi/Controllers/MainController.cs

149 lines
4.6 KiB
C#
Raw Normal View History

using ComputerShopContracts.BindingModels;
using ComputerShopContracts.BusinessLogicContracts;
using ComputerShopContracts.SearchModels;
2023-05-20 07:41:13 +04:00
using ComputerShopContracts.StorageContracts;
using ComputerShopContracts.ViewModels;
2023-05-20 07:41:13 +04:00
using DocumentFormat.OpenXml.Presentation;
using Microsoft.AspNetCore.Mvc;
namespace ComputerShopRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class MainController : Controller
{
private readonly ILogger _logger;
private readonly IPurchaseLogic _purchase;
2023-05-20 07:41:13 +04:00
private readonly IPurchaseStorage _purchaseSTR;
private readonly IComponentLogic _component;
2023-05-20 07:41:13 +04:00
public MainController(ILogger<MainController> logger, IPurchaseLogic purchase, IComponentLogic component, IPurchaseStorage purchaseStorage)
{
_logger = logger;
_purchase = purchase;
_component = component;
2023-05-20 07:41:13 +04:00
_purchaseSTR = purchaseStorage;
}
[HttpGet]
public List<ComponentViewModel>? GetComponentList()
{
try
{
return _component.ReadList(null);
}
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;
}
}
[HttpGet]
public List<PurchaseViewModel>? GetPurchases(int clientId)
{
try
{
return _purchase.ReadList(new PurchaseSearchModel
{
ClientId = clientId
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка закупок клиента id ={ Id}", clientId);
throw;
}
}
[HttpPost]
public void CreatePurchase(PurchaseBindingModel model)
{
try
{
_purchase.CreatePurchase(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка создания закупки");
throw;
}
}
2023-05-20 07:41:13 +04:00
[HttpPost]
public void SetInWork(int id)
{
try
{
var puchase = _purchaseSTR.GetElement(new() { Id = id });
_purchase.TakePurchaseInWork(new()
{
Id = id,
ComponentId = puchase.ComponentId,
Count = puchase.Count,
Sum = puchase.Sum,
Status = puchase.Status
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка изменения закупки");
throw;
}
}
[HttpPost]
public void SetReady(int id)
{
try
{
var puchase = _purchaseSTR.GetElement(new() { Id = id });
_purchase.FinishPurchase(new()
{
Id = id,
ComponentId = puchase.ComponentId,
Count = puchase.Count,
Sum = puchase.Sum,
Status = puchase.Status
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка изменения закупки");
throw;
}
}
[HttpPost]
public void SetDone(int id)
{
try
{
var puchase = _purchaseSTR.GetElement(new() { Id = id });
_purchase.DeliveryPurchase(new()
{
Id = id,
ComponentId = puchase.ComponentId,
Count = puchase.Count,
Sum = puchase.Sum,
Status = puchase.Status
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка изменения закупки");
throw;
}
}
}
}