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

72 lines
2.3 KiB
C#
Raw Normal View History

2024-11-13 23:20:06 +04:00
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);
2024-11-15 18:27:06 +04:00
if (supplier == null)
2024-11-13 23:20:06 +04:00
{
throw new InvalidDataException(nameof(supplier));
}
2024-11-15 18:27:06 +04:00
comboBoxSupplierName.DataSource = supplier.ProductName;
2024-11-13 23:20:06 +04:00
_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
{
2024-11-15 18:27:06 +04:00
if (comboBoxSupplierName.SelectedIndex < 1)
2024-11-13 23:20:06 +04:00
{
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;
}
}
2024-11-15 18:27:06 +04:00
private Supplier CreateSupplier(int id) => Supplier.CreateSupplier(id, comboBoxSupplierName.Text);
2024-11-13 23:20:06 +04:00
private void ButtonCancel_Click(object sender, EventArgs e) => Close();
}
}