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); if (gasman != null) { 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); } }