using ComputersShopContracts.BusinessLogicContracts; using ComputersShopContracts.ViewModels; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace ComputersShopView { public partial class FormShopReplenishment : Form { private readonly ILogger _logger; private readonly IShopLogic _shopLogic; private readonly IComputerLogic _computerLogic; private readonly List? _listStores; private readonly List? _listcomputers; public FormShopReplenishment(ILogger logger, IShopLogic shopLogic, IComputerLogic computerLogic) { InitializeComponent(); _shopLogic = shopLogic; _computerLogic = computerLogic; _logger = logger; _listStores = shopLogic.ReadList(null); if (_listStores != null) { сomboBoxShopName.DisplayMember = "ShopName"; сomboBoxShopName.ValueMember = "Id"; сomboBoxShopName.DataSource = _listStores; сomboBoxShopName.SelectedItem = null; } _listcomputers = computerLogic.ReadList(null); if (_listcomputers != null) { comboBoxComputerName.DisplayMember = "ComputerName"; comboBoxComputerName.ValueMember = "Id"; comboBoxComputerName.DataSource = _listcomputers; comboBoxComputerName.SelectedItem = null; } } private void ButtonSave_Click(object sender, EventArgs e) { if (сomboBoxShopName.SelectedValue == null) { MessageBox.Show("Выберите магазин", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (comboBoxComputerName.SelectedValue == null) { MessageBox.Show("Выберите компьютер", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } _logger.LogInformation("Добавление компьютер в магазин"); try { var computer = _computerLogic.ReadElement(new() { Id = (int)comboBoxComputerName.SelectedValue }); if (computer == null) { throw new Exception("Не найден компьютер. Дополнительная информация в логах."); } var resultOperation = _shopLogic.AddComputer( model: new() { Id = (int)сomboBoxShopName.SelectedValue }, computer: computer, quantity: Convert.ToInt32(CountTextBox.Text) ); if (!resultOperation) { throw new Exception("Ошибка при добавлении. Дополнительная информация в логах."); } MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); DialogResult = DialogResult.OK; Close(); } catch (Exception ex) { _logger.LogError(ex, "Ошибка сохранения компьютера"); MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void ButtonCancel_Click(object sender, EventArgs e) { DialogResult = DialogResult.Cancel; Close(); } } }