PIbd-23-Volkov-N.A.-Compute.../ComputersShop/ComputersShopView/FormSellComputers.cs

87 lines
3.4 KiB
C#

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 FormSellComputers : Form
{
private readonly ILogger _logger;
private readonly IShopLogic _shopLogic;
private readonly IComputerLogic _computerLogic;
private readonly List<ComputerViewModel>? _listComputer;
public FormSellComputers(ILogger<FormSellComputers> logger, IShopLogic shopLogic, IComputerLogic computerLogic)
{
InitializeComponent();
_logger = logger;
_shopLogic = shopLogic;
_computerLogic = computerLogic;
_listComputer = computerLogic.ReadList(null);
if (_listComputer != null)
{
comboBoxDocuments.DisplayMember = "ComputerName";
comboBoxDocuments.ValueMember = "Id";
comboBoxDocuments.DataSource = _listComputer;
comboBoxDocuments.SelectedItem = null;
}
}
private void ButtonSave_Click(object sender, EventArgs e)
{
if (comboBoxDocuments.SelectedValue == null)
{
MessageBox.Show("Выберите компьютер", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (string.IsNullOrEmpty(numericUpDownCount.Text))
{
MessageBox.Show("Заполните количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_logger.LogInformation("Продажа поездок");
try
{
var comp = _computerLogic.ReadElement(new()
{
Id = (int)comboBoxDocuments.SelectedValue
});
if (comp == null)
{
throw new Exception("Компьютер не найден. Дополнительная информация в логах.");
}
var operationResult = _shopLogic.SellComputers(
computer: comp,
quantity: (int)numericUpDownCount.Value
);
if (!operationResult)
{
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();
}
}
}