diff --git a/ProjectAirline/Forms/FormPreparatoryWork.Designer.cs b/ProjectAirline/Forms/FormPreparatoryWork.Designer.cs index c0ad7e9..a8aedef 100644 --- a/ProjectAirline/Forms/FormPreparatoryWork.Designer.cs +++ b/ProjectAirline/Forms/FormPreparatoryWork.Designer.cs @@ -29,13 +29,15 @@ private void InitializeComponent() { labelFlightID = new Label(); - labelPlaneID = new Label(); - labelPreparatoryWorkStatus = new Label(); - comboBoxStatus = new ComboBox(); - comboBoxFlightId = new ComboBox(); - comboBoxPlaneId = new ComboBox(); + comboBoxFlight = new ComboBox(); buttonSave = new Button(); buttonCancel = new Button(); + dataGridView = new DataGridView(); + ColumnPlane = new DataGridViewComboBoxColumn(); + ColumnCount = new DataGridViewTextBoxColumn(); + comboBoxStatus = new ComboBox(); + labelStatus = new Label(); + ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); SuspendLayout(); // // labelFlightID @@ -47,64 +49,27 @@ labelFlightID.TabIndex = 0; labelFlightID.Text = "Рейс"; // - // labelPlaneID + // comboBoxFlight // - labelPlaneID.AutoSize = true; - labelPlaneID.Location = new Point(40, 80); - labelPlaneID.Name = "labelPlaneID"; - labelPlaneID.Size = new Size(55, 15); - labelPlaneID.TabIndex = 1; - labelPlaneID.Text = "Самолет"; - // - // labelPreparatoryWorkStatus - // - labelPreparatoryWorkStatus.AutoSize = true; - labelPreparatoryWorkStatus.Location = new Point(40, 130); - labelPreparatoryWorkStatus.Name = "labelPreparatoryWorkStatus"; - labelPreparatoryWorkStatus.Size = new Size(43, 15); - labelPreparatoryWorkStatus.TabIndex = 2; - labelPreparatoryWorkStatus.Text = "Статус"; - // - // comboBoxStatus - // - comboBoxStatus.DropDownStyle = ComboBoxStyle.DropDownList; - comboBoxStatus.FormattingEnabled = true; - comboBoxStatus.Location = new Point(142, 130); - comboBoxStatus.Name = "comboBoxStatus"; - comboBoxStatus.Size = new Size(121, 23); - comboBoxStatus.TabIndex = 3; - // - // comboBoxFlightId - // - comboBoxFlightId.DropDownStyle = ComboBoxStyle.DropDownList; - comboBoxFlightId.FormattingEnabled = true; - comboBoxFlightId.Location = new Point(142, 32); - comboBoxFlightId.Name = "comboBoxFlightId"; - comboBoxFlightId.Size = new Size(121, 23); - comboBoxFlightId.TabIndex = 4; - // - // comboBoxPlaneId - // - comboBoxPlaneId.DropDownStyle = ComboBoxStyle.DropDownList; - comboBoxPlaneId.FormattingEnabled = true; - comboBoxPlaneId.Location = new Point(142, 80); - comboBoxPlaneId.Name = "comboBoxPlaneId"; - comboBoxPlaneId.Size = new Size(121, 23); - comboBoxPlaneId.TabIndex = 5; + comboBoxFlight.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxFlight.FormattingEnabled = true; + comboBoxFlight.Location = new Point(142, 32); + comboBoxFlight.Name = "comboBoxFlight"; + comboBoxFlight.Size = new Size(121, 23); + comboBoxFlight.TabIndex = 4; // // buttonSave // - buttonSave.Location = new Point(40, 210); + buttonSave.Location = new Point(40, 311); buttonSave.Name = "buttonSave"; buttonSave.Size = new Size(75, 23); buttonSave.TabIndex = 6; buttonSave.Text = "Сохранить"; buttonSave.UseVisualStyleBackColor = true; - buttonSave.Click += ButtonSave_Click; // // buttonCancel // - buttonCancel.Location = new Point(188, 210); + buttonCancel.Location = new Point(188, 311); buttonCancel.Name = "buttonCancel"; buttonCancel.Size = new Size(75, 23); buttonCancel.TabIndex = 7; @@ -112,21 +77,63 @@ buttonCancel.UseVisualStyleBackColor = true; buttonCancel.Click += ButtonCancel_Click; // + // dataGridView + // + dataGridView.AllowUserToResizeColumns = false; + dataGridView.AllowUserToResizeRows = false; + dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; + dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + dataGridView.Columns.AddRange(new DataGridViewColumn[] { ColumnPlane, ColumnCount }); + dataGridView.Location = new Point(40, 130); + dataGridView.MultiSelect = false; + dataGridView.Name = "dataGridView"; + dataGridView.RowHeadersVisible = false; + dataGridView.Size = new Size(223, 150); + dataGridView.TabIndex = 0; + // + // ColumnPlane + // + ColumnPlane.HeaderText = "Самолет"; + ColumnPlane.Name = "ColumnPlane"; + // + // ColumnCount + // + ColumnCount.HeaderText = "Количество"; + ColumnCount.Name = "ColumnCount"; + // + // comboBoxStatus + // + comboBoxStatus.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStatus.FormattingEnabled = true; + comboBoxStatus.Location = new Point(142, 74); + comboBoxStatus.Name = "comboBoxStatus"; + comboBoxStatus.Size = new Size(121, 23); + comboBoxStatus.TabIndex = 9; + // + // labelStatus + // + labelStatus.AutoSize = true; + labelStatus.Location = new Point(40, 74); + labelStatus.Name = "labelStatus"; + labelStatus.Size = new Size(43, 15); + labelStatus.TabIndex = 10; + labelStatus.Text = "Статус"; + // // FormPreparatoryWork // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(330, 314); + ClientSize = new Size(290, 364); + Controls.Add(labelStatus); + Controls.Add(comboBoxStatus); + Controls.Add(dataGridView); Controls.Add(buttonCancel); Controls.Add(buttonSave); - Controls.Add(comboBoxPlaneId); - Controls.Add(comboBoxFlightId); - Controls.Add(comboBoxStatus); - Controls.Add(labelPreparatoryWorkStatus); - Controls.Add(labelPlaneID); + Controls.Add(comboBoxFlight); Controls.Add(labelFlightID); Name = "FormPreparatoryWork"; Text = "Подготовительные работы"; + ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); ResumeLayout(false); PerformLayout(); } @@ -134,12 +141,13 @@ #endregion private Label labelFlightID; - private Label labelPlaneID; - private Label labelPreparatoryWorkStatus; - private ComboBox comboBoxStatus; - private ComboBox comboBoxFlightId; - private ComboBox comboBoxPlaneId; + private ComboBox comboBoxFlight; private Button buttonSave; private Button buttonCancel; + private DataGridView dataGridView; + private ComboBox comboBoxStatus; + private Label labelStatus; + private DataGridViewComboBoxColumn ColumnPlane; + private DataGridViewTextBoxColumn ColumnCount; } } \ No newline at end of file diff --git a/ProjectAirline/Forms/FormPreparatoryWork.cs b/ProjectAirline/Forms/FormPreparatoryWork.cs index ee2beae..7d4dd11 100644 --- a/ProjectAirline/Forms/FormPreparatoryWork.cs +++ b/ProjectAirline/Forms/FormPreparatoryWork.cs @@ -1,93 +1,71 @@ -using ProjectAirline.Entities.Enums; -using ProjectAirline.Entities; -using ProjectAirline.Repositories; -using System; +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; -using ProjectAirline.Repositories.Implementations; +using ProjectAirline.Entities; +using ProjectAirline.Entities.Enums; +using ProjectAirline.Repositories; -namespace ProjectAirline.Forms; - -public partial class FormPreparatoryWork : Form +namespace ProjectAirline.Forms { - private readonly IPreparatoryWorkRepository _preparatoryWorkRepository; - private readonly IFlightRepository _flightRepository; - private readonly IPlaneRepository _planeRepository; - private int? _preparatoryWorkId; - - public int Id + public partial class FormPreparatoryWork : Form { - set + private readonly IPreparatoryWorkRepository _preparatoryWorkRepository; + + public FormPreparatoryWork(IPreparatoryWorkRepository preparatoryWorkRepository, IFlightRepository flightRepository, IPlaneRepository planeRepository) { + InitializeComponent(); + _preparatoryWorkRepository = preparatoryWorkRepository ?? throw new ArgumentNullException(nameof(preparatoryWorkRepository)); + ColumnPlane.DataSource = planeRepository.ReadPlanes(); + ColumnPlane.DisplayMember = "Name"; + ColumnPlane.ValueMember = "Id"; + + comboBoxStatus.DataSource = preparatoryWorkRepository.ReadPreparatoryWorks(); + } + + private void ButtonAdd_Click(object sender, EventArgs e) + { + if (comboBoxFlight.SelectedIndex < 0) + { + MessageBox.Show("Имеются незаполненные поля.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + try { - var preparatoryWork = _preparatoryWorkRepository.ReadPreparatoryWorkById(value); - if (preparatoryWork == null) - { - throw new InvalidDataException(nameof(preparatoryWork)); - } - comboBoxFlightId.SelectedItem = preparatoryWork.FlightId; - comboBoxStatus.SelectedItem = preparatoryWork.Status; - _preparatoryWorkId = value; + _preparatoryWorkRepository.CreatePreparatoryWork(PreparatoryWork.CreateOperation( + 0, + (int)comboBoxFlight.SelectedValue, + DateTime.Now, + DateTime.Now, + (PreparatoryWorkStatus)comboBoxStatus.SelectedItem, + CreateListFromDataGrid() + )); + Close(); } catch (Exception ex) { - MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error); - return; + MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error); } } - } - public FormPreparatoryWork(IPreparatoryWorkRepository preparatoryWorkRepository, IFlightRepository flightRepository, IPlaneRepository planeRepository) - { - InitializeComponent(); - _preparatoryWorkRepository = preparatoryWorkRepository ?? throw new ArgumentNullException(nameof(preparatoryWorkRepository)); - _flightRepository = flightRepository ?? throw new ArgumentNullException(nameof(_flightRepository)); - _planeRepository = planeRepository ?? throw new ArgumentNullException(nameof(_planeRepository)); - comboBoxFlightId.DataSource = _flightRepository.ReadFlights(); - comboBoxPlaneId.DataSource = _planeRepository.ReadPlanes(); - - comboBoxStatus.DataSource = Enum.GetValues(typeof(PreparatoryWorkStatus)); - } - - private void ButtonSave_Click(object sender, EventArgs e) - { - try + private void ButtonCancel_Click(object sender, EventArgs e) { - if (comboBoxFlightId.SelectedIndex < 0 || comboBoxPlaneId.SelectedIndex < 0 || comboBoxStatus.SelectedIndex < 0) - { - throw new Exception("Имеются незаполненные поля"); - } - //else - //{ - // _preparatoryWorkRepository.CreatePreparatoryWork(); - //} - Close(); } - catch (Exception ex) + + private List CreateListFromDataGrid() { - MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error); + var list = new List(); + foreach (DataGridViewRow row in dataGridView.Rows) + { + if (row.Cells["ColumnPlane"].Value == null || row.Cells["ColumnCount"].Value == null) + { + continue; + } + list.Add(PreparatoryWorkPlane.CreateOperation(0, 0, Convert.ToInt32(row.Cells["ColumnPlane"].Value), Convert.ToInt32(row.Cells["ColumnCount"].Value))); + } + return list; } } - - private void ButtonCancel_Click(object sender, EventArgs e) => Close(); - - //private PreparatoryWork CreatePreparatoryWork(int id) - //{ - // return PreparatoryWork.CreateOperation( - // id, - // (int)comboBoxFlightId.SelectedItem, - // DateTime.Now, - // DateTime.Now.AddHours(1), - // (PreparatoryWorkStatus)comboBoxStatus.SelectedItem - - // ); - //} -} +} \ No newline at end of file diff --git a/ProjectAirline/Forms/FormPreparatoryWork.resx b/ProjectAirline/Forms/FormPreparatoryWork.resx index af32865..a820869 100644 --- a/ProjectAirline/Forms/FormPreparatoryWork.resx +++ b/ProjectAirline/Forms/FormPreparatoryWork.resx @@ -117,4 +117,10 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + True + + + True + \ No newline at end of file diff --git a/ProjectAirline/Forms/FormPreparatoryWorks.Designer.cs b/ProjectAirline/Forms/FormPreparatoryWorks.Designer.cs index 2685e24..3f1805e 100644 --- a/ProjectAirline/Forms/FormPreparatoryWorks.Designer.cs +++ b/ProjectAirline/Forms/FormPreparatoryWorks.Designer.cs @@ -28,59 +28,14 @@ /// private void InitializeComponent() { - panel = new Panel(); - buttonUpd = new Button(); - buttonDel = new Button(); - buttonAdd = new Button(); dataGridViewData = new DataGridView(); - panel.SuspendLayout(); + buttonAdd = new Button(); + buttonDel = new Button(); + panel = new Panel(); ((System.ComponentModel.ISupportInitialize)dataGridViewData).BeginInit(); + panel.SuspendLayout(); SuspendLayout(); // - // panel - // - panel.Controls.Add(buttonUpd); - panel.Controls.Add(buttonDel); - panel.Controls.Add(buttonAdd); - panel.Dock = DockStyle.Right; - panel.Location = new Point(600, 0); - panel.Name = "panel"; - panel.Size = new Size(200, 450); - panel.TabIndex = 0; - // - //// buttonUpd - //// - //buttonUpd.BackgroundImage = Properties.Resources.Antu_qtdesigner_svg; - //buttonUpd.BackgroundImageLayout = ImageLayout.Zoom; - //buttonUpd.Location = new Point(51, 148); - //buttonUpd.Name = "buttonUpd"; - //buttonUpd.Size = new Size(79, 65); - //buttonUpd.TabIndex = 2; - //buttonUpd.UseVisualStyleBackColor = true; - //buttonUpd.Click += ButtonUpd_Click; - //// - //// buttonDel - //// - //buttonDel.BackgroundImage = Properties.Resources.kisspng_computer_icons_download_5b218289889485_4804831415289227615594; - //buttonDel.BackgroundImageLayout = ImageLayout.Stretch; - //buttonDel.Location = new Point(51, 248); - //buttonDel.Name = "buttonDel"; - //buttonDel.Size = new Size(79, 65); - //buttonDel.TabIndex = 1; - //buttonDel.UseVisualStyleBackColor = true; - //buttonDel.Click += ButtonDel_Click; - //// - //// buttonAdd - //// - //buttonAdd.BackgroundImage = Properties.Resources.Fairytale_button_add_svg; - //buttonAdd.BackgroundImageLayout = ImageLayout.Zoom; - //buttonAdd.Location = new Point(51, 44); - //buttonAdd.Name = "buttonAdd"; - //buttonAdd.Size = new Size(79, 65); - //buttonAdd.TabIndex = 0; - //buttonAdd.UseVisualStyleBackColor = true; - //buttonAdd.Click += ButtonAdd_Click; - // // dataGridViewData // dataGridViewData.AllowUserToAddRows = false; @@ -89,37 +44,66 @@ dataGridViewData.AllowUserToResizeRows = false; dataGridViewData.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; dataGridViewData.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; - dataGridViewData.ColumnHeadersVisible = false; - dataGridViewData.Dock = DockStyle.Fill; + dataGridViewData.Dock = DockStyle.Left; dataGridViewData.Location = new Point(0, 0); dataGridViewData.MultiSelect = false; dataGridViewData.Name = "dataGridViewData"; dataGridViewData.ReadOnly = true; dataGridViewData.SelectionMode = DataGridViewSelectionMode.FullRowSelect; - dataGridViewData.Size = new Size(600, 450); - dataGridViewData.TabIndex = 1; + dataGridViewData.Size = new Size(616, 450); + dataGridViewData.TabIndex = 0; + // + // buttonAdd + // + buttonAdd.BackgroundImage = Properties.Resources.Fairytale_button_add_svg; + buttonAdd.BackgroundImageLayout = ImageLayout.Zoom; + buttonAdd.Location = new Point(25, 15); + buttonAdd.Name = "buttonAdd"; + buttonAdd.Size = new Size(75, 65); + buttonAdd.TabIndex = 1; + buttonAdd.UseVisualStyleBackColor = true; + buttonAdd.Click += ButtonAdd_Click; + // + // buttonDel + // + buttonDel.BackgroundImage = Properties.Resources.kisspng_computer_icons_download_5b218289889485_4804831415289227615594; + buttonDel.BackgroundImageLayout = ImageLayout.Stretch; + buttonDel.Location = new Point(25, 115); + buttonDel.Name = "buttonDel"; + buttonDel.Size = new Size(75, 65); + buttonDel.TabIndex = 3; + buttonDel.UseVisualStyleBackColor = true; + buttonDel.Click += ButtonRemove_Click; + // + // panel + // + panel.Controls.Add(buttonAdd); + panel.Controls.Add(buttonDel); + panel.Dock = DockStyle.Right; + panel.Location = new Point(613, 0); + panel.Name = "panel"; + panel.Size = new Size(187, 450); + panel.TabIndex = 4; // // FormPreparatoryWorks // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(800, 450); - Controls.Add(dataGridViewData); Controls.Add(panel); + Controls.Add(dataGridViewData); Name = "FormPreparatoryWorks"; - StartPosition = FormStartPosition.CenterParent; Text = "Подготовительные работы"; - panel.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)dataGridViewData).EndInit(); + panel.ResumeLayout(false); ResumeLayout(false); } #endregion - private Panel panel; - private Button buttonUpd; - private Button buttonDel; - private Button buttonAdd; private DataGridView dataGridViewData; + private Button buttonAdd; + private Button buttonDel; + private Panel panel; } } \ No newline at end of file diff --git a/ProjectAirline/Forms/FormPreparatoryWorks.cs b/ProjectAirline/Forms/FormPreparatoryWorks.cs index 7428baa..9b63d4d 100644 --- a/ProjectAirline/Forms/FormPreparatoryWorks.cs +++ b/ProjectAirline/Forms/FormPreparatoryWorks.cs @@ -1,6 +1,5 @@ -using ProjectAirline.Entities.Enums; -using ProjectAirline.Entities; -using ProjectAirline.Repositories; +using ProjectAirline.Repositories; +using ProjectAirline.Repositories.Implementations; using System; using System.Collections.Generic; using System.ComponentModel; @@ -10,177 +9,82 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using System.Xml.Linq; using Unity; namespace ProjectAirline.Forms; -//public partial class FormPreparatoryWorks : Form -//{ -// private readonly IUnityContainer _container; -// private readonly IPreparatoryWorkRepository _preparatoryWorkRepository; - -// public FormPreparatoryWorks(IUnityContainer container, IPreparatoryWorkRepository preparatoryWorkRepository) -// { -// InitializeComponent(); -// _container = container ?? throw new ArgumentNullException(nameof(container)); -// _preparatoryWorkRepository = preparatoryWorkRepository ?? throw new ArgumentNullException(nameof(preparatoryWorkRepository)); -// } - -// private void FormPreparatoryWorks_Load(object sender, EventArgs e) -// { -// try -// { -// LoadList(); -// } -// catch (Exception ex) -// { -// MessageBox.Show(ex.Message, "Ошибка при загрузке", MessageBoxButtons.OK, MessageBoxIcon.Error); -// } -// } - -// private void ButtonAdd_Click(object sender, EventArgs e) -// { -// try -// { -// _container.Resolve().ShowDialog(); -// LoadList(); -// } -// catch (Exception ex) -// { -// MessageBox.Show(ex.Message, "Ошибка при добавлении", MessageBoxButtons.OK, MessageBoxIcon.Error); -// } -// } - -// private void ButtonUpd_Click(object sender, EventArgs e) -// { -// if (!TryGetIdentifierFromSelectedRow(out var findId)) -// { -// return; -// } -// try -// { -// var form = _container.Resolve(); -// form.Id = findId; -// form.ShowDialog(); -// LoadList(); -// } -// catch (Exception ex) -// { -// MessageBox.Show(ex.Message, "Ошибка при изменении", MessageBoxButtons.OK, MessageBoxIcon.Error); -// } -// } - -// private void ButtonDel_Click(object sender, EventArgs e) -// { -// if (!TryGetIdentifierFromSelectedRow(out var findId)) -// { -// return; -// } -// if (MessageBox.Show("Удалить запись?", "Удаление", MessageBoxButtons.YesNo) != DialogResult.Yes) -// { -// return; -// } -// try -// { -// _preparatoryWorkRepository.DeletePreparatoryWork(findId); -// LoadList(); -// } -// catch (Exception ex) -// { -// MessageBox.Show(ex.Message, "Ошибка при удалении", MessageBoxButtons.OK, MessageBoxIcon.Error); -// } -// } - -// private void LoadList() => dataGridViewData.DataSource = _preparatoryWorkRepository.ReadPreparatoryWorks(); - -// private bool TryGetIdentifierFromSelectedRow(out int id) -// { -// id = 0; -// if (dataGridViewData.SelectedRows.Count < 1) -// { -// MessageBox.Show("Нет выбранной записи", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); -// return false; -// } -// id = Convert.ToInt32(dataGridViewData.SelectedRows[0].Cells["Id"].Value); -// return true; -// } -//} public partial class FormPreparatoryWorks : Form { + private readonly IUnityContainer _container; private readonly IPreparatoryWorkRepository _preparatoryWorkRepository; - private readonly IPlaneRepository _planeRepository; - public FormPreparatoryWorks(IPreparatoryWorkRepository preparatoryWorkRepository, IPlaneRepository planeRepository) + public FormPreparatoryWorks(IUnityContainer container, IPreparatoryWorkRepository preparatoryWorkRepository) { InitializeComponent(); + _container = container ?? throw new ArgumentNullException(nameof(container)); _preparatoryWorkRepository = preparatoryWorkRepository ?? throw new ArgumentNullException(nameof(preparatoryWorkRepository)); - _planeRepository = planeRepository ?? throw new ArgumentNullException(nameof(planeRepository)); - - - dataGridViewData.AutoGenerateColumns = false; - dataGridViewData.Columns.Add(new DataGridViewComboBoxColumn - { - Name = "ColumnPlane", - HeaderText = "Самолет", - DataSource = _planeRepository.ReadPlanes(), - DisplayMember = "Name", - ValueMember = "Id", - DataPropertyName = "PlaneId" - }); - dataGridViewData.Columns.Add(new DataGridViewTextBoxColumn - { - Name = "ColumnCount", - HeaderText = "Количество", - DataPropertyName = "Count" - }); } - private void ButtonSave_Click(object sender, EventArgs e) + private void FormPreparatoryWorks_Load(object sender, EventArgs e) { try { - if (dataGridViewData.RowCount < 1) - { - throw new Exception("Имеются незаполненные поля"); - } - - var preparatoryWorkPlanes = CreateListPreparatoryWorkPlanesFromDataGrid(); - - var preparatoryWork = PreparatoryWork.CreateOperation( - 0, - -1, - DateTime.Now, - DateTime.Now, - PreparatoryWorkStatus.None, - preparatoryWorkPlanes - ); - - _preparatoryWorkRepository.CreatePreparatoryWork(preparatoryWork); - Close(); + LoadList(); } catch (Exception ex) { - MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show(ex.Message, "Ошибка при загрузке", MessageBoxButtons.OK, MessageBoxIcon.Error); } } - private void ButtonCancel_Click(object sender, EventArgs e) => Close(); - - private List CreateListPreparatoryWorkPlanesFromDataGrid() + private void ButtonAdd_Click(object sender, EventArgs e) { - var list = new List(); - foreach (DataGridViewRow row in dataGridViewData.Rows) + try { - if (row.Cells["ColumnPlane"].Value == null || row.Cells["ColumnCount"].Value == null) - { - continue; - } - - var planeId = Convert.ToInt32(row.Cells["ColumnPlane"].Value); - var count = Convert.ToInt32(row.Cells["ColumnCount"].Value); - - list.Add(PreparatoryWorkPlane.CreateOperation(0, -1, planeId, count)); + _container.Resolve().ShowDialog(); + LoadList(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка при добавлении", MessageBoxButtons.OK, MessageBoxIcon.Error); } - return list; } -} + + private void ButtonRemove_Click(object sender, EventArgs e) + { + if (!TryGetIdentifierFromSelectedRow(out var findId)) + { + return; + } + if (MessageBox.Show("Удалить запись?", "Удаление", MessageBoxButtons.YesNo) != DialogResult.Yes) + { + return; + } + + try + { + _preparatoryWorkRepository.DeletePreparatoryWork(findId); + LoadList(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка при удалении", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void LoadList() => dataGridViewData.DataSource = _preparatoryWorkRepository.ReadPreparatoryWorks(); + + private bool TryGetIdentifierFromSelectedRow(out int id) + { + id = 0; + if (dataGridViewData.SelectedRows.Count < 1) + { + MessageBox.Show("Нет выбранной записи", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return false; + } + + id = Convert.ToInt32(dataGridViewData.SelectedRows[0].Cells["Id"].Value); + return true; + } +} \ No newline at end of file