72 lines
2.3 KiB
C#
72 lines
2.3 KiB
C#
using GasStation.Entities;
|
|
using GasStation.Repositories;
|
|
|
|
namespace GasStation.Forms
|
|
{
|
|
public partial class FormSupplier : Form
|
|
{
|
|
private readonly ISupplierRepository _suppliarRepository;
|
|
|
|
private int? _supplierId;
|
|
|
|
public int ID
|
|
{
|
|
set
|
|
{
|
|
try
|
|
{
|
|
var supplier = _suppliarRepository.ReadSupplierByID(value);
|
|
if (supplier == null)
|
|
{
|
|
throw new InvalidDataException(nameof(supplier));
|
|
}
|
|
|
|
comboBoxSupplierName.DataSource = supplier.ProductName;
|
|
_supplierId = value;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
public FormSupplier(ISupplierRepository supplierRepository)
|
|
{
|
|
InitializeComponent();
|
|
_suppliarRepository = supplierRepository ??
|
|
throw new ArgumentNullException(nameof(supplierRepository));
|
|
}
|
|
|
|
private void ButtonSave_Click(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
if (comboBoxSupplierName.SelectedIndex < 1)
|
|
{
|
|
throw new Exception("Имеются незаполненые поля");
|
|
}
|
|
if (_supplierId.HasValue)
|
|
{
|
|
_suppliarRepository.UpdateSupplier(CreateSupplier(_supplierId.Value));
|
|
}
|
|
else
|
|
{
|
|
_suppliarRepository.CreateSupplier(CreateSupplier(0));
|
|
}
|
|
Close();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
}
|
|
|
|
private Supplier CreateSupplier(int id) => Supplier.CreateSupplier(id, comboBoxSupplierName.Text);
|
|
|
|
private void ButtonCancel_Click(object sender, EventArgs e) => Close();
|
|
}
|
|
}
|