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

75 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 FormGasman : Form
{
private readonly IGasmanRepository _gasmanRepository;
private int? _gasmanId;
public FormGasman(IGasmanRepository gasmanRepository)
{
InitializeComponent();
_gasmanRepository = gasmanRepository ??
throw new ArgumentNullException(nameof(gasmanRepository));
}
public int ID
{
set
{
try
{
var gasman = _gasmanRepository.ReadGasmanByID(value);
2024-11-15 18:27:06 +04:00
if (gasman == null)
2024-11-13 23:20:06 +04:00
{
throw new InvalidDataException(nameof(gasman));
}
textBoxName.Text = gasman.GasmanName;
textBoxNumber.Text = gasman.PhoneNumber;
_gasmanId = value;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
}
private void ButtonSave_Click(object sender, EventArgs e)
{
try
{
if (string.IsNullOrWhiteSpace(textBoxName.Text) ||
string.IsNullOrWhiteSpace(textBoxNumber.Text))
{
throw new Exception("Имеются незаполненые поля");
}
if (_gasmanId.HasValue)
{
_gasmanRepository.UpdateGasman(CreateGasman(_gasmanId.Value));
}
else
{
_gasmanRepository.CreateGasman(CreateGasman(0));
}
Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
private void ButtonCancel_Click(object sender, EventArgs e) => Close();
private Gasman CreateGasman(int id) => Gasman.CreateGasman(id, textBoxName.Text, textBoxNumber.Text);
}
}