From 3ad12b46fd9b08b3c333caf3e2eb4e9cd886ec0b Mon Sep 17 00:00:00 2001 From: Programmist73 Date: Wed, 10 May 2023 14:00:34 +0400 Subject: [PATCH] Another fixation. --- .../TransportCompany/FormClients.cs | 10 +- .../TransportCompany/FormCreateClient.cs | 35 ++++- .../TransportCompany/FormCreateTransport.cs | 36 +++++- .../FormTransferData.Designer.cs | 39 ++++++ .../TransportCompany/FormTransferData.cs | 20 +++ .../TransportCompany/FormTransferData.resx | 120 ++++++++++++++++++ .../TransportCompany/FormTransport.cs | 11 +- .../TransportCompany/FormTrucking.Designer.cs | 43 +++++-- .../TransportCompany/FormTrucking.cs | 60 +++++++-- TransportCompany/TransportCompany/Program.cs | 1 + 10 files changed, 346 insertions(+), 29 deletions(-) create mode 100644 TransportCompany/TransportCompany/FormTransferData.Designer.cs create mode 100644 TransportCompany/TransportCompany/FormTransferData.cs create mode 100644 TransportCompany/TransportCompany/FormTransferData.resx diff --git a/TransportCompany/TransportCompany/FormClients.cs b/TransportCompany/TransportCompany/FormClients.cs index 01cb396..7da5104 100644 --- a/TransportCompany/TransportCompany/FormClients.cs +++ b/TransportCompany/TransportCompany/FormClients.cs @@ -44,8 +44,9 @@ namespace TransportCompany { dataGridView.DataSource = list; dataGridView.Columns["Id"].Visible = false; - dataGridView.Columns[4].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + dataGridView.Columns["MongoId"].Visible = false; dataGridView.Columns[5].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + dataGridView.Columns[6].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; } _logger.LogInformation("Загрузка клиентов"); @@ -81,6 +82,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(); @@ -98,13 +101,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 ClientBindingModel { - Id = id + Id = id, + MongoId = mongoId })) { throw new Exception("Ошибка при удалении. Дополнительная информация в логах."); diff --git a/TransportCompany/TransportCompany/FormCreateClient.cs b/TransportCompany/TransportCompany/FormCreateClient.cs index a252665..87c30ac 100644 --- a/TransportCompany/TransportCompany/FormCreateClient.cs +++ b/TransportCompany/TransportCompany/FormCreateClient.cs @@ -22,9 +22,13 @@ namespace TransportCompany private int? _id; - public int Id { set { _id = value; } } + private string? _mongoId; - public FormCreateClient(ILogger logger, IClientLogic logicC) + public int Id { set { _id = value; } } + + public string MongoId { set { _mongoId = value; } } + + public FormCreateClient(ILogger logger, IClientLogic logicC) { InitializeComponent(); @@ -36,7 +40,31 @@ namespace TransportCompany private void FormCreateClient_Load(object sender, EventArgs e) { //проверка на заполнение поля id. Если оно заполнено, то пробуем получить запись и выести её на экран - if (_id.HasValue) + if (!string.IsNullOrEmpty(_mongoId)) + { + try + { + _logger.LogInformation("Получение клиента"); + + var view = _logicC.ReadElement(new ClientSearchModel { MongoId = _mongoId }); + + if (view != null) + { + textBoxName.Text = view.Name; + textBoxSurname.Text = view.Surname; + textBoxPatronymic.Text = view.Patronymic; + textBoxTelephone.Text = view.Telephone; + textBoxEmail.Text = view.Email; + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения компонента"); + + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + else if (_id.HasValue) { try { @@ -101,6 +129,7 @@ namespace TransportCompany var model = new ClientBindingModel { Id = 0, + MongoId = _mongoId, Name = textBoxName.Text, Surname = textBoxSurname.Text, Patronymic = textBoxPatronymic.Text, diff --git a/TransportCompany/TransportCompany/FormCreateTransport.cs b/TransportCompany/TransportCompany/FormCreateTransport.cs index f2c8f48..02b9e88 100644 --- a/TransportCompany/TransportCompany/FormCreateTransport.cs +++ b/TransportCompany/TransportCompany/FormCreateTransport.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 FormCreateTransport(ILogger logger, ITransportLogic logicT) { InitializeComponent(); @@ -35,7 +39,27 @@ namespace TransportCompany private void FormCreateTransport_Load(object sender, EventArgs e) { //проверка на заполнение поля id. Если оно заполнено, то пробуем получить запись и выести её на экран - if (_id.HasValue) + if (!string.IsNullOrEmpty(_mongoId)) + { + try + { + _logger.LogInformation("Получение транспорта"); + + var view = _logicT.ReadElement(new TransportSearchModel { MongoId = _mongoId }); + + if (view != null) + { + textBoxTransport.Text = view.Tranport; + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения транспорта"); + + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + else if (_id.HasValue) { try { @@ -65,14 +89,22 @@ namespace TransportCompany return; } + /*if (string.IsNullOrEmpty(textBoxTypeTransportation.Text)) + { + MessageBox.Show("Введите тип транспортировки", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + }*/ + _logger.LogInformation("Добавление транспорта"); try { var model = new TransportBindingModel { - Id = 0, + Id = _id ?? 0, + MongoId = _mongoId, Tranport = textBoxTransport.Text, + TransportationType = textBoxTypeTransportation.Text }; var operationResult = _id.HasValue ? _logicT.Update(model) : _logicT.Create(model); diff --git a/TransportCompany/TransportCompany/FormTransferData.Designer.cs b/TransportCompany/TransportCompany/FormTransferData.Designer.cs new file mode 100644 index 0000000..151ef0c --- /dev/null +++ b/TransportCompany/TransportCompany/FormTransferData.Designer.cs @@ -0,0 +1,39 @@ +namespace TransportCompany +{ + partial class FormTransferData + { + /// + /// 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); + } + + #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() + { + this.components = new System.ComponentModel.Container(); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(800, 450); + this.Text = "FormTransferData"; + } + + #endregion + } +} \ No newline at end of file diff --git a/TransportCompany/TransportCompany/FormTransferData.cs b/TransportCompany/TransportCompany/FormTransferData.cs new file mode 100644 index 0000000..c6348d5 --- /dev/null +++ b/TransportCompany/TransportCompany/FormTransferData.cs @@ -0,0 +1,20 @@ +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 TransportCompany +{ + public partial class FormTransferData : Form + { + public FormTransferData() + { + InitializeComponent(); + } + } +} diff --git a/TransportCompany/TransportCompany/FormTransferData.resx b/TransportCompany/TransportCompany/FormTransferData.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/TransportCompany/TransportCompany/FormTransferData.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/TransportCompany/TransportCompany/FormTransport.cs b/TransportCompany/TransportCompany/FormTransport.cs index 1bf398b..65da68b 100644 --- a/TransportCompany/TransportCompany/FormTransport.cs +++ b/TransportCompany/TransportCompany/FormTransport.cs @@ -42,7 +42,9 @@ namespace TransportCompany if (list != null) { dataGridView.DataSource = list; - dataGridView.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + dataGridView.Columns["MongoId"].Visible = false; + dataGridView.Columns["TransportationType"].Visible = false; + dataGridView.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; } _logger.LogInformation("Загрузка транспортных средств"); @@ -83,6 +85,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(); @@ -100,13 +104,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 (!_logic.Delete(new TransportBindingModel { - Id = id + Id = id, + MongoId = mongoId })) { throw new Exception("Ошибка при удалении. Дополнительная информация в логах."); diff --git a/TransportCompany/TransportCompany/FormTrucking.Designer.cs b/TransportCompany/TransportCompany/FormTrucking.Designer.cs index 754373a..1f86781 100644 --- a/TransportCompany/TransportCompany/FormTrucking.Designer.cs +++ b/TransportCompany/TransportCompany/FormTrucking.Designer.cs @@ -46,7 +46,10 @@ label1 = new Label(); checkBoxSorted = new CheckBox(); checkBoxForFilterMode = new CheckBox(); - createJsonFilesToolStripMenuItem = new ToolStripMenuItem(); + chooiceDBToolStripMenuItem = new ToolStripMenuItem(); + transferDataToolStripMenuItem = new ToolStripMenuItem(); + startPostgresqlToolStripMenuItem = new ToolStripMenuItem(); + startMongoDBToolStripMenuItem = new ToolStripMenuItem(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); menuStrip.SuspendLayout(); SuspendLayout(); @@ -74,7 +77,7 @@ // menuStrip // menuStrip.ImageScalingSize = new Size(20, 20); - menuStrip.Items.AddRange(new ToolStripItem[] { toolStripMenuItem, rndGenerationToolStripMenuItem, testTimeGetDataToolStripMenuItem, testComplexQueriesToolStripMenuItem, createJsonFilesToolStripMenuItem }); + menuStrip.Items.AddRange(new ToolStripItem[] { toolStripMenuItem, rndGenerationToolStripMenuItem, testTimeGetDataToolStripMenuItem, testComplexQueriesToolStripMenuItem, chooiceDBToolStripMenuItem }); menuStrip.Location = new Point(0, 0); menuStrip.Name = "menuStrip"; menuStrip.Padding = new Padding(6, 3, 0, 3); @@ -201,12 +204,33 @@ checkBoxForFilterMode.Text = "Включить режим фильтра"; checkBoxForFilterMode.UseVisualStyleBackColor = true; // - // createJsonFilesToolStripMenuItem + // chooiceDBToolStripMenuItem // - createJsonFilesToolStripMenuItem.Name = "createJsonFilesToolStripMenuItem"; - createJsonFilesToolStripMenuItem.Size = new Size(209, 24); - createJsonFilesToolStripMenuItem.Text = "Сформировать json файлы"; - createJsonFilesToolStripMenuItem.Click += CreateJsonFilesToolStripMenuItem_Click; + chooiceDBToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { transferDataToolStripMenuItem, startPostgresqlToolStripMenuItem, startMongoDBToolStripMenuItem }); + chooiceDBToolStripMenuItem.Name = "chooiceDBToolStripMenuItem"; + chooiceDBToolStripMenuItem.Size = new Size(93, 24); + chooiceDBToolStripMenuItem.Text = "Выбор БД"; + // + // transferDataToolStripMenuItem + // + transferDataToolStripMenuItem.Name = "transferDataToolStripMenuItem"; + transferDataToolStripMenuItem.Size = new Size(224, 26); + transferDataToolStripMenuItem.Text = "Передача данных"; + transferDataToolStripMenuItem.Click += TransferDataToolStripMenuItem_Click; + // + // startPostgresqlToolStripMenuItem + // + startPostgresqlToolStripMenuItem.Name = "startPostgresqlToolStripMenuItem"; + startPostgresqlToolStripMenuItem.Size = new Size(224, 26); + startPostgresqlToolStripMenuItem.Text = "Запуск Postgresql"; + startPostgresqlToolStripMenuItem.Click += StartPostgresqlToolStripMenuItem_Click; + // + // startMongoDBToolStripMenuItem + // + startMongoDBToolStripMenuItem.Name = "startMongoDBToolStripMenuItem"; + startMongoDBToolStripMenuItem.Size = new Size(224, 26); + startMongoDBToolStripMenuItem.Text = "Запуск MongoDB"; + startMongoDBToolStripMenuItem.Click += StartMongoDBToolStripMenuItem_Click; // // FormTrucking // @@ -265,6 +289,9 @@ private CheckBox checkBoxForFilterMode; private ToolStripMenuItem testTimeGetDataToolStripMenuItem; private ToolStripMenuItem testComplexQueriesToolStripMenuItem; - private ToolStripMenuItem createJsonFilesToolStripMenuItem; + private ToolStripMenuItem chooiceDBToolStripMenuItem; + private ToolStripMenuItem transferDataToolStripMenuItem; + private ToolStripMenuItem startPostgresqlToolStripMenuItem; + private ToolStripMenuItem startMongoDBToolStripMenuItem; } } \ No newline at end of file diff --git a/TransportCompany/TransportCompany/FormTrucking.cs b/TransportCompany/TransportCompany/FormTrucking.cs index 1fc5af2..3f32385 100644 --- a/TransportCompany/TransportCompany/FormTrucking.cs +++ b/TransportCompany/TransportCompany/FormTrucking.cs @@ -4,6 +4,7 @@ using System.Reflection; using System.Text; using System.Text.Json; using System.Windows.Forms; +using TransportCompanyBusinessLogic.BusinessLogic; using TransportCompanyContracts.BusinessLogicsContracts; using TransportCompanyContracts.SearchModels; using static System.Windows.Forms.VisualStyles.VisualStyleElement; @@ -14,15 +15,11 @@ namespace TransportCompany { private readonly ILogger _logger; - private readonly ITruckingLogic _truckingLogic; + private bool _mongo; - private readonly IClientLogic _clientLogic; + private ITruckingLogic _truckingLogic; - private readonly ICargoLogic _cargoLogic; - - private readonly ITransportLogic _transportLogic; - - private readonly ITransportationLogic _transportationLogic; + private IClientLogic _clientLogic; public FormTrucking(ILogger logger, ITruckingLogic truckingLogic, IClientLogic clientLogic, ICargoLogic cargoLogic, ITransportLogic transportLogic, ITransportationLogic transportationLogic) @@ -32,9 +29,6 @@ namespace TransportCompany _logger = logger; _truckingLogic = truckingLogic; _clientLogic = clientLogic; - _cargoLogic = cargoLogic; - _transportLogic = transportLogic; - _transportationLogic = transportationLogic; } private void FormMain_Load(object sender, EventArgs e) @@ -55,6 +49,18 @@ namespace TransportCompany if (list != null) { dataGridView.DataSource = list; + + if (list.Any(x => !string.IsNullOrEmpty(x.MongoId))) + { + dataGridView.Columns["Id"].Visible = false; + dataGridView.Columns["MongoId"].Visible = true; + } + else + { + dataGridView.Columns["Id"].Visible = true; + dataGridView.Columns["MongoId"].Visible = false; + } + dataGridView.Columns["ClientId"].Visible = false; dataGridView.Columns["CargoId"].Visible = false; dataGridView.Columns["TransportId"].Visible = false; @@ -200,7 +206,7 @@ namespace TransportCompany } } - private void CreateJsonFilesToolStripMenuItem_Click(object sender, EventArgs e) + /*private void CreateJsonFilesToolStripMenuItem_Click(object sender, EventArgs e) { var listTrucking = _truckingLogic.ReadList(null); var listClients = _clientLogic.ReadList(null); @@ -214,7 +220,7 @@ namespace TransportCompany string realPath; - for(int i = 0; i < listClients.Count; i++) + for (int i = 0; i < listClients.Count; i++) { realPath = path + "json" + i.ToString() + ".json"; @@ -226,6 +232,36 @@ namespace TransportCompany writetext.Close(); } + }*/ + + private void TransferDataToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormTransferData)); + + if (service is FormTransferData form) + { + form.ShowDialog(); + } + } + + private void StartPostgresqlToolStripMenuItem_Click(object sender, EventArgs e) + { + _mongo = false; + + Program.ConnectPostgres(); + + _truckingLogic = Program.ServiceProvider.GetService(typeof(ITruckingLogic)) as TruckingLogic; + _clientLogic = Program.ServiceProvider.GetService(typeof(IClientLogic)) as ClientLogic; + } + + private void StartMongoDBToolStripMenuItem_Click(object sender, EventArgs e) + { + _mongo = true; + + Program.ConnectMongo(); + + _truckingLogic = Program.ServiceProvider.GetService(typeof(ITruckingLogic)) as TruckingLogic; + _clientLogic = Program.ServiceProvider.GetService(typeof(IClientLogic)) as ClientLogic; } } } \ No newline at end of file diff --git a/TransportCompany/TransportCompany/Program.cs b/TransportCompany/TransportCompany/Program.cs index 07a682f..3485722 100644 --- a/TransportCompany/TransportCompany/Program.cs +++ b/TransportCompany/TransportCompany/Program.cs @@ -66,6 +66,7 @@ namespace TransportCompany services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); } // Postgresql