ISEbd_21_Kuklew_M.I._Softwa.../SoftwareInstallation/FormMakeShipment.cs
Максим Куклев a415a05259 загрузил
2024-05-12 14:38:32 +04:00

117 lines
4.4 KiB
C#

using SoftwareInstallationContracts.BusinessLogicsContracts;
using SoftwareInstallationContracts.SearchModels;
using Microsoft.Extensions.Logging;
namespace SoftwareInstallationView
{
public partial class FormMakeShipment : Form
{
private readonly ILogger _logger;
private readonly IPackageLogic _logicPackage;
private readonly IShopLogic _logicShop;
public FormMakeShipment(ILogger<FormMakeShipment> logger, IPackageLogic logicPackage, IShopLogic logicShop)
{
InitializeComponent();
_logger = logger;
_logicPackage = logicPackage;
_logicShop = logicShop;
}
private void FormMakeShipment_Load(object sender, EventArgs e)
{
_logger.LogInformation("Ice creams loading");
try
{
var list = _logicPackage.ReadList(null);
if (list != null)
{
comboBoxPackage.DisplayMember = "PackageName";
comboBoxPackage.ValueMember = "Id";
comboBoxPackage.DataSource = list;
comboBoxPackage.SelectedItem = null;
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Ice creams loading error");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
_logger.LogInformation("Shops loading");
try
{
var list = _logicShop.ReadList(null);
if (list != null)
{
comboBoxShop.DisplayMember = "ShopName";
comboBoxShop.ValueMember = "Id";
comboBoxShop.DataSource = list;
comboBoxShop.SelectedItem = null;
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Shops loading error");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ButtonSave_Click(object sender, EventArgs e)
{
if (comboBoxShop.SelectedValue == null)
{
MessageBox.Show("Выберите магазин", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (comboBoxPackage.SelectedValue == null)
{
MessageBox.Show("Выберите мороженое", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (string.IsNullOrEmpty(textBoxCount.Text))
{
MessageBox.Show("Заполните поле Количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_logger.LogInformation("Shop replenishment");
try
{
var iceCream = _logicPackage.ReadElement(new PackageSearchModel
{ Id = Convert.ToInt32(comboBoxPackage.SelectedValue) });
if (iceCream == null)
{
throw new Exception("Мороженое не найдено.");
}
var operationResult = _logicShop.MakeShipment(new ShopSearchModel
{
Id = Convert.ToInt32(comboBoxShop.SelectedValue)
},
iceCream,
Convert.ToInt32(textBoxCount.Text));
if (!operationResult)
{
throw new Exception("Ошибка при проведении поставки.");
}
MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
DialogResult = DialogResult.OK;
Close();
}
catch (Exception ex)
{
_logger.LogError(ex, "Shop replenishment error");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
DialogResult = DialogResult.OK;
Close();
}
private void ButtonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
}
}