using ProjectGarage.Entities; using ProjectGarage.Entities.Enums; using ProjectGarage.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 ProjectGarage.Forms { public partial class FormDriver : Form { private readonly IDriverRepository _driverRepository; private int? _driverId; public int Id { set { try { var driver = _driverRepository.ReadDriverByID(value); if (driver == null) { throw new InvalidDataException(nameof(driver)); } textBoxFirstName.Text = driver.First_name; textBoxLastName.Text = driver.Last_name; //comboBoxTruckID.SelectedItem = driver.TruckId; _driverId = value; } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } } public FormDriver(IDriverRepository driverRepository, ITruckRepository truckRepository) { InitializeComponent(); _driverRepository = driverRepository ?? throw new ArgumentNullException(nameof(driverRepository)); comboBoxTruckID.DataSource = truckRepository.ReadTrucks();// comboBoxTruckID.DisplayMember = "Numbers"; comboBoxTruckID.ValueMember = "Id"; } private void ButtonSaveDriver_Click(object sender, EventArgs e) { try { if (string.IsNullOrWhiteSpace(textBoxFirstName.Text) || string.IsNullOrWhiteSpace(textBoxLastName.Text) || comboBoxTruckID.SelectedIndex < 0)// { throw new Exception("Имеются незаполненные поля"); } if (_driverId.HasValue) { _driverRepository.UpdateDriver(CreateDriver(_driverId.Value)); } else { _driverRepository.CreateDriver(CreateDriver(0)); } Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void ButtonCancelDriver_Click(object sender, EventArgs e) => Close(); private Driver CreateDriver(int id) => Driver.CreateDriver(id, textBoxFirstName.Text, textBoxLastName.Text, (int)comboBoxTruckID.SelectedIndex!);// } }