From b475dac5d54049c7e0b163b1321b17077715bff1 Mon Sep 17 00:00:00 2001 From: Programmist73 Date: Wed, 10 May 2023 13:13:06 +0400 Subject: [PATCH] First form common. --- .../TransportCompany/FormCargo.cs | 10 +- .../TransportCompany/FormCreateCargo.cs | 31 ++- .../FormCreateTransport.Designer.cs | 198 ++++++++++-------- .../TransportCompany/FormCreateTransport.cs | 138 ++++++------ 4 files changed, 215 insertions(+), 162 deletions(-) diff --git a/TransportCompany/TransportCompany/FormCargo.cs b/TransportCompany/TransportCompany/FormCargo.cs index fde8648..ebd09e1 100644 --- a/TransportCompany/TransportCompany/FormCargo.cs +++ b/TransportCompany/TransportCompany/FormCargo.cs @@ -42,7 +42,8 @@ namespace TransportCompany { dataGridView.DataSource = list; dataGridView.Columns["Id"].Visible = false; - dataGridView.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + dataGridView.Columns["MongoId"].Visible = false; + dataGridView.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; } _logger.LogInformation("Загрузка типов грузов"); @@ -78,6 +79,8 @@ namespace TransportCompany { form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + form.MongoId = Convert.ToString(dataGridView.SelectedRows[0].Cells["MongoId"].Value); + if (form.ShowDialog() == DialogResult.OK) { LoadData(); @@ -95,13 +98,16 @@ namespace TransportCompany { int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + string _mongoId = Convert.ToString(dataGridView.SelectedRows[0].Cells["MongoId"].Value); + _logger.LogInformation("Удаление типа груза"); try { if (!_logicC.Delete(new CargoBindingModel { - Id = id + Id = id, + MongoId = _mongoId })) { throw new Exception("Ошибка при удалении. Дополнительная информация в логах."); diff --git a/TransportCompany/TransportCompany/FormCreateCargo.cs b/TransportCompany/TransportCompany/FormCreateCargo.cs index bf8255a..55abaf9 100644 --- a/TransportCompany/TransportCompany/FormCreateCargo.cs +++ b/TransportCompany/TransportCompany/FormCreateCargo.cs @@ -22,8 +22,12 @@ namespace TransportCompany private int? _id; + private string? _mongoId; + public int Id { set { _id = value; } } + public string MongoId { set { _mongoId = value; } } + public FormCreateCargo(ILogger logger, ICargoLogic logicCg) { InitializeComponent(); @@ -35,7 +39,27 @@ namespace TransportCompany private void FormCreateCargo_Load(object sender, EventArgs e) { //проверка на заполнение поля id. Если оно заполнено, то пробуем получить запись и выести её на экран - if (_id.HasValue) + if (!string.IsNullOrEmpty(_mongoId)) + { + try + { + _logger.LogInformation("Получение типа груза"); + + var view = _logicCg.ReadElement(new CargoSearchModel { MongoId = _mongoId }); + + if (view != null) + { + textBoxCargo.Text = view.TypeCargo; + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения типа груза"); + + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + else if (_id.HasValue) { try { @@ -71,7 +95,8 @@ namespace TransportCompany { var model = new CargoBindingModel { - Id = 0, + Id = _id ?? 0, + MongoId = _mongoId, TypeCargo = textBoxCargo.Text }; @@ -79,7 +104,7 @@ namespace TransportCompany if (!operationResult) { - throw new Exception("Ошибка при сохранеии. Дополнительная информация в логах."); + throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); } MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); diff --git a/TransportCompany/TransportCompany/FormCreateTransport.Designer.cs b/TransportCompany/TransportCompany/FormCreateTransport.Designer.cs index 6abb63e..45f1209 100644 --- a/TransportCompany/TransportCompany/FormCreateTransport.Designer.cs +++ b/TransportCompany/TransportCompany/FormCreateTransport.Designer.cs @@ -1,96 +1,118 @@ namespace TransportCompany { - partial class FormCreateTransport - { - /// - /// Required designer variable. - /// - private System.ComponentModel.IContainer components = null; + partial class FormCreateTransport + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; - /// - /// Clean up any resources being used. - /// - /// true if managed resources should be disposed; otherwise, false. - protected override void Dispose(bool disposing) - { - if (disposing && (components != null)) - { - components.Dispose(); - } - base.Dispose(disposing); - } + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } - #region Windows Form Designer generated code + #region Windows Form Designer generated code - /// - /// Required method for Designer support - do not modify - /// the contents of this method with the code editor. - /// - private void InitializeComponent() - { - label1 = new Label(); - textBoxTransport = new TextBox(); - buttonCreate = new Button(); - buttonCancel = new Button(); - SuspendLayout(); - // - // label1 - // - label1.AutoSize = true; - label1.Location = new Point(36, 43); - label1.Name = "label1"; - label1.Size = new Size(122, 20); - label1.TabIndex = 0; - label1.Text = "Тип транспорта:"; - // - // textBoxTransport - // - textBoxTransport.Location = new Point(215, 40); - textBoxTransport.Name = "textBoxTransport"; - textBoxTransport.Size = new Size(254, 27); - textBoxTransport.TabIndex = 1; - // - // buttonCreate - // - buttonCreate.Location = new Point(262, 92); - buttonCreate.Name = "buttonCreate"; - buttonCreate.Size = new Size(94, 29); - buttonCreate.TabIndex = 2; - buttonCreate.Text = "Создать"; - buttonCreate.UseVisualStyleBackColor = true; - buttonCreate.Click += ButtonCreate_Click; - // - // buttonCancel - // - buttonCancel.Location = new Point(375, 92); - buttonCancel.Name = "buttonCancel"; - buttonCancel.Size = new Size(94, 29); - buttonCancel.TabIndex = 3; - buttonCancel.Text = "Отмена"; - buttonCancel.UseVisualStyleBackColor = true; - buttonCancel.Click += ButtonCancel_Click; - // - // FormCreateTransport - // - AutoScaleDimensions = new SizeF(8F, 20F); - AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(527, 155); - Controls.Add(buttonCancel); - Controls.Add(buttonCreate); - Controls.Add(textBoxTransport); - Controls.Add(label1); - Name = "FormCreateTransport"; - Text = "Транспорт"; - Load += FormCreateTransport_Load; - ResumeLayout(false); - PerformLayout(); - } + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + label1 = new Label(); + textBoxTransport = new TextBox(); + buttonCreate = new Button(); + buttonCancel = new Button(); + label2 = new Label(); + textBoxTypeTransportation = new TextBox(); + SuspendLayout(); + // + // label1 + // + label1.AutoSize = true; + label1.Location = new Point(36, 43); + label1.Name = "label1"; + label1.Size = new Size(122, 20); + label1.TabIndex = 0; + label1.Text = "Тип транспорта:"; + // + // textBoxTransport + // + textBoxTransport.Location = new Point(215, 40); + textBoxTransport.Name = "textBoxTransport"; + textBoxTransport.Size = new Size(254, 27); + textBoxTransport.TabIndex = 1; + // + // buttonCreate + // + buttonCreate.Location = new Point(262, 164); + buttonCreate.Name = "buttonCreate"; + buttonCreate.Size = new Size(94, 29); + buttonCreate.TabIndex = 2; + buttonCreate.Text = "Создать"; + buttonCreate.UseVisualStyleBackColor = true; + buttonCreate.Click += ButtonCreate_Click; + // + // buttonCancel + // + buttonCancel.Location = new Point(375, 164); + buttonCancel.Name = "buttonCancel"; + buttonCancel.Size = new Size(94, 29); + buttonCancel.TabIndex = 3; + buttonCancel.Text = "Отмена"; + buttonCancel.UseVisualStyleBackColor = true; + buttonCancel.Click += ButtonCancel_Click; + // + // label2 + // + label2.AutoSize = true; + label2.Location = new Point(36, 112); + label2.Name = "label2"; + label2.Size = new Size(116, 20); + label2.TabIndex = 4; + label2.Text = "Тип перевозки:"; + // + // textBoxTypeTransportation + // + textBoxTypeTransportation.Location = new Point(215, 109); + textBoxTypeTransportation.Name = "textBoxTypeTransportation"; + textBoxTypeTransportation.Size = new Size(254, 27); + textBoxTypeTransportation.TabIndex = 5; + // + // FormCreateTransport + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(527, 215); + Controls.Add(textBoxTypeTransportation); + Controls.Add(label2); + Controls.Add(buttonCancel); + Controls.Add(buttonCreate); + Controls.Add(textBoxTransport); + Controls.Add(label1); + Name = "FormCreateTransport"; + Text = "Транспорт"; + Load += FormCreateTransport_Load; + ResumeLayout(false); + PerformLayout(); + } - #endregion + #endregion - private Label label1; - private TextBox textBoxTransport; - private Button buttonCreate; - private Button buttonCancel; - } + private Label label1; + private TextBox textBoxTransport; + private Button buttonCreate; + private Button buttonCancel; + private Label label2; + private TextBox textBoxTypeTransportation; + } } \ No newline at end of file diff --git a/TransportCompany/TransportCompany/FormCreateTransport.cs b/TransportCompany/TransportCompany/FormCreateTransport.cs index dee8208..f2c8f48 100644 --- a/TransportCompany/TransportCompany/FormCreateTransport.cs +++ b/TransportCompany/TransportCompany/FormCreateTransport.cs @@ -14,91 +14,91 @@ using TransportCompanyContracts.SearchModels; namespace TransportCompany { - public partial class FormCreateTransport : Form - { - private readonly ILogger _logger; + public partial class FormCreateTransport : Form + { + private readonly ILogger _logger; - private readonly ITransportLogic _logicT; + private readonly ITransportLogic _logicT; - private int? _id; + private int? _id; - public int Id { set { _id = value; } } + public int Id { set { _id = value; } } - public FormCreateTransport(ILogger logger, ITransportLogic logicT) - { - InitializeComponent(); + public FormCreateTransport(ILogger logger, ITransportLogic logicT) + { + InitializeComponent(); - _logger = logger; - _logicT = logicT; - } + _logger = logger; + _logicT = logicT; + } - private void FormCreateTransport_Load(object sender, EventArgs e) - { - //проверка на заполнение поля id. Если оно заполнено, то пробуем получить запись и выести её на экран - if (_id.HasValue) - { - try - { - _logger.LogInformation("Получение транспорта"); + private void FormCreateTransport_Load(object sender, EventArgs e) + { + //проверка на заполнение поля id. Если оно заполнено, то пробуем получить запись и выести её на экран + if (_id.HasValue) + { + try + { + _logger.LogInformation("Получение транспорта"); - var view = _logicT.ReadElement(new TransportSearchModel { Id = _id.Value }); + var view = _logicT.ReadElement(new TransportSearchModel { Id = _id.Value }); - if (view != null) - { - textBoxTransport.Text = view.Tranport; - } - } - catch (Exception ex) - { - _logger.LogError(ex, "Ошибка получения транспорта"); + if (view != null) + { + textBoxTransport.Text = view.Tranport; + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения транспорта"); - MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - } - } + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } - private void ButtonCreate_Click(object sender, EventArgs e) - { - if (string.IsNullOrEmpty(textBoxTransport.Text)) - { - MessageBox.Show("Введите тип транспорта", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); - return; - } + private void ButtonCreate_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxTransport.Text)) + { + MessageBox.Show("Введите тип транспорта", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } - _logger.LogInformation("Добавление транспорта"); + _logger.LogInformation("Добавление транспорта"); - try - { - var model = new TransportBindingModel - { - Id = 0, - Tranport = textBoxTransport.Text, - }; + try + { + var model = new TransportBindingModel + { + Id = 0, + Tranport = textBoxTransport.Text, + }; - var operationResult = _id.HasValue ? _logicT.Update(model) : _logicT.Create(model); + var operationResult = _id.HasValue ? _logicT.Update(model) : _logicT.Create(model); - if (!operationResult) - { - throw new Exception("Ошибка при сохранеии. Дополнительная информация в логах."); - } + if (!operationResult) + { + throw new Exception("Ошибка при сохранеии. Дополнительная информация в логах."); + } - MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); - DialogResult = DialogResult.OK; + MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; - Close(); - } - catch (Exception ex) - { - _logger.LogError(ex, "Ошибка сохранения транспорта"); + Close(); + } + catch (Exception ex) + { + _logger.LogError(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) - { - DialogResult = DialogResult.Cancel; - Close(); - } - } + private void ButtonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + } }