using ProjectAirline.Entities; using ProjectAirline.Repositories; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace ProjectAirline.Forms; public partial class FormPassanger : Form { private readonly IPassangerRepository _passangerRepository; private int? _passangerId; public int Id { set { try { var passenger = _passangerRepository.ReadPassangerById(value); if (passenger == null) { throw new InvalidDataException(nameof(passenger)); } textBoxFirstName.Text = passenger.FirstName; textBoxFirstName.Text = passenger.LastName; _passangerId = value; } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } } public FormPassanger(IPassangerRepository passengerRepository) { InitializeComponent(); _passangerRepository = passengerRepository ?? throw new ArgumentNullException(nameof(passengerRepository)); } private void ButtonSave_Click(object sender, EventArgs e) { try { if (string.IsNullOrWhiteSpace(textBoxFirstName.Text) || string.IsNullOrWhiteSpace(textBoxFirstName.Text)) { throw new Exception("Имеются незаполненные поля"); } if (_passangerId.HasValue) { _passangerRepository.UpdatePassanger(CreatePassanger(_passangerId.Value)); } else { _passangerRepository.CreatePassanger(CreatePassanger(0)); } Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void ButtonCancel_Click(object sender, EventArgs e) => Close(); private Passanger CreatePassanger(int id) => Passanger.CreatePassanger(id, textBoxFirstName.Text, textBoxFirstName.Text); }