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

72 lines
2.2 KiB
C#

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