CourseWorkElectronicsShop/ElectronicsShop/ElectronicsShopRestAPI/Controllers/MainController.cs
Илья Федотов 26745f6d2b 123
2024-05-30 20:33:25 +04:00

103 lines
3.0 KiB
C#

using ElectronicsShopContracts.BindingModels;
using ElectronicsShopContracts.BusinessLogicContracts;
using ElectronicsShopContracts.SearchModels;
using ElectronicsShopContracts.ViewModels;
using ElectronicsShopDataBaseImplement.Models;
using ElectronicsShopDataModels.Models;
using Microsoft.AspNetCore.Mvc;
namespace ElectronicsShopRestAPI.Controllers {
[Route("api/[controller]/[action]")]
[ApiController]
public class MainController : Controller {
private readonly ILogger _logger;
private readonly IProductLogic _product;
private readonly IOrderLogic _order;
private readonly IMessageInfoLogic _message;
public MainController(ILogger<MainController> logger, IProductLogic product,
IOrderLogic orderLogic) {
_logger = logger;
_product = product;
_order = orderLogic;
_ProductList = new Dictionary<int, (IProductModel, int)>();
}
[HttpGet]
public List<ProductViewModel>? GetProducts() {
try {
return _product.ReadList(null);
}
catch (Exception ex) {
_logger.LogError(ex, $"Ошибка получения данных");
throw;
}
}
[HttpGet]
public ProductViewModel? GetProduct(int _productID)
{
try
{
return _product.ReadElement(new ProductSearchModel
{
ID = _productID
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения товаров id={Id}", _productID);
throw;
}
}
[HttpGet]
public List<OrderViewModel>? GetOrders(int _clientID) {
try
{
return _order.ReadList(new OrderSearchModel
{
ClientID = _clientID
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка заказов клиента id = {Id} ", _clientID);
throw;
}
}
[HttpGet]
public OrderViewModel? GetOrder(int _clientID) {
try {
return _order.ReadElement(new OrderSearchModel { ClientID = _clientID });
}
catch (Exception ex) {
_logger.LogError(ex, $"Ошибка получения данных clientid = {_clientID}");
throw;
}
}
[HttpPost]
public void AddProduct(OrderBindingModel model)
{
try
{
var order=_order.ReadElement(new OrderSearchModel { ID = model.ID });//возвращает null
if (model != null&& order!=null)
{
order.ProductList = model.ProductList;
order.Sum += model.Sum;
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка создания заказа");
throw;
}
}
}
}