WIP: хочу знать сколько тут изменений #2
274
SUBD_Car_rent/Forms/FormMain.Designer.cs
generated
274
SUBD_Car_rent/Forms/FormMain.Designer.cs
generated
@ -20,115 +20,170 @@
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
#region Windows Form Designer generated code
|
||||
#region Windows Form Designer generated code
|
||||
|
||||
/// <summary>
|
||||
/// Required method for Designer support - do not modify
|
||||
/// the contents of this method with the code editor.
|
||||
/// </summary>
|
||||
private void InitializeComponent()
|
||||
{
|
||||
menuStrip1 = new MenuStrip();
|
||||
tablesToolStripMenuItem = new ToolStripMenuItem();
|
||||
carModelToolStripMenuItem = new ToolStripMenuItem();
|
||||
carToolStripMenuItem = new ToolStripMenuItem();
|
||||
branchToolStripMenuItem = new ToolStripMenuItem();
|
||||
clientToolStripMenuItem = new ToolStripMenuItem();
|
||||
rentalToolStripMenuItem = new ToolStripMenuItem();
|
||||
buttonTest = new Button();
|
||||
textBoxTest = new TextBox();
|
||||
menuStrip1.SuspendLayout();
|
||||
SuspendLayout();
|
||||
//
|
||||
// menuStrip1
|
||||
//
|
||||
menuStrip1.Items.AddRange(new ToolStripItem[] { tablesToolStripMenuItem });
|
||||
menuStrip1.Location = new Point(0, 0);
|
||||
menuStrip1.Name = "menuStrip1";
|
||||
menuStrip1.Size = new Size(435, 24);
|
||||
menuStrip1.TabIndex = 0;
|
||||
menuStrip1.Text = "menuStrip1";
|
||||
//
|
||||
// tablesToolStripMenuItem
|
||||
//
|
||||
tablesToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { carModelToolStripMenuItem, carToolStripMenuItem, branchToolStripMenuItem, clientToolStripMenuItem, rentalToolStripMenuItem });
|
||||
tablesToolStripMenuItem.Name = "tablesToolStripMenuItem";
|
||||
tablesToolStripMenuItem.Size = new Size(50, 20);
|
||||
tablesToolStripMenuItem.Text = "tables";
|
||||
//
|
||||
// carModelToolStripMenuItem
|
||||
//
|
||||
carModelToolStripMenuItem.Name = "carModelToolStripMenuItem";
|
||||
carModelToolStripMenuItem.Size = new Size(127, 22);
|
||||
carModelToolStripMenuItem.Text = "car model";
|
||||
carModelToolStripMenuItem.Click += carModelToolStripMenuItem_Click;
|
||||
//
|
||||
// carToolStripMenuItem
|
||||
//
|
||||
carToolStripMenuItem.Name = "carToolStripMenuItem";
|
||||
carToolStripMenuItem.Size = new Size(127, 22);
|
||||
carToolStripMenuItem.Text = "car";
|
||||
carToolStripMenuItem.Click += carToolStripMenuItem_Click;
|
||||
//
|
||||
// branchToolStripMenuItem
|
||||
//
|
||||
branchToolStripMenuItem.Name = "branchToolStripMenuItem";
|
||||
branchToolStripMenuItem.Size = new Size(127, 22);
|
||||
branchToolStripMenuItem.Text = "branch";
|
||||
branchToolStripMenuItem.Click += branchToolStripMenuItem_Click;
|
||||
//
|
||||
// clientToolStripMenuItem
|
||||
//
|
||||
clientToolStripMenuItem.Name = "clientToolStripMenuItem";
|
||||
clientToolStripMenuItem.Size = new Size(127, 22);
|
||||
clientToolStripMenuItem.Text = "client";
|
||||
clientToolStripMenuItem.Click += clientToolStripMenuItem_Click;
|
||||
//
|
||||
// rentalToolStripMenuItem
|
||||
//
|
||||
rentalToolStripMenuItem.Name = "rentalToolStripMenuItem";
|
||||
rentalToolStripMenuItem.Size = new Size(127, 22);
|
||||
rentalToolStripMenuItem.Text = "rental";
|
||||
rentalToolStripMenuItem.Click += rentalToolStripMenuItem_Click;
|
||||
//
|
||||
// buttonTest
|
||||
//
|
||||
buttonTest.Location = new Point(24, 50);
|
||||
buttonTest.Name = "buttonTest";
|
||||
buttonTest.Size = new Size(75, 23);
|
||||
buttonTest.TabIndex = 1;
|
||||
buttonTest.Text = "buttonTest";
|
||||
buttonTest.UseVisualStyleBackColor = true;
|
||||
buttonTest.Click += buttonTest_Click;
|
||||
//
|
||||
// textBoxTest
|
||||
//
|
||||
textBoxTest.Location = new Point(121, 51);
|
||||
textBoxTest.Multiline = true;
|
||||
textBoxTest.Name = "textBoxTest";
|
||||
textBoxTest.Size = new Size(251, 75);
|
||||
textBoxTest.TabIndex = 2;
|
||||
//
|
||||
// FormMain
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(7F, 15F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(435, 167);
|
||||
Controls.Add(textBoxTest);
|
||||
Controls.Add(buttonTest);
|
||||
Controls.Add(menuStrip1);
|
||||
MainMenuStrip = menuStrip1;
|
||||
Name = "FormMain";
|
||||
Text = "FormMain";
|
||||
menuStrip1.ResumeLayout(false);
|
||||
menuStrip1.PerformLayout();
|
||||
ResumeLayout(false);
|
||||
PerformLayout();
|
||||
}
|
||||
/// <summary>
|
||||
/// Required method for Designer support - do not modify
|
||||
/// the contents of this method with the code editor.
|
||||
/// </summary>
|
||||
private void InitializeComponent()
|
||||
{
|
||||
menuStrip1 = new MenuStrip();
|
||||
tablesToolStripMenuItem = new ToolStripMenuItem();
|
||||
carModelToolStripMenuItem = new ToolStripMenuItem();
|
||||
carToolStripMenuItem = new ToolStripMenuItem();
|
||||
branchToolStripMenuItem = new ToolStripMenuItem();
|
||||
clientToolStripMenuItem = new ToolStripMenuItem();
|
||||
rentalToolStripMenuItem = new ToolStripMenuItem();
|
||||
buttonTest = new Button();
|
||||
textBoxTest = new TextBox();
|
||||
trackBarDB = new TrackBar();
|
||||
labelPostrges = new Label();
|
||||
labelMongoDB = new Label();
|
||||
buttonTransferDataFromPostgresToMongo = new TextBox();
|
||||
menuStrip1.SuspendLayout();
|
||||
((System.ComponentModel.ISupportInitialize)trackBarDB).BeginInit();
|
||||
SuspendLayout();
|
||||
//
|
||||
// menuStrip1
|
||||
//
|
||||
menuStrip1.Items.AddRange(new ToolStripItem[] { tablesToolStripMenuItem });
|
||||
menuStrip1.Location = new Point(0, 0);
|
||||
menuStrip1.Name = "menuStrip1";
|
||||
menuStrip1.Size = new Size(435, 24);
|
||||
menuStrip1.TabIndex = 0;
|
||||
menuStrip1.Text = "menuStrip1";
|
||||
//
|
||||
// tablesToolStripMenuItem
|
||||
//
|
||||
tablesToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { carModelToolStripMenuItem, carToolStripMenuItem, branchToolStripMenuItem, clientToolStripMenuItem, rentalToolStripMenuItem });
|
||||
tablesToolStripMenuItem.Name = "tablesToolStripMenuItem";
|
||||
tablesToolStripMenuItem.Size = new Size(50, 20);
|
||||
tablesToolStripMenuItem.Text = "tables";
|
||||
//
|
||||
// carModelToolStripMenuItem
|
||||
//
|
||||
carModelToolStripMenuItem.Name = "carModelToolStripMenuItem";
|
||||
carModelToolStripMenuItem.Size = new Size(127, 22);
|
||||
carModelToolStripMenuItem.Text = "car model";
|
||||
carModelToolStripMenuItem.Click += carModelToolStripMenuItem_Click;
|
||||
//
|
||||
// carToolStripMenuItem
|
||||
//
|
||||
carToolStripMenuItem.Name = "carToolStripMenuItem";
|
||||
carToolStripMenuItem.Size = new Size(127, 22);
|
||||
carToolStripMenuItem.Text = "car";
|
||||
carToolStripMenuItem.Click += carToolStripMenuItem_Click;
|
||||
//
|
||||
// branchToolStripMenuItem
|
||||
//
|
||||
branchToolStripMenuItem.Name = "branchToolStripMenuItem";
|
||||
branchToolStripMenuItem.Size = new Size(127, 22);
|
||||
branchToolStripMenuItem.Text = "branch";
|
||||
branchToolStripMenuItem.Click += branchToolStripMenuItem_Click;
|
||||
//
|
||||
// clientToolStripMenuItem
|
||||
//
|
||||
clientToolStripMenuItem.Name = "clientToolStripMenuItem";
|
||||
clientToolStripMenuItem.Size = new Size(127, 22);
|
||||
clientToolStripMenuItem.Text = "client";
|
||||
clientToolStripMenuItem.Click += clientToolStripMenuItem_Click;
|
||||
//
|
||||
// rentalToolStripMenuItem
|
||||
//
|
||||
rentalToolStripMenuItem.Name = "rentalToolStripMenuItem";
|
||||
rentalToolStripMenuItem.Size = new Size(127, 22);
|
||||
rentalToolStripMenuItem.Text = "rental";
|
||||
rentalToolStripMenuItem.Click += rentalToolStripMenuItem_Click;
|
||||
//
|
||||
// buttonTest
|
||||
//
|
||||
buttonTest.Location = new Point(75, 135);
|
||||
buttonTest.Name = "buttonTest";
|
||||
buttonTest.Size = new Size(75, 23);
|
||||
buttonTest.TabIndex = 1;
|
||||
buttonTest.Text = "buttonTest";
|
||||
buttonTest.UseVisualStyleBackColor = true;
|
||||
buttonTest.Click += buttonTest_Click;
|
||||
//
|
||||
// textBoxTest
|
||||
//
|
||||
textBoxTest.Location = new Point(172, 136);
|
||||
textBoxTest.Multiline = true;
|
||||
textBoxTest.Name = "textBoxTest";
|
||||
textBoxTest.Size = new Size(251, 75);
|
||||
textBoxTest.TabIndex = 2;
|
||||
//
|
||||
// trackBarDB
|
||||
//
|
||||
trackBarDB.LargeChange = 1;
|
||||
trackBarDB.Location = new Point(12, 34);
|
||||
trackBarDB.Maximum = 100;
|
||||
trackBarDB.Name = "trackBarDB";
|
||||
trackBarDB.Orientation = Orientation.Vertical;
|
||||
trackBarDB.Size = new Size(45, 64);
|
||||
trackBarDB.TabIndex = 3;
|
||||
trackBarDB.MouseUp += trackBarDB_MouseUp;
|
||||
//
|
||||
// labelPostrges
|
||||
//
|
||||
labelPostrges.AutoSize = true;
|
||||
labelPostrges.Font = new Font("Segoe UI", 20F, FontStyle.Regular, GraphicsUnit.Point);
|
||||
labelPostrges.Location = new Point(33, 64);
|
||||
labelPostrges.Name = "labelPostrges";
|
||||
labelPostrges.Size = new Size(117, 37);
|
||||
labelPostrges.TabIndex = 4;
|
||||
labelPostrges.Text = "Postrges";
|
||||
//
|
||||
// labelMongoDB
|
||||
//
|
||||
labelMongoDB.AutoSize = true;
|
||||
labelMongoDB.Font = new Font("Segoe UI", 20F, FontStyle.Regular, GraphicsUnit.Point);
|
||||
labelMongoDB.Location = new Point(33, 27);
|
||||
labelMongoDB.Name = "labelMongoDB";
|
||||
labelMongoDB.Size = new Size(138, 37);
|
||||
labelMongoDB.TabIndex = 5;
|
||||
labelMongoDB.Text = "MongoDB";
|
||||
//
|
||||
// buttonTransferDataFromPostgresToMongo
|
||||
//
|
||||
buttonTransferDataFromPostgresToMongo.BackColor = SystemColors.ControlLight;
|
||||
buttonTransferDataFromPostgresToMongo.Cursor = Cursors.Hand;
|
||||
buttonTransferDataFromPostgresToMongo.Font = new Font("Segoe UI", 15F, FontStyle.Regular, GraphicsUnit.Point);
|
||||
buttonTransferDataFromPostgresToMongo.Location = new Point(177, 34);
|
||||
buttonTransferDataFromPostgresToMongo.Multiline = true;
|
||||
buttonTransferDataFromPostgresToMongo.Name = "buttonTransferDataFromPostgresToMongo";
|
||||
buttonTransferDataFromPostgresToMongo.ReadOnly = true;
|
||||
buttonTransferDataFromPostgresToMongo.Size = new Size(246, 67);
|
||||
buttonTransferDataFromPostgresToMongo.TabIndex = 8;
|
||||
buttonTransferDataFromPostgresToMongo.Text = "Transfer data from PostgresSQL to MongoDB";
|
||||
buttonTransferDataFromPostgresToMongo.Click += buttonTransferDataFromPostgresToMongo_Click;
|
||||
//
|
||||
// FormMain
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(7F, 15F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(435, 224);
|
||||
Controls.Add(buttonTransferDataFromPostgresToMongo);
|
||||
Controls.Add(labelMongoDB);
|
||||
Controls.Add(labelPostrges);
|
||||
Controls.Add(trackBarDB);
|
||||
Controls.Add(textBoxTest);
|
||||
Controls.Add(buttonTest);
|
||||
Controls.Add(menuStrip1);
|
||||
MainMenuStrip = menuStrip1;
|
||||
Name = "FormMain";
|
||||
Text = "FormMain";
|
||||
menuStrip1.ResumeLayout(false);
|
||||
menuStrip1.PerformLayout();
|
||||
((System.ComponentModel.ISupportInitialize)trackBarDB).EndInit();
|
||||
ResumeLayout(false);
|
||||
PerformLayout();
|
||||
}
|
||||
|
||||
#endregion
|
||||
#endregion
|
||||
|
||||
private MenuStrip menuStrip1;
|
||||
private MenuStrip menuStrip1;
|
||||
private ToolStripMenuItem tablesToolStripMenuItem;
|
||||
private ToolStripMenuItem carModelToolStripMenuItem;
|
||||
private ToolStripMenuItem carToolStripMenuItem;
|
||||
@ -137,5 +192,10 @@
|
||||
private ToolStripMenuItem rentalToolStripMenuItem;
|
||||
private Button buttonTest;
|
||||
private TextBox textBoxTest;
|
||||
}
|
||||
private TrackBar trackBarDB;
|
||||
private Label labelPostrges;
|
||||
private Label labelMongoDB;
|
||||
private TextBox textBox1;
|
||||
private TextBox buttonTransferDataFromPostgresToMongo;
|
||||
}
|
||||
}
|
@ -57,30 +57,114 @@ namespace Forms
|
||||
|
||||
private void buttonTest_Click(object sender, EventArgs e)
|
||||
{
|
||||
var service = Program.ServiceProvider?.GetService(typeof(Abstractions));
|
||||
if (service is Abstractions bd)
|
||||
var car = new Car()
|
||||
{
|
||||
var client = new Client()
|
||||
{
|
||||
Id = 0,
|
||||
Address = "awsedff",
|
||||
Email = "afdgaw",
|
||||
Name = "aedfrg",
|
||||
Phone = "asda",
|
||||
Surname = "dfgvsdfg",
|
||||
};
|
||||
Id = 0,
|
||||
StatusId = 1,
|
||||
BranchId = 1,
|
||||
Mileage = 1,
|
||||
ModelId = 1,
|
||||
Year = 1,
|
||||
};
|
||||
|
||||
DateTime startTime = DateTime.Now;
|
||||
bd.DeleteClient(10);
|
||||
bd.DeleteClient(11);
|
||||
bd.DeleteClient(12);
|
||||
bd.DeleteClient(13);
|
||||
bd.DeleteClient(14);
|
||||
DateTime endTime = DateTime.Now;
|
||||
ImplementationPostgres bd = new();
|
||||
DateTime startTime = DateTime.Now;
|
||||
bd.DeleteCar(40);
|
||||
bd.DeleteCar(41);
|
||||
bd.DeleteCar(42);
|
||||
bd.DeleteCar(43);
|
||||
bd.DeleteCar(44);
|
||||
DateTime endTime = DateTime.Now;
|
||||
|
||||
// Выводим время выполнения запроса в консоль
|
||||
textBoxTest.Text = $"Время выполнения запроса: {(endTime - startTime).TotalMilliseconds} миллисекунд";
|
||||
// Выводим время выполнения запроса в консоль
|
||||
textBoxTest.Text = $"Время выполнения запроса в постгресе: {(endTime - startTime).TotalMilliseconds} миллисекунд ";
|
||||
|
||||
ImplementationMongoDB bdMongodb = new();
|
||||
|
||||
startTime = DateTime.Now;
|
||||
bdMongodb.DeleteCar(40);
|
||||
bdMongodb.DeleteCar(41);
|
||||
bdMongodb.DeleteCar(42);
|
||||
bdMongodb.DeleteCar(43);
|
||||
bdMongodb.DeleteCar(44);
|
||||
endTime = DateTime.Now;
|
||||
|
||||
textBoxTest.Text += $"Время выполнения запроса в монгоДб: {(endTime - startTime).TotalMilliseconds} миллисекунд";
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
private void buttonTransferDataFromPostgresToMongo_Click(object sender, EventArgs e)
|
||||
{
|
||||
ImplementationMongoDB implementationMongoDB = new();
|
||||
|
||||
// очищаем всё
|
||||
foreach (var it in implementationMongoDB.GetStatuses())
|
||||
implementationMongoDB.DeleteStatus(it.Id);
|
||||
foreach (var it in implementationMongoDB.GetBodyTypes())
|
||||
implementationMongoDB.DeleteBodyType(it.Id);
|
||||
foreach (var it in implementationMongoDB.GetBranches())
|
||||
implementationMongoDB.DeleteBranch(it.Id);
|
||||
foreach (var it in implementationMongoDB.GetClients())
|
||||
implementationMongoDB.DeleteClient(it.Id);
|
||||
foreach (var it in implementationMongoDB.GetCarModels())
|
||||
implementationMongoDB.DeleteCarModel(it.Id);
|
||||
foreach (var it in implementationMongoDB.GetCars())
|
||||
implementationMongoDB.DeleteCar(it.Id);
|
||||
foreach (var it in implementationMongoDB.GetRentals())
|
||||
implementationMongoDB.DeleteRental(it.Id);
|
||||
|
||||
ImplementationPostgres implementationPostgres = new();
|
||||
|
||||
// скачиваем из постгреса
|
||||
var listStatuses = implementationPostgres.GetStatuses();
|
||||
var listBodyTypes = implementationPostgres.GetBodyTypes();
|
||||
var listBranches = implementationPostgres.GetBranches();
|
||||
var listClients = implementationPostgres.GetClients();
|
||||
var listCarModels = implementationPostgres.GetCarModels();
|
||||
var listCars = implementationPostgres.GetCars();
|
||||
var listRentals = implementationPostgres.GetRentals();
|
||||
|
||||
// вливаем данные монго дб
|
||||
foreach (var it in listStatuses)
|
||||
implementationMongoDB.AddStatus(it);
|
||||
foreach (var it in listBodyTypes)
|
||||
implementationMongoDB.AddBodyType(it);
|
||||
foreach (var it in listBranches)
|
||||
implementationMongoDB.AddBranch(it);
|
||||
foreach (var it in listClients)
|
||||
implementationMongoDB.AddClient(it);
|
||||
foreach (var it in listCarModels)
|
||||
implementationMongoDB.AddCarModel(it);
|
||||
foreach (var it in listCars)
|
||||
implementationMongoDB.AddCar(it);
|
||||
foreach (var it in listRentals)
|
||||
implementationMongoDB.AddRental(it);
|
||||
|
||||
// забираем информацию о последовательностях
|
||||
var listSequence = implementationPostgres.GetSequences();
|
||||
foreach (var it in listSequence)
|
||||
if (it.Id == "car_model" ||
|
||||
it.Id == "branch" ||
|
||||
it.Id == "car" ||
|
||||
it.Id == "client" ||
|
||||
it.Id == "rental"
|
||||
)
|
||||
implementationMongoDB.UpdateSequence(it);
|
||||
else
|
||||
throw new Exception("неправильнй id последовательности");
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -7,10 +7,31 @@ namespace Forms
|
||||
{
|
||||
private static ServiceProvider? _serviceProvider;
|
||||
public static ServiceProvider? ServiceProvider => _serviceProvider;
|
||||
/// <summary>
|
||||
/// The main entry point for the application.
|
||||
/// </summary>
|
||||
[STAThread]
|
||||
|
||||
private static bool isPostgreSQL = true;
|
||||
|
||||
private static void ConfigureServices(ServiceCollection services)
|
||||
{
|
||||
if (isPostgreSQL)
|
||||
{
|
||||
services.AddTransient<Abstractions, ImplementationPostgres>();
|
||||
}
|
||||
else
|
||||
{
|
||||
services.AddTransient<Abstractions, ImplementationMongoDB>();
|
||||
}
|
||||
services.AddTransient<FormMain>();
|
||||
services.AddTransient<FormBranch>();
|
||||
services.AddTransient<FormCar>();
|
||||
services.AddTransient<FormCarModel>();
|
||||
services.AddTransient<FormClient>();
|
||||
services.AddTransient<FormRental>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The main entry point for the application.
|
||||
/// </summary>
|
||||
[STAThread]
|
||||
static void Main()
|
||||
{
|
||||
// To customize application configuration such as set high DPI settings or default font,
|
||||
@ -21,15 +42,22 @@ namespace Forms
|
||||
_serviceProvider = services.BuildServiceProvider();
|
||||
Application.Run(_serviceProvider.GetRequiredService<FormMain>());
|
||||
}
|
||||
private static void ConfigureServices(ServiceCollection services)
|
||||
{
|
||||
services.AddTransient<Abstractions, Implementation>();
|
||||
services.AddTransient<FormMain>();
|
||||
services.AddTransient<FormBranch>();
|
||||
services.AddTransient<FormCar>();
|
||||
services.AddTransient<FormCarModel>();
|
||||
services.AddTransient<FormClient>();
|
||||
services.AddTransient<FormRental>();
|
||||
}
|
||||
}
|
||||
|
||||
public static void ChangeDB()
|
||||
{
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
257
SUBD_Car_rent/database/ImplementationMongoDB.cs
Normal file
257
SUBD_Car_rent/database/ImplementationMongoDB.cs
Normal file
@ -0,0 +1,257 @@
|
||||
using MongoDB.Driver;
|
||||
|
||||
namespace database
|
||||
{
|
||||
public class ImplementationMongoDB : Abstractions
|
||||
{
|
||||
private IMongoDatabase _database;
|
||||
private IMongoCollection<Car> _carCollection;
|
||||
private IMongoCollection<Client> _clientCollection;
|
||||
private IMongoCollection<Rental> _rentalCollection;
|
||||
private IMongoCollection<CarModel> _carModelCollection;
|
||||
private IMongoCollection<Branch> _branchCollection;
|
||||
private IMongoCollection<Status> _statusCollection;
|
||||
private IMongoCollection<BodyType> _bodyTypeCollection;
|
||||
|
||||
private IMongoCollection<Sequence> _sequenceCollection;
|
||||
|
||||
public ImplementationMongoDB()
|
||||
{
|
||||
var client = new MongoClient("mongodb://localhost:27017");
|
||||
_database = client.GetDatabase("car_rent");
|
||||
|
||||
_carCollection = _database.GetCollection<Car>("car");
|
||||
_clientCollection = _database.GetCollection<Client>("client");
|
||||
_rentalCollection = _database.GetCollection<Rental>("rental");
|
||||
_carModelCollection = _database.GetCollection<CarModel>("car_model");
|
||||
_branchCollection = _database.GetCollection<Branch>("branch");
|
||||
_statusCollection = _database.GetCollection<Status>("status");
|
||||
_bodyTypeCollection = _database.GetCollection<BodyType>("body_type");
|
||||
_sequenceCollection = _database.GetCollection<Sequence>("sequence");
|
||||
}
|
||||
|
||||
// CDUD îïåðàöèè äëÿ êàæäîé ñóùíîñòè
|
||||
|
||||
// Car
|
||||
public override void AddCar(Car car)
|
||||
{
|
||||
if (car.Id == 0)
|
||||
{
|
||||
Sequence sequence = _sequenceCollection.Find(seq => seq.Id == "car").FirstOrDefault();
|
||||
sequence.Count++;
|
||||
car.Id = sequence.Count;
|
||||
_sequenceCollection.ReplaceOne(seq => seq.Id == "car", sequence);
|
||||
}
|
||||
_carCollection.InsertOne(car);
|
||||
}
|
||||
|
||||
public override List<Car> GetCars()
|
||||
{
|
||||
return _carCollection.Find(_ => true).ToList();
|
||||
}
|
||||
|
||||
public override Car GetCarById(int id)
|
||||
{
|
||||
return _carCollection.Find(car => car.Id == id).FirstOrDefault();
|
||||
}
|
||||
|
||||
public override void UpdateCar(Car car)
|
||||
{
|
||||
_carCollection.ReplaceOne(c => c.Id == car.Id, car);
|
||||
}
|
||||
|
||||
public override void DeleteCar(int id)
|
||||
{
|
||||
_carCollection.DeleteOne(car => car.Id == id);
|
||||
}
|
||||
|
||||
// Client
|
||||
public override void AddClient(Client client)
|
||||
{
|
||||
if (client.Id == 0)
|
||||
{
|
||||
Sequence sequence = _sequenceCollection.Find(seq => seq.Id == "client").FirstOrDefault();
|
||||
sequence.Count++;
|
||||
client.Id = sequence.Count;
|
||||
_sequenceCollection.ReplaceOne(seq => seq.Id == "client", sequence);
|
||||
}
|
||||
_clientCollection.InsertOne(client);
|
||||
}
|
||||
|
||||
public override List<Client> GetClients()
|
||||
{
|
||||
return _clientCollection.Find(_ => true).ToList();
|
||||
}
|
||||
|
||||
public override Client GetClientById(int id)
|
||||
{
|
||||
return _clientCollection.Find(client => client.Id == id).FirstOrDefault();
|
||||
}
|
||||
|
||||
public override void UpdateClient(Client client)
|
||||
{
|
||||
_clientCollection.ReplaceOne(c => c.Id == client.Id, client);
|
||||
}
|
||||
|
||||
public override void DeleteClient(int id)
|
||||
{
|
||||
_clientCollection.DeleteOne(client => client.Id == id);
|
||||
}
|
||||
|
||||
// Rental
|
||||
public override void AddRental(Rental rental)
|
||||
{
|
||||
if (rental.Id == 0)
|
||||
{
|
||||
Sequence sequence = _sequenceCollection.Find(seq => seq.Id == "rental").FirstOrDefault();
|
||||
sequence.Count++;
|
||||
rental.Id = sequence.Count;
|
||||
_sequenceCollection.ReplaceOne(seq => seq.Id == "rental", sequence);
|
||||
}
|
||||
_rentalCollection.InsertOne(rental);
|
||||
}
|
||||
|
||||
public override List<Rental> GetRentals()
|
||||
{
|
||||
return _rentalCollection.Find(_ => true).ToList();
|
||||
}
|
||||
|
||||
public override Rental GetRentalById(int id)
|
||||
{
|
||||
return _rentalCollection.Find(rental => rental.Id == id).FirstOrDefault();
|
||||
}
|
||||
|
||||
public override void UpdateRental(Rental rental)
|
||||
{
|
||||
_rentalCollection.ReplaceOne(r => r.Id == rental.Id, rental);
|
||||
}
|
||||
|
||||
public override void DeleteRental(int id)
|
||||
{
|
||||
_rentalCollection.DeleteOne(rental => rental.Id == id);
|
||||
}
|
||||
|
||||
// CarModel
|
||||
public override void AddCarModel(CarModel carModel)
|
||||
{
|
||||
if (carModel.Id == 0)
|
||||
{
|
||||
Sequence sequence = _sequenceCollection.Find(seq => seq.Id == "car_model").FirstOrDefault();
|
||||
sequence.Count++;
|
||||
carModel.Id = sequence.Count;
|
||||
_sequenceCollection.ReplaceOne(seq => seq.Id == "car_model", sequence);
|
||||
}
|
||||
_carModelCollection.InsertOne(carModel);
|
||||
}
|
||||
|
||||
public override List<CarModel> GetCarModels()
|
||||
{
|
||||
return _carModelCollection.Find(_ => true).ToList();
|
||||
}
|
||||
|
||||
public override CarModel GetCarModelById(int id)
|
||||
{
|
||||
return _carModelCollection.Find(model => model.Id == id).FirstOrDefault();
|
||||
}
|
||||
|
||||
public override void UpdateCarModel(CarModel carModel)
|
||||
{
|
||||
_carModelCollection.ReplaceOne(m => m.Id == carModel.Id, carModel);
|
||||
}
|
||||
|
||||
public override void DeleteCarModel(int id)
|
||||
{
|
||||
_carModelCollection.DeleteOne(model => model.Id == id);
|
||||
}
|
||||
|
||||
// Branch
|
||||
public override void AddBranch(Branch branch)
|
||||
{
|
||||
if (branch.Id == 0)
|
||||
{
|
||||
Sequence sequence = _sequenceCollection.Find(seq => seq.Id == "branch").FirstOrDefault();
|
||||
sequence.Count++;
|
||||
branch.Id = sequence.Count;
|
||||
_sequenceCollection.ReplaceOne(seq => seq.Id == "branch", sequence);
|
||||
}
|
||||
_branchCollection.InsertOne(branch);
|
||||
}
|
||||
|
||||
public override List<Branch> GetBranches()
|
||||
{
|
||||
return _branchCollection.Find(_ => true).ToList();
|
||||
}
|
||||
|
||||
public override Branch GetBranchById(int id)
|
||||
{
|
||||
return _branchCollection.Find(branch => branch.Id == id).FirstOrDefault();
|
||||
}
|
||||
|
||||
public override void UpdateBranch(Branch branch)
|
||||
{
|
||||
_branchCollection.ReplaceOne(b => b.Id == branch.Id, branch);
|
||||
}
|
||||
|
||||
public override void DeleteBranch(int id)
|
||||
{
|
||||
_branchCollection.DeleteOne(branch => branch.Id == id);
|
||||
}
|
||||
|
||||
// Status
|
||||
public override void AddStatus(Status status)
|
||||
{
|
||||
_statusCollection.InsertOne(status);
|
||||
}
|
||||
|
||||
public override List<Status> GetStatuses()
|
||||
{
|
||||
return _statusCollection.Find(_ => true).ToList();
|
||||
}
|
||||
|
||||
public override Status GetStatusById(int id)
|
||||
{
|
||||
return _statusCollection.Find(status => status.Id == id).FirstOrDefault();
|
||||
}
|
||||
|
||||
public override void UpdateStatus(Status status)
|
||||
{
|
||||
_statusCollection.ReplaceOne(s => s.Id == status.Id, status);
|
||||
}
|
||||
|
||||
public override void DeleteStatus(int id)
|
||||
{
|
||||
_statusCollection.DeleteOne(status => status.Id == id);
|
||||
}
|
||||
|
||||
// BodyType
|
||||
public override void AddBodyType(BodyType bodyType)
|
||||
{
|
||||
_bodyTypeCollection.InsertOne(bodyType);
|
||||
}
|
||||
|
||||
public override List<BodyType> GetBodyTypes()
|
||||
{
|
||||
return _bodyTypeCollection.Find(_ => true).ToList();
|
||||
}
|
||||
|
||||
public override BodyType GetBodyTypeById(int id)
|
||||
{
|
||||
return _bodyTypeCollection.Find(bodyType => bodyType.Id == id).FirstOrDefault();
|
||||
}
|
||||
|
||||
public override void UpdateBodyType(BodyType bodyType)
|
||||
{
|
||||
_bodyTypeCollection.ReplaceOne(b => b.Id == bodyType.Id, bodyType);
|
||||
}
|
||||
|
||||
public override void DeleteBodyType(int id)
|
||||
{
|
||||
_bodyTypeCollection.DeleteOne(bodyType => bodyType.Id == id);
|
||||
}
|
||||
|
||||
public void UpdateSequence(Sequence sequence)
|
||||
{
|
||||
_sequenceCollection.ReplaceOne(seq => seq.Id == sequence.Id, sequence);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,8 +1,9 @@
|
||||
using Npgsql;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace database
|
||||
{
|
||||
public class Implementation : Abstractions
|
||||
public class ImplementationPostgres : Abstractions
|
||||
{
|
||||
private NpgsqlConnection GetConnection()
|
||||
{
|
||||
@ -497,5 +498,40 @@ namespace database
|
||||
using var cmd = new NpgsqlCommand($"DELETE FROM body_type WHERE id = {id}", conn);
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
private Sequence GetSequence(string nameInPg, string nameInApp)
|
||||
{
|
||||
using var conn = GetConnection();
|
||||
conn.Open();
|
||||
using var cmd = new NpgsqlCommand("SELECT sequencename, last_value " +
|
||||
"FROM pg_sequences " +
|
||||
"WHERE sequencename = '" + nameInPg + "_id_seq'", conn);
|
||||
using var reader = cmd.ExecuteReader();
|
||||
if (reader.Read())
|
||||
{
|
||||
return new Sequence
|
||||
{
|
||||
Id = nameInApp,
|
||||
Count = reader.GetInt32(1)
|
||||
};
|
||||
}
|
||||
else
|
||||
return new Sequence
|
||||
{
|
||||
Id = nameInApp,
|
||||
Count = 0
|
||||
};
|
||||
}
|
||||
|
||||
public List<Sequence> GetSequences()
|
||||
{
|
||||
return new List<Sequence>{
|
||||
GetSequence("client", "client"),
|
||||
GetSequence("branch", "branch"),
|
||||
GetSequence("carmodel", "car_model"),
|
||||
GetSequence("car", "car"),
|
||||
GetSequence("rental", "rental")
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
@ -7,6 +7,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="MongoDB.Driver" Version="2.25.0" />
|
||||
<PackageReference Include="Npgsql" Version="8.0.2" />
|
||||
</ItemGroup>
|
||||
|
||||
|
@ -1,10 +1,14 @@
|
||||
namespace database
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using MongoDB.Bson;
|
||||
|
||||
namespace database
|
||||
{
|
||||
// Определение моделей данных
|
||||
|
||||
public class Car
|
||||
{
|
||||
public int Id { get; set; }
|
||||
[BsonRepresentation(BsonType.Int32)]
|
||||
public int Id { get; set; }
|
||||
public int ModelId { get; set; }
|
||||
public int BranchId { get; set; }
|
||||
public int Year { get; set; }
|
||||
@ -14,7 +18,8 @@
|
||||
|
||||
public class Client
|
||||
{
|
||||
public int Id { get; set; }
|
||||
[BsonRepresentation(BsonType.Int32)]
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string Surname { get; set; }
|
||||
public string Address { get; set; }
|
||||
@ -24,7 +29,8 @@
|
||||
|
||||
public class Rental
|
||||
{
|
||||
public int Id { get; set; }
|
||||
[BsonRepresentation(BsonType.Int32)]
|
||||
public int Id { get; set; }
|
||||
public int CarId { get; set; }
|
||||
public int ClientId { get; set; }
|
||||
public DateTime StartDate { get; set; }
|
||||
@ -34,7 +40,8 @@
|
||||
|
||||
public class CarModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
[BsonRepresentation(BsonType.Int32)]
|
||||
public int Id { get; set; }
|
||||
public string Brand { get; set; }
|
||||
public string Model { get; set; }
|
||||
public int Year { get; set; }
|
||||
@ -44,7 +51,8 @@
|
||||
|
||||
public class Branch
|
||||
{
|
||||
public int Id { get; set; }
|
||||
[BsonRepresentation(BsonType.Int32)]
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string Address { get; set; }
|
||||
public string Phone { get; set; }
|
||||
@ -53,13 +61,15 @@
|
||||
|
||||
public class Status
|
||||
{
|
||||
public int Id { get; set; }
|
||||
[BsonRepresentation(BsonType.Int32)]
|
||||
public int Id { get; set; }
|
||||
public string Title { get; set; }
|
||||
}
|
||||
|
||||
public class BodyType
|
||||
{
|
||||
public int Id { get; set; }
|
||||
[BsonRepresentation(BsonType.Int32)]
|
||||
public int Id { get; set; }
|
||||
public string Title { get; set; }
|
||||
}
|
||||
|
||||
@ -68,4 +78,10 @@
|
||||
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; }
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user