From 1c33862134ec3e8e185309c432e807316110f627 Mon Sep 17 00:00:00 2001 From: goldfest Date: Sat, 11 May 2024 23:55:39 +0400 Subject: [PATCH] MongoDB --- .../Transportlog/FormMain.Designer.cs | 184 ++++++++--- Transportlog/Transportlog/FormMain.cs | 197 +++++++++--- Transportlog/Transportlog/Program.cs | 52 +++- .../database/ImplementationMongoDB.cs | 286 ++++++++++++++++++ ...mentation.cs => ImplementationPostgres.cs} | 30 +- Transportlog/database/database.csproj | 1 + Transportlog/database/models.cs | 20 +- 7 files changed, 676 insertions(+), 94 deletions(-) create mode 100644 Transportlog/database/ImplementationMongoDB.cs rename Transportlog/database/{Implementation.cs => ImplementationPostgres.cs} (95%) diff --git a/Transportlog/Transportlog/FormMain.Designer.cs b/Transportlog/Transportlog/FormMain.Designer.cs index 7bc57b9..cf14c2d 100644 --- a/Transportlog/Transportlog/FormMain.Designer.cs +++ b/Transportlog/Transportlog/FormMain.Designer.cs @@ -40,21 +40,29 @@ cargoordersToolStripMenuItem = new ToolStripMenuItem(); buttonTest1 = new Button(); textBoxTest1 = new TextBox(); - textBoxTest2 = new TextBox(); + trackBarDB = new TrackBar(); + labelMongoDB = new Label(); + labelPostrges = new Label(); + button1 = new Button(); buttonTest2 = new Button(); + textBoxTest2 = new TextBox(); buttonTest3 = new Button(); textBoxTest3 = new TextBox(); + label1 = new Label(); + label2 = new Label(); + label3 = new Label(); menuStrip1.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)trackBarDB).BeginInit(); SuspendLayout(); // // menuStrip1 // - menuStrip1.BackColor = SystemColors.ActiveCaption; menuStrip1.ImageScalingSize = new Size(20, 20); menuStrip1.Items.AddRange(new ToolStripItem[] { tablesToolStripMenuItem }); menuStrip1.Location = new Point(0, 0); menuStrip1.Name = "menuStrip1"; - menuStrip1.Size = new Size(878, 24); + menuStrip1.Padding = new Padding(7, 3, 0, 3); + menuStrip1.Size = new Size(1125, 30); menuStrip1.TabIndex = 0; menuStrip1.Text = "menuStrip1"; // @@ -62,73 +70,74 @@ // tablesToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { cargoToolStripMenuItem, transportToolStripMenuItem, carModelToolStripMenuItem, branchToolStripMenuItem, clientToolStripMenuItem, rentalToolStripMenuItem, cargotransportToolStripMenuItem, cargoordersToolStripMenuItem }); tablesToolStripMenuItem.Name = "tablesToolStripMenuItem"; - tablesToolStripMenuItem.Size = new Size(50, 20); + tablesToolStripMenuItem.Size = new Size(63, 24); tablesToolStripMenuItem.Text = "tables"; // // cargoToolStripMenuItem // cargoToolStripMenuItem.Name = "cargoToolStripMenuItem"; - cargoToolStripMenuItem.Size = new Size(157, 22); + cargoToolStripMenuItem.Size = new Size(196, 26); cargoToolStripMenuItem.Text = "cargo"; cargoToolStripMenuItem.Click += cargoToolStripMenuItem_Click; // // transportToolStripMenuItem // transportToolStripMenuItem.Name = "transportToolStripMenuItem"; - transportToolStripMenuItem.Size = new Size(157, 22); + transportToolStripMenuItem.Size = new Size(196, 26); transportToolStripMenuItem.Text = "transport"; transportToolStripMenuItem.Click += transportToolStripMenuItem_Click; // // carModelToolStripMenuItem // carModelToolStripMenuItem.Name = "carModelToolStripMenuItem"; - carModelToolStripMenuItem.Size = new Size(157, 22); + carModelToolStripMenuItem.Size = new Size(196, 26); carModelToolStripMenuItem.Text = "warehouse"; carModelToolStripMenuItem.Click += warehouseToolStripMenuItem_Click; // // branchToolStripMenuItem // branchToolStripMenuItem.Name = "branchToolStripMenuItem"; - branchToolStripMenuItem.Size = new Size(157, 22); + branchToolStripMenuItem.Size = new Size(196, 26); branchToolStripMenuItem.Text = "driver"; branchToolStripMenuItem.Click += driverToolStripMenuItem_Click; // // clientToolStripMenuItem // clientToolStripMenuItem.Name = "clientToolStripMenuItem"; - clientToolStripMenuItem.Size = new Size(157, 22); + clientToolStripMenuItem.Size = new Size(196, 26); clientToolStripMenuItem.Text = "routes"; clientToolStripMenuItem.Click += routesToolStripMenuItem_Click; // // rentalToolStripMenuItem // rentalToolStripMenuItem.Name = "rentalToolStripMenuItem"; - rentalToolStripMenuItem.Size = new Size(157, 22); + rentalToolStripMenuItem.Size = new Size(196, 26); rentalToolStripMenuItem.Text = "orders"; rentalToolStripMenuItem.Click += ordersToolStripMenuItem_Click; // // cargotransportToolStripMenuItem // cargotransportToolStripMenuItem.Name = "cargotransportToolStripMenuItem"; - cargotransportToolStripMenuItem.Size = new Size(157, 22); + cargotransportToolStripMenuItem.Size = new Size(196, 26); cargotransportToolStripMenuItem.Text = "cargo_transport"; cargotransportToolStripMenuItem.Click += cargo_transportToolStripMenuItem_Click; // // cargoordersToolStripMenuItem // cargoordersToolStripMenuItem.Name = "cargoordersToolStripMenuItem"; - cargoordersToolStripMenuItem.Size = new Size(157, 22); + cargoordersToolStripMenuItem.Size = new Size(196, 26); cargoordersToolStripMenuItem.Text = "cargo_orders"; cargoordersToolStripMenuItem.Click += cargo_ordersToolStripMenuItem_Click; // // buttonTest1 // buttonTest1.BackColor = SystemColors.Highlight; - buttonTest1.Font = new Font("Microsoft Sans Serif", 19.8000011F, FontStyle.Regular, GraphicsUnit.Point); + buttonTest1.Font = new Font("Showcard Gothic", 19.8000011F, FontStyle.Regular, GraphicsUnit.Point); buttonTest1.ForeColor = SystemColors.ActiveCaption; - buttonTest1.Location = new Point(12, 73); + buttonTest1.Location = new Point(403, 298); + buttonTest1.Margin = new Padding(3, 4, 3, 4); buttonTest1.Name = "buttonTest1"; - buttonTest1.Size = new Size(232, 86); + buttonTest1.Size = new Size(202, 53); buttonTest1.TabIndex = 1; buttonTest1.Text = "Тест"; buttonTest1.UseVisualStyleBackColor = false; @@ -136,71 +145,161 @@ // // textBoxTest1 // - textBoxTest1.Location = new Point(12, 27); + textBoxTest1.Location = new Point(403, 96); + textBoxTest1.Margin = new Padding(3, 4, 3, 4); textBoxTest1.Multiline = true; textBoxTest1.Name = "textBoxTest1"; - textBoxTest1.Size = new Size(232, 40); + textBoxTest1.Size = new Size(202, 194); textBoxTest1.TabIndex = 2; // - // textBoxTest2 + // trackBarDB // - textBoxTest2.Location = new Point(326, 27); - textBoxTest2.Multiline = true; - textBoxTest2.Name = "textBoxTest2"; - textBoxTest2.Size = new Size(232, 40); - textBoxTest2.TabIndex = 3; + trackBarDB.LargeChange = 1; + trackBarDB.Location = new Point(27, 69); + trackBarDB.Margin = new Padding(3, 4, 3, 4); + trackBarDB.Maximum = 100; + trackBarDB.Name = "trackBarDB"; + trackBarDB.Orientation = Orientation.Vertical; + trackBarDB.Size = new Size(56, 85); + trackBarDB.TabIndex = 4; + trackBarDB.MouseUp += trackBarDB_MouseUp; + // + // labelMongoDB + // + labelMongoDB.AutoSize = true; + labelMongoDB.Font = new Font("Segoe UI", 20F, FontStyle.Regular, GraphicsUnit.Point); + labelMongoDB.Location = new Point(55, 59); + labelMongoDB.Name = "labelMongoDB"; + labelMongoDB.Size = new Size(173, 46); + labelMongoDB.TabIndex = 6; + labelMongoDB.Text = "MongoDB"; + // + // labelPostrges + // + labelPostrges.AutoSize = true; + labelPostrges.Font = new Font("Segoe UI", 20F, FontStyle.Regular, GraphicsUnit.Point); + labelPostrges.Location = new Point(55, 105); + labelPostrges.Name = "labelPostrges"; + labelPostrges.Size = new Size(148, 46); + labelPostrges.TabIndex = 7; + labelPostrges.Text = "Postrges"; + // + // button1 + // + button1.BackColor = Color.Red; + button1.Font = new Font("Showcard Gothic", 16.2F, FontStyle.Regular, GraphicsUnit.Point); + button1.ForeColor = SystemColors.ActiveCaption; + button1.Location = new Point(26, 272); + button1.Margin = new Padding(3, 4, 3, 4); + button1.Name = "button1"; + button1.Size = new Size(263, 91); + button1.TabIndex = 8; + button1.Text = "PSQL to MONGODB"; + button1.UseVisualStyleBackColor = false; + button1.Click += buttonTransferDataFromPostgresToMongo_Click; // // buttonTest2 // buttonTest2.BackColor = SystemColors.Highlight; - buttonTest2.Font = new Font("Microsoft Sans Serif", 19.8000011F, FontStyle.Regular, GraphicsUnit.Point); + buttonTest2.Font = new Font("Showcard Gothic", 19.8000011F, FontStyle.Regular, GraphicsUnit.Point); buttonTest2.ForeColor = SystemColors.ActiveCaption; - buttonTest2.Location = new Point(326, 73); + buttonTest2.Location = new Point(629, 298); + buttonTest2.Margin = new Padding(3, 4, 3, 4); buttonTest2.Name = "buttonTest2"; - buttonTest2.Size = new Size(232, 86); - buttonTest2.TabIndex = 4; + buttonTest2.Size = new Size(202, 53); + buttonTest2.TabIndex = 9; buttonTest2.Text = "Тест"; buttonTest2.UseVisualStyleBackColor = false; buttonTest2.Click += buttonTest2_Click; // + // textBoxTest2 + // + textBoxTest2.Location = new Point(629, 96); + textBoxTest2.Margin = new Padding(3, 4, 3, 4); + textBoxTest2.Multiline = true; + textBoxTest2.Name = "textBoxTest2"; + textBoxTest2.Size = new Size(202, 194); + textBoxTest2.TabIndex = 10; + // // buttonTest3 // buttonTest3.BackColor = SystemColors.Highlight; - buttonTest3.Font = new Font("Microsoft Sans Serif", 19.8000011F, FontStyle.Regular, GraphicsUnit.Point); + buttonTest3.Font = new Font("Showcard Gothic", 19.8000011F, FontStyle.Regular, GraphicsUnit.Point); buttonTest3.ForeColor = SystemColors.ActiveCaption; - buttonTest3.Location = new Point(635, 73); + buttonTest3.Location = new Point(853, 298); + buttonTest3.Margin = new Padding(3, 4, 3, 4); buttonTest3.Name = "buttonTest3"; - buttonTest3.Size = new Size(232, 86); - buttonTest3.TabIndex = 6; + buttonTest3.Size = new Size(202, 53); + buttonTest3.TabIndex = 11; buttonTest3.Text = "Тест"; buttonTest3.UseVisualStyleBackColor = false; buttonTest3.Click += buttonTest3_Click; // // textBoxTest3 // - textBoxTest3.Location = new Point(635, 27); + textBoxTest3.Location = new Point(853, 96); + textBoxTest3.Margin = new Padding(3, 4, 3, 4); textBoxTest3.Multiline = true; textBoxTest3.Name = "textBoxTest3"; - textBoxTest3.Size = new Size(232, 40); - textBoxTest3.TabIndex = 5; + textBoxTest3.Size = new Size(202, 194); + textBoxTest3.TabIndex = 12; + // + // label1 + // + label1.AutoSize = true; + label1.Font = new Font("Segoe UI", 20F, FontStyle.Regular, GraphicsUnit.Point); + label1.Location = new Point(457, 46); + label1.Name = "label1"; + label1.Size = new Size(93, 46); + label1.TabIndex = 13; + label1.Text = "del 1"; + // + // label2 + // + label2.AutoSize = true; + label2.Font = new Font("Segoe UI", 20F, FontStyle.Regular, GraphicsUnit.Point); + label2.Location = new Point(653, 46); + label2.Name = "label2"; + label2.Size = new Size(158, 46); + label2.TabIndex = 14; + label2.Text = "add 1000"; + // + // label3 + // + label3.AutoSize = true; + label3.Font = new Font("Segoe UI", 20F, FontStyle.Regular, GraphicsUnit.Point); + label3.Location = new Point(875, 46); + label3.Name = "label3"; + label3.Size = new Size(111, 46); + label3.TabIndex = 15; + label3.Text = "del 10"; // // FormMain // - AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(878, 167); - Controls.Add(buttonTest3); + ClientSize = new Size(1125, 387); + Controls.Add(label3); + Controls.Add(label2); + Controls.Add(label1); Controls.Add(textBoxTest3); - Controls.Add(buttonTest2); + Controls.Add(buttonTest3); Controls.Add(textBoxTest2); + Controls.Add(buttonTest2); + Controls.Add(button1); + Controls.Add(labelPostrges); + Controls.Add(labelMongoDB); + Controls.Add(trackBarDB); Controls.Add(textBoxTest1); Controls.Add(buttonTest1); Controls.Add(menuStrip1); MainMenuStrip = menuStrip1; + Margin = new Padding(3, 4, 3, 4); Name = "FormMain"; Text = "FormMain"; menuStrip1.ResumeLayout(false); menuStrip1.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)trackBarDB).EndInit(); ResumeLayout(false); PerformLayout(); } @@ -219,9 +318,16 @@ private ToolStripMenuItem transportToolStripMenuItem; private ToolStripMenuItem cargotransportToolStripMenuItem; private ToolStripMenuItem cargoordersToolStripMenuItem; - private TextBox textBoxTest2; + private TrackBar trackBarDB; + private Label labelMongoDB; + private Label labelPostrges; + private Button button1; private Button buttonTest2; + private TextBox textBoxTest2; private Button buttonTest3; private TextBox textBoxTest3; + private Label label1; + private Label label2; + private Label label3; } } \ No newline at end of file diff --git a/Transportlog/Transportlog/FormMain.cs b/Transportlog/Transportlog/FormMain.cs index 2277d1c..f8ec177 100644 --- a/Transportlog/Transportlog/FormMain.cs +++ b/Transportlog/Transportlog/FormMain.cs @@ -82,59 +82,178 @@ namespace Forms } } + private void buttonTest1_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(Abstractions)); - if (service is Abstractions bd) - { - var cargos = bd.GetCargos(); - int id = cargos.Last().id; - DateTime startTime = DateTime.Now; - bd.DeleteCargo(id); - DateTime endTime = DateTime.Now; + ImplementationPostgres bdPostgres = new ImplementationPostgres(); + ImplementationMongoDB bdMongoDB = new ImplementationMongoDB(); - textBoxTest1.Text = $"Время выполнения запроса: {(endTime - startTime).TotalMilliseconds} миллисекунд"; - } + var cargos1 = bdPostgres.GetCargos(); + int id1 = cargos1.Last().id; + var cargos2 = bdMongoDB.GetCargos(); + int id2 = cargos2.Last().id; + + DateTime startTime = DateTime.Now; + bdPostgres.DeleteCargo(id1); + DateTime endTime = DateTime.Now; + + textBoxTest1.Text = $"Время удаления на PostgreSQL: {(endTime - startTime).TotalMilliseconds} миллисекунд{Environment.NewLine}"; + + startTime = DateTime.Now; + bdPostgres.DeleteCargo(id2); + endTime = DateTime.Now; + + textBoxTest1.Text += $"Время удаления на MongoDB: {(endTime - startTime).TotalMilliseconds} миллисекунд"; + } private void buttonTest2_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(Abstractions)); - if (service is Abstractions bd) - { - DateTime startTime = DateTime.Now; - for (int i = 0; i < 1000; i++) - { - Cargo cargo = new Cargo - { - Name = "Груз " + i, - Description = "Описание груза " + i, - Weight = i * 10, - Type = "Тип груза " + i - }; - bd.AddCargo(cargo); - } - DateTime endTime = DateTime.Now; + ImplementationPostgres bdPostgres = new ImplementationPostgres(); + ImplementationMongoDB bdMongoDB = new ImplementationMongoDB(); - textBoxTest2.Text = $"Добавление 1000 строк выполнено за {(endTime - startTime).TotalMilliseconds} миллисекунд"; + // Добавляем 1000 грузов в PostgreSQL + DateTime startTime = DateTime.Now; + for (int i = 0; i < 1000; i++) + { + Cargo cargo = new Cargo + { + Name = "Груз " + i, + Description = "Описание груза " + i, + Weight = i * 10, + Type = "Тип груза " + i + }; + bdPostgres.AddCargo(cargo); } + DateTime endTime = DateTime.Now; + textBoxTest2.Text = $"Добавление 1000 строк в PostgreSQL выполнено за {(endTime - startTime).TotalMilliseconds} миллисекунд{Environment.NewLine}"; + + // Добавляем 1000 грузов в MongoDB + startTime = DateTime.Now; + for (int i = 0; i < 1000; i++) + { + Cargo cargo = new Cargo + { + Name = "Груз " + i, + Description = "Описание груза " + i, + Weight = i * 10, + Type = "Тип груза " + i + }; + bdMongoDB.AddCargo(cargo); + } + endTime = DateTime.Now; + textBoxTest2.Text += $"Добавление 1000 строк в MongoDB выполнено за {(endTime - startTime).TotalMilliseconds} миллисекунд"; } + private void buttonTest3_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(Abstractions)); - if (service is Abstractions bd) - { - DateTime startTime = DateTime.Now; - for (int i = 0; i < 1000; i++) - { - var cargos = bd.GetCargos(); - int id = cargos.Last().id; - bd.DeleteCargo(id); - } - DateTime endTime = DateTime.Now; + ImplementationPostgres bdPostgres = new ImplementationPostgres(); + ImplementationMongoDB bdMongoDB = new ImplementationMongoDB(); - textBoxTest3.Text = $"Удаление 1000 строк выполнено за {(endTime - startTime).TotalMilliseconds} миллисекунд"; + // Удаляем 10 грузов в PostgreSQL + DateTime startTime = DateTime.Now; + for (int i = 0; i < 10; i++) + { + var cargos = bdPostgres.GetCargos(); + int id = cargos.Last().id; + bdPostgres.DeleteCargo(id); + } + DateTime endTime = DateTime.Now; + textBoxTest3.Text = $"Удаление 10 строк в PostgreSQL выполнено за {(endTime - startTime).TotalMilliseconds} миллисекунд{Environment.NewLine}"; + + // Удаляем 10 грузов в MongoDB + startTime = DateTime.Now; + for (int i = 0; i < 10; i++) + { + var cargos = bdMongoDB.GetCargos(); + int id = cargos.Last().id; + bdMongoDB.DeleteCargo(id); + } + endTime = DateTime.Now; + textBoxTest3.Text += $"Удаление 10 строк в MongoDB выполнено за {(endTime - startTime).TotalMilliseconds} миллисекунд"; + } + + private void buttonTransferDataFromPostgresToMongo_Click(object sender, EventArgs e) + { + ImplementationMongoDB implementationMongoDB = new ImplementationMongoDB(); + + // очищаем всё + foreach (var it in implementationMongoDB.GetCargos()) + implementationMongoDB.DeleteCargo(it.id); + foreach (var it in implementationMongoDB.GetTransports()) + implementationMongoDB.DeleteTransport(it.id); + foreach (var it in implementationMongoDB.GetWarehouses()) + implementationMongoDB.DeleteWarehouse(it.id); + foreach (var it in implementationMongoDB.GetDrivers()) + implementationMongoDB.DeleteDriver(it.id); + foreach (var it in implementationMongoDB.GetCargoTransports()) + implementationMongoDB.DeleteCargoTransports(it.id); + foreach (var it in implementationMongoDB.GetRoutes()) + implementationMongoDB.DeleteRoutes(it.id); + foreach (var it in implementationMongoDB.GetOrders()) + implementationMongoDB.DeleteOrders(it.id); + foreach (var it in implementationMongoDB.GetCargoOrders()) + implementationMongoDB.DeleteCargoOrder(it.id); + + ImplementationPostgres implementationPostgres = new ImplementationPostgres(); + + // скачиваем из постгреса + var listCargos = implementationPostgres.GetCargos(); + var listTransports = implementationPostgres.GetTransports(); + var listWarehouses = implementationPostgres.GetWarehouses(); + var listDrivers = implementationPostgres.GetDrivers(); + var listCargoTransports = implementationPostgres.GetCargoTransports(); + var listRoutes = implementationPostgres.GetRoutes(); + var listOrders = implementationPostgres.GetOrders(); + var listCargoOrders = implementationPostgres.GetCargoOrders(); + + // вливаем данные монго дб + foreach (var it in listCargos) + implementationMongoDB.AddCargo(it); + foreach (var it in listTransports) + implementationMongoDB.AddTransport(it); + foreach (var it in listWarehouses) + implementationMongoDB.AddWarehouse(it); + foreach (var it in listDrivers) + implementationMongoDB.AddDriver(it); + foreach (var it in listCargoTransports) + implementationMongoDB.AddCargoTransports(it); + foreach (var it in listRoutes) + implementationMongoDB.AddRoutes(it); + foreach (var it in listOrders) + implementationMongoDB.AddOrders(it); + foreach (var it in listCargoOrders) + implementationMongoDB.AddCargoOrder(it); + + // забираем информацию о последовательностях + var listSequence = implementationPostgres.GetSequences(); + foreach (var it in listSequence) + if (it.Id == "cargo" || + it.Id == "transport" || + it.Id == "warehouse" || + it.Id == "driver" || + it.Id == "cargo_transport" || + it.Id == "routes" || + it.Id == "orders" || + it.Id == "cargo_order") + implementationMongoDB.UpdateSequence(it); + else + throw new Exception("неправильный id последовательности"); + MessageBox.Show("Данные успешно перенесены из PostgreSQL в MongoDB!"); + } + private void trackBarDB_MouseUp(object sender, MouseEventArgs e) + { + if (trackBarDB.Value > 50) + { + trackBarDB.Value = 100; + Program.ChangeDB(false); + } + else + { + trackBarDB.Value = 0; + Program.ChangeDB(true); } } + } } diff --git a/Transportlog/Transportlog/Program.cs b/Transportlog/Transportlog/Program.cs index a6c0c64..a33dd80 100644 --- a/Transportlog/Transportlog/Program.cs +++ b/Transportlog/Transportlog/Program.cs @@ -7,6 +7,30 @@ namespace Forms { private static ServiceProvider? _serviceProvider; public static ServiceProvider? ServiceProvider => _serviceProvider; + private static bool isPostgreSQL = true; + private static void ConfigureServices(ServiceCollection services) + { + if (isPostgreSQL) + { + services.AddTransient(); + } + else + { + services.AddTransient(); + } + services.AddTransient(); + + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + + } + /// /// The main entry point for the application. /// @@ -21,20 +45,22 @@ namespace Forms _serviceProvider = services.BuildServiceProvider(); Application.Run(_serviceProvider.GetRequiredService()); } - private static void ConfigureServices(ServiceCollection services) + + public static void ChangeDB() { - services.AddTransient(); - services.AddTransient(); - - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - + isPostgreSQL = !isPostgreSQL; + var services = new ServiceCollection(); + ConfigureServices(services); + _serviceProvider = services.BuildServiceProvider(); + } + public static void ChangeDB(bool newIsPostrgeSQL) + { + if (newIsPostrgeSQL == isPostgreSQL) + return; + isPostgreSQL = newIsPostrgeSQL; + var services = new ServiceCollection(); + ConfigureServices(services); + _serviceProvider = services.BuildServiceProvider(); } } } \ No newline at end of file diff --git a/Transportlog/database/ImplementationMongoDB.cs b/Transportlog/database/ImplementationMongoDB.cs new file mode 100644 index 0000000..ead8d94 --- /dev/null +++ b/Transportlog/database/ImplementationMongoDB.cs @@ -0,0 +1,286 @@ +using MongoDB.Driver; + +namespace database +{ + public class ImplementationMongoDB : Abstractions + { + private IMongoDatabase _database; + private IMongoCollection _cargoCollection; + private IMongoCollection _transportCollection; + private IMongoCollection _warehouseCollection; + private IMongoCollection _driverCollection; + private IMongoCollection _routesCollection; + private IMongoCollection _ordersCollection; + private IMongoCollection _cargoTransportCollection; + private IMongoCollection _cargoOrderCollection; + + private IMongoCollection _sequenceCollection; + + public ImplementationMongoDB() + { + var Transport = new MongoClient("mongodb://localhost:27017"); + _database = Transport.GetDatabase("transportlog"); + _cargoCollection = _database.GetCollection("Cargo"); + _transportCollection = _database.GetCollection("Transport"); + _warehouseCollection = _database.GetCollection("Warehouse"); + _driverCollection = _database.GetCollection("Driver"); + _routesCollection = _database.GetCollection("Routes"); + _ordersCollection = _database.GetCollection("Orders"); + _cargoTransportCollection = _database.GetCollection("Cargo_Transport"); + _cargoOrderCollection = _database.GetCollection("Cargo_Order"); + + _sequenceCollection = _database.GetCollection("sequence"); + } + + // CDUD + + // Cargo + public override void AddCargo(Cargo Cargo) + { + if (Cargo.id == 0) + { + Sequence sequence = _sequenceCollection.Find(seq => seq.Id == "Cargo").FirstOrDefault(); + sequence.Count++; + Cargo.id = sequence.Count; + _sequenceCollection.ReplaceOne(seq => seq.Id == "Cargo", sequence); + } + _cargoCollection.InsertOne(Cargo); + } + + public override List GetCargos() + { + return _cargoCollection.Find(_ => true).ToList(); + } + + public override Cargo GetCargoById(int id) + { + return _cargoCollection.Find(Cargo => Cargo.id == id).FirstOrDefault(); + } + + public override void UpdateCargo(Cargo Cargo) + { + _cargoCollection.ReplaceOne(c => c.id == Cargo.id, Cargo); + } + + public override void DeleteCargo(int id) + { + _cargoCollection.DeleteOne(Cargo => Cargo.id == id); + } + + // Transport + public override void AddTransport(Transport Transport) + { + if (Transport.id == 0) + { + Sequence sequence = _sequenceCollection.Find(seq => seq.Id == "Transport").FirstOrDefault(); + sequence.Count++; + Transport.id = sequence.Count; + _sequenceCollection.ReplaceOne(seq => seq.Id == "Transport", sequence); + } + _transportCollection.InsertOne(Transport); + } + + public override List GetTransports() + { + return _transportCollection.Find(_ => true).ToList(); + } + + public override Transport GetTransportById(int id) + { + return _transportCollection.Find(Transport => Transport.id == id).FirstOrDefault(); + } + + public override void UpdateTransport(Transport Transport) + { + _transportCollection.ReplaceOne(c => c.id == Transport.id, Transport); + } + + public override void DeleteTransport(int id) + { + _transportCollection.DeleteOne(Transport => Transport.id == id); + } + + // Warehouse + public override void AddWarehouse(Warehouse Warehouse) + { + if (Warehouse.id == 0) + { + Sequence sequence = _sequenceCollection.Find(seq => seq.Id == "Warehouse").FirstOrDefault(); + sequence.Count++; + Warehouse.id = sequence.Count; + _sequenceCollection.ReplaceOne(seq => seq.Id == "Warehouse", sequence); + } + _warehouseCollection.InsertOne(Warehouse); + } + + public override List GetWarehouses() + { + return _warehouseCollection.Find(_ => true).ToList(); + } + + public override Warehouse GetWarehouseById(int id) + { + return _warehouseCollection.Find(Warehouse => Warehouse.id == id).FirstOrDefault(); + } + + public override void UpdateWarehouse(Warehouse Warehouse) + { + _warehouseCollection.ReplaceOne(r => r.id == Warehouse.id, Warehouse); + } + + public override void DeleteWarehouse(int id) + { + _warehouseCollection.DeleteOne(Warehouse => Warehouse.id == id); + } + + // Driver + public override void AddDriver(Driver Driver) + { + if (Driver.id == 0) + { + Sequence sequence = _sequenceCollection.Find(seq => seq.Id == "car_model").FirstOrDefault(); + sequence.Count++; + Driver.id = sequence.Count; + _sequenceCollection.ReplaceOne(seq => seq.Id == "car_model", sequence); + } + _driverCollection.InsertOne(Driver); + } + + public override List GetDrivers() + { + return _driverCollection.Find(_ => true).ToList(); + } + + public override Driver GetDriverById(int id) + { + return _driverCollection.Find(model => model.id == id).FirstOrDefault(); + } + + public override void UpdateDriver(Driver Driver) + { + _driverCollection.ReplaceOne(m => m.id == Driver.id, Driver); + } + + public override void DeleteDriver(int id) + { + _driverCollection.DeleteOne(model => model.id == id); + } + + // Routes + public override void AddRoutes(Routes Routes) + { + if (Routes.id == 0) + { + Sequence sequence = _sequenceCollection.Find(seq => seq.Id == "Routes").FirstOrDefault(); + sequence.Count++; + Routes.id = sequence.Count; + _sequenceCollection.ReplaceOne(seq => seq.Id == "Routes", sequence); + } + _routesCollection.InsertOne(Routes); + } + + public override List GetRoutes() + { + return _routesCollection.Find(_ => true).ToList(); + } + + public override Routes GetRoutesById(int id) + { + return _routesCollection.Find(Routes => Routes.id == id).FirstOrDefault(); + } + + public override void UpdateRoutes(Routes Routes) + { + _routesCollection.ReplaceOne(b => b.id == Routes.id, Routes); + } + + public override void DeleteRoutes(int id) + { + _routesCollection.DeleteOne(Routes => Routes.id == id); + } + + // Orders + public override void AddOrders(Orders Orders) + { + _ordersCollection.InsertOne(Orders); + } + + public override List GetOrders() + { + return _ordersCollection.Find(_ => true).ToList(); + } + + public override Orders GetOrdersById(int id) + { + return _ordersCollection.Find(Orders => Orders.id == id).FirstOrDefault(); + } + + public override void UpdateOrders(Orders Orders) + { + _ordersCollection.ReplaceOne(s => s.id == Orders.id, Orders); + } + + public override void DeleteOrders(int id) + { + _ordersCollection.DeleteOne(Orders => Orders.id == id); + } + + // CargoTransport + public override void AddCargoTransports(CargoTransport CargoTransport) + { + _cargoTransportCollection.InsertOne(CargoTransport); + } + + public override List GetCargoTransports() + { + return _cargoTransportCollection.Find(_ => true).ToList(); + } + + public override CargoTransport GetCargoTransportsById(int id) + { + return _cargoTransportCollection.Find(CargoTransport => CargoTransport.id == id).FirstOrDefault(); + } + + public override void UpdateCargoTransports(CargoTransport CargoTransport) + { + _cargoTransportCollection.ReplaceOne(b => b.id == CargoTransport.id, CargoTransport); + } + + public override void DeleteCargoTransports(int id) + { + _cargoTransportCollection.DeleteOne(CargoTransport => CargoTransport.id == id); + } + + // CargoOrder + + public override void AddCargoOrder(CargoOrder CargoOrder) + { + _cargoOrderCollection.InsertOne(CargoOrder); + } + + public override List GetCargoOrders() + { + return _cargoOrderCollection.Find(_ => true).ToList(); + } + + public override CargoOrder GetCargoOrderById(int id) + { + return _cargoOrderCollection.Find(CargoOrder => CargoOrder.id == id).FirstOrDefault(); + } + + public override void UpdateCargoOrder(CargoOrder CargoOrder) + { + _cargoOrderCollection.ReplaceOne(b => b.id == CargoOrder.id, CargoOrder); + } + + public override void DeleteCargoOrder(int id) + { + _cargoOrderCollection.DeleteOne(CargoOrder => CargoOrder.id == id); + } + + public void UpdateSequence(Sequence sequence) + { + _sequenceCollection.ReplaceOne(seq => seq.Id == sequence.Id, sequence); + } + } +} \ No newline at end of file diff --git a/Transportlog/database/Implementation.cs b/Transportlog/database/ImplementationPostgres.cs similarity index 95% rename from Transportlog/database/Implementation.cs rename to Transportlog/database/ImplementationPostgres.cs index 14c1dcb..9ddeada 100644 --- a/Transportlog/database/Implementation.cs +++ b/Transportlog/database/ImplementationPostgres.cs @@ -2,11 +2,11 @@ namespace database { - public class Implementation : Abstractions + public class ImplementationPostgres : Abstractions { private NpgsqlConnection GetConnection() { - return new NpgsqlConnection("Host=localhost;Username=postgres;Password=5558;Database=transportlog;"); + return new NpgsqlConnection("Host=localhost;Username=postgres;Password=5558;Database=transports;"); } // CDUD операции для каждой сущности @@ -598,6 +598,32 @@ namespace database cmd.ExecuteNonQuery(); } + public List GetSequences() + { + List sequences = new List(); + + { + using var conn = GetConnection(); + conn.Open(); + string[] tableNames = { "Cargo", "Transport", "Warehouse", "Driver", "Cargo_Transport", "Routes", "Orders", "Cargo_Order" }; + + foreach (string tableName in tableNames) + { + using var cmd = new NpgsqlCommand($"SELECT MAX(id) FROM {tableName}", conn); + object result = cmd.ExecuteScalar(); + int maxId = result == null ? 0 : Convert.ToInt32(result); + + sequences.Add(new Sequence + { + Id = tableName.ToLower(), + Count = maxId + }); + } + } + + return sequences; + } + } } diff --git a/Transportlog/database/database.csproj b/Transportlog/database/database.csproj index 23b43f6..5b2fc8b 100644 --- a/Transportlog/database/database.csproj +++ b/Transportlog/database/database.csproj @@ -7,6 +7,7 @@ + diff --git a/Transportlog/database/models.cs b/Transportlog/database/models.cs index 14d0b35..86a6167 100644 --- a/Transportlog/database/models.cs +++ b/Transportlog/database/models.cs @@ -1,9 +1,13 @@ -namespace database +using MongoDB.Bson.Serialization.Attributes; +using MongoDB.Bson; + +namespace database { // Определение моделей данных public class Cargo { + [BsonRepresentation(BsonType.Int32)] public int id { get; set; } public string Name { get; set; } public string Description { get; set; } @@ -13,6 +17,7 @@ public class Transport { + [BsonRepresentation(BsonType.Int32)] public int id { get; set; } public string Type { get; set; } public decimal PayloadCapacity { get; set; } @@ -21,6 +26,7 @@ public class Warehouse { + [BsonRepresentation(BsonType.Int32)] public int id { get; set; } public string Name { get; set; } public string Address { get; set; } @@ -28,6 +34,7 @@ } public class Driver { + [BsonRepresentation(BsonType.Int32)] public int id { get; set; } public string Name { get; set; } public string Surname { get; set; } @@ -36,6 +43,7 @@ public class Routes { + [BsonRepresentation(BsonType.Int32)] public int id { get; set; } public string EndPoint { get; set; } public DateTime ArrivalTime { get; set; } @@ -45,6 +53,7 @@ } public class Orders { + [BsonRepresentation(BsonType.Int32)] public int id { get; set; } public string Status { get; set; } public DateTime OrderDate { get; set; } @@ -53,6 +62,7 @@ public class CargoTransport { + [BsonRepresentation(BsonType.Int32)] public int id { get; set; } public int CargoId { get; set; } public int TransportId { get; set; } @@ -60,6 +70,7 @@ public class CargoOrder { + [BsonRepresentation(BsonType.Int32)] public int id { get; set; } public int CargoId { get; set; } public int OrderId { get; set; } @@ -71,4 +82,11 @@ public string Text { get; set; } public int Id { get; set; } } + public class Sequence + { + [BsonRepresentation(BsonType.String)] + public string Id { get; set; } + public int Count { get; set; } + } + }