PIbd-23-Volkov-N.A.-Compute.../ComputersShop/ComputersShopView/FormShopReplenishment.cs
2024-02-18 12:22:10 +04:00

105 lines
3.1 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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<ShopViewModel>? _listStores;
private readonly List<ComputerViewModel>? _listcomputers;
public FormShopReplenishment(ILogger<FormShopReplenishment> 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();
}
}
}