using ElectronicsShopContracts.BindingModels; using ElectronicsShopContracts.BusinessLogicContracts; using ElectronicsShopContracts.SearchModels; using ElectronicsShopContracts.ViewModels; using ElectronicsShopDataBaseImplement.Models; using ElectronicsShopDataModels.Models; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; 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 logger, IProductLogic product, IOrderLogic orderLogic) { _logger = logger; _product = product; _order = orderLogic; } [HttpGet] public List? 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? 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 CreateOrder(OrderBindingModel model) { try { _order.CreateOrder(model); } catch (Exception ex) { _logger.LogError(ex, "Ошибка создания заказа"); throw; } } [HttpPost] public void AddProduct(List jslist) { var product = JsonConvert.DeserializeObject(jslist[0]); int count = JsonConvert.DeserializeObject(jslist[1]); int orderid = JsonConvert.DeserializeObject(jslist[2]); try { _order.AddProduct(product, count, orderid); } catch (Exception ex) { _logger.LogError(ex, "Ошибка добавления заказа"); throw; } } } }