ISEbd-22_Rozhkov.I.E._Simple/GasStation/Forms/FormSupply.cs

51 lines
1.8 KiB
C#

using GasStation.Repositories;
using GasStation.Entities;
namespace GasStation.Forms
{
public partial class FormSupply : Form
{
private readonly ISupplyRepository _supplyRepository;
public FormSupply(ISupplyRepository supplyRepository, ISupplierRepository supplierRepository, IProductRepository productRepository)
{
InitializeComponent();
_supplyRepository = supplyRepository ??
throw new ArgumentNullException(nameof(supplyRepository));
comboBoxSupplier.DataSource = supplierRepository.ReadSupplier();
comboBoxSupplier.DisplayMember = "SupplierName";
comboBoxSupplier.ValueMember = "ID";
comboBoxProduct.DataSource = productRepository.ReadProduct();
comboBoxProduct.DisplayMember = "FullName";
comboBoxProduct.ValueMember = "ID";
}
private void ButtonSave_Click(object sender, EventArgs e)
{
try
{
if (comboBoxProduct.SelectedIndex < 0 ||
comboBoxSupplier.SelectedIndex < 0)
{
throw new Exception("Имеются незаполненые поля");
}
_supplyRepository.CreateSupply(Supply.CreateSupply(0, (int)comboBoxSupplier.SelectedValue!,
(int)comboBoxProduct.SelectedValue!, Convert.ToInt32(numericUpDownCount.Value)));
Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
private void ButtonCancel_Click(object sender, EventArgs e) => Close();
}
}