From 6694e9c4a7d0f0d15221efdb1e7900419ed8b46c Mon Sep 17 00:00:00 2001 From: Baryshev Dmitry Date: Sun, 17 Nov 2024 16:52:57 +0400 Subject: [PATCH] a little work --- ProjectGarage/Forms/FormDriver.Designer.cs | 3 + ProjectGarage/Forms/FormDriver.cs | 75 +++++++++++++++++++++- ProjectGarage/Forms/FormTruck.cs | 9 --- 3 files changed, 76 insertions(+), 11 deletions(-) diff --git a/ProjectGarage/Forms/FormDriver.Designer.cs b/ProjectGarage/Forms/FormDriver.Designer.cs index 9415591..c8169b1 100644 --- a/ProjectGarage/Forms/FormDriver.Designer.cs +++ b/ProjectGarage/Forms/FormDriver.Designer.cs @@ -106,6 +106,7 @@ buttonSaveDriver.TabIndex = 8; buttonSaveDriver.Text = "Сохранить"; buttonSaveDriver.UseVisualStyleBackColor = true; + buttonSaveDriver.Click += ButtonSaveDriver_Click; // // buttonCancelDriver // @@ -115,9 +116,11 @@ buttonCancelDriver.TabIndex = 9; buttonCancelDriver.Text = "Отмена"; buttonCancelDriver.UseVisualStyleBackColor = true; + buttonCancelDriver.Click += ButtonCancelDriver_Click; // // comboBoxTruckID // + comboBoxTruckID.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxTruckID.FormattingEnabled = true; comboBoxTruckID.Location = new Point(120, 156); comboBoxTruckID.Name = "comboBoxTruckID"; diff --git a/ProjectGarage/Forms/FormDriver.cs b/ProjectGarage/Forms/FormDriver.cs index 25da12e..a630642 100644 --- a/ProjectGarage/Forms/FormDriver.cs +++ b/ProjectGarage/Forms/FormDriver.cs @@ -1,4 +1,7 @@ -using System; +using ProjectGarage.Entities; +using ProjectGarage.Entities.Enums; +using ProjectGarage.Repositories; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; @@ -12,9 +15,77 @@ namespace ProjectGarage.Forms { public partial class FormDriver : Form { - public FormDriver() + 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.Id;//TODO НЕ ЗАБУДЬ + _driverId = value; + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + } + } + + public FormDriver(IDriverRepository driverRepository) { InitializeComponent(); + _driverRepository = driverRepository ?? throw new ArgumentNullException(nameof(driverRepository)); + comboBoxTruckID.DataSource = Enum.GetValues(typeof(FuelType));//TODO НЕ ЗАБУДЬ + } + + private void ButtonSaveDriver_Click(object sender, EventArgs e) + { + try + { + if (string.IsNullOrWhiteSpace(textBoxFirstName.Text) || string.IsNullOrWhiteSpace(textBoxLastName.Text) + || comboBoxTruckID.SelectedIndex < 1)//TODO НЕ ЗАБУДЬ + { + 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) + { + + } + + private Driver CreateDriver(int id) => Driver.CreateDriver(id, textBoxFirstName.Text, + textBoxLastName.Text, textBoxPhoneNum.Text, (int)(FuelType)comboBoxTruckID.SelectedItem!);//TODO НЕ ЗАБУДЬ } + } diff --git a/ProjectGarage/Forms/FormTruck.cs b/ProjectGarage/Forms/FormTruck.cs index 8fb0c4b..50c16d2 100644 --- a/ProjectGarage/Forms/FormTruck.cs +++ b/ProjectGarage/Forms/FormTruck.cs @@ -1,14 +1,5 @@ using ProjectGarage.Entities; 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 {