ISEbd-22_Rozhkov.I.E._Simple/GasStation/Forms/FormSupplier.cs
2024-11-13 23:20:06 +04:00

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));
}
textBoxSupplierName.Text = 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 (string.IsNullOrWhiteSpace(textBoxSupplierName.Text))
{
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, textBoxSupplierName.Text);
private void ButtonCancel_Click(object sender, EventArgs e) => Close();
}
}