using HardwareShopContracts.BindingModels; using HardwareShopContracts.BusinessLogicsContracts; using HardwareShopContracts.SearchModels; using HardwareShopContracts.ViewModels; using HardwareShopDatabaseImplement.Models.Storekeeper; using HardwareShopDataModels.Enums; using HardwareShopDataModels.Models; using HardwareShopRestApi.Controllers; using Microsoft.AspNetCore.Mvc; using System.Xml.Linq; namespace HardwareShopRestApi.Controllers { [Route("api/[controller]/[action]")] [ApiController] public class PurchaseController : Controller { private readonly ILogger _logger; private readonly IPurchaseLogic _purchaseLogic; public PurchaseController(IPurchaseLogic purchaseLogic, ILogger logger) { _logger = logger; _purchaseLogic = purchaseLogic; } [HttpGet] public List? GetPurchases(int userId) { try { return _purchaseLogic.ReadList(new() { UserId = userId }); } catch (Exception ex) { _logger.LogError(ex, "Ошибка получения списка товаров"); throw; } } [HttpGet] public PurchaseViewModel? GetPurchase(int id) { try { return _purchaseLogic.ReadElement(new() { Id = id }); } catch (Exception ex) { _logger.LogError(ex, "Ошибка получения товара"); throw; } } [HttpPost] public void CreatePurchase(PurchaseBindingModel model) { try { for (int i = 0; i < model.PurchaseGoodsCounts.Count; i++) { model.PurchaseGoods.Add(model.ListPurchaseGoods[i].Id,(model.ListPurchaseGoods[i] as IGoodModel, model.PurchaseGoodsCounts[i])); } _purchaseLogic.Create(model); } catch (Exception ex) { _logger.LogError(ex, "Ошибка создания товара"); throw; } } [HttpPost] public void UpdatePurchase(PurchaseBindingModel model) { try { var oldModel = _purchaseLogic.ReadElement(new() { Id = model.Id, }); model.Sum = oldModel.Sum; _purchaseLogic.DeliveryPurchase(model); } catch (Exception ex) { _logger.LogError(ex, "Ошибка обновления данных товара"); throw; } } [HttpPost] public void DeletePurchase(PurchaseBindingModel model) { try { _purchaseLogic.Delete(model); } catch (Exception ex) { _logger.LogError(ex, "Ошибка удаления товара"); throw; } } } }