This commit is contained in:
Дмитрий Блохин 2024-05-21 22:20:17 +04:00
parent 9494d07ff5
commit 26e699c393
8 changed files with 557 additions and 484 deletions

View File

@ -7,6 +7,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MongoDB.Bson" Version="2.25.0" />
<PackageReference Include="MongoDB.Driver" Version="2.25.0" />
<PackageReference Include="Npgsql" Version="8.0.3" />
</ItemGroup>

View File

@ -0,0 +1,244 @@
using Commentbase;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.ConstrainedExecution;
using System.Text;
using System.Threading.Tasks;
namespace Database
{
public class ImplementsMongoDB : Abstracts
{
private IMongoDatabase _database;
private IMongoCollection<Album> _albumCollection;
private IMongoCollection<Location> _locationCollection;
private IMongoCollection<Author> _authorCollection;
private IMongoCollection<Photo> _photoCollection;
private IMongoCollection<Comment> _commentCollection;
private IMongoCollection<Sequence> _sequenceCollection;
public ImplementsMongoDB()
{
var client = new MongoClient("mongodb://localhost:27017");
_database = client.GetDatabase("SUBD");
_albumCollection = _database.GetCollection<Album>("Album");
_locationCollection = _database.GetCollection<Location>("Location");
_authorCollection = _database.GetCollection<Author>("Author");
_photoCollection = _database.GetCollection<Photo>("Photo");
_commentCollection = _database.GetCollection<Comment>("Comment");
_sequenceCollection = _database.GetCollection<Sequence>("sequence");
}
//Album
public override void CreateAlbum(Album album)
{
if (album.Id == 0)
{
Sequence sequence = _sequenceCollection.Find(seq => seq.Id == "Album").FirstOrDefault();
sequence.Count++;
album.Id = sequence.Count;
_sequenceCollection.ReplaceOne(seq => seq.Id == "Album", sequence);
}
_albumCollection.InsertOne(album);
}
public override List<Album> GetAlbums()
{
return _albumCollection.Find(_ => true).ToList();
}
public override Album GetAlbum(int id)
{
return _albumCollection.Find(album => album.Id == id).FirstOrDefault();
}
public override void UpdateAlbum(Album album)
{
_albumCollection.ReplaceOne(c => c.Id == album.Id, album);
}
public override void DeleteAlbum(int id)
{
_albumCollection.DeleteOne(album => album.Id == id);
}
public override Album GetAlbum(string title)
{
return _albumCollection.Find(album => album.Title == title).FirstOrDefault();
}
public override void DeleteAllAlbums()
{
_albumCollection.DeleteManyAsync(Builders<Album>.Filter.Empty);
}
// Location
public override void CreateLocation(Location location)
{
if (location.Id == 0)
{
Sequence sequence = _sequenceCollection.Find(seq => seq.Id == "Location").FirstOrDefault();
sequence.Count++;
location.Id = sequence.Count;
_sequenceCollection.ReplaceOne(seq => seq.Id == "Location", sequence);
}
_locationCollection.InsertOne(location);
}
public override List<Location> GetLocations()
{
return _locationCollection.Find(_ => true).ToList();
}
public override Location GetLocation(int id)
{
return _locationCollection.Find(location => location.Id == id).FirstOrDefault();
}
public override void UpdateLocation(Location location)
{
_locationCollection.ReplaceOne(c => c.Id == location.Id, location);
}
public override void DeleteLocation(int id)
{
_locationCollection.DeleteOne(location => location.Id == id);
}
public override Location GetLocation(string name)
{
return _locationCollection.Find(location => location.Name == name).FirstOrDefault();
}
public override void DeleteAllLocations()
{
_locationCollection.DeleteManyAsync(Builders<Location>.Filter.Empty);
}
// Author
public override void CreateAuthor(Author author)
{
if (author.Id == 0)
{
Sequence sequence = _sequenceCollection.Find(seq => seq.Id == "Author").FirstOrDefault();
sequence.Count++;
author.Id = sequence.Count;
_sequenceCollection.ReplaceOne(seq => seq.Id == "Author", sequence);
}
_authorCollection.InsertOne(author);
}
public override List<Author> GetAuthors()
{
return _authorCollection.Find(_ => true).ToList();
}
public override Author GetAuthor(int id)
{
return _authorCollection.Find(author => author.Id == id).FirstOrDefault();
}
public override void UpdateAuthor(Author author)
{
_authorCollection.ReplaceOne(r => r.Id == author.Id, author);
}
public override void DeleteAuthor(int id)
{
_authorCollection.DeleteOne(author => author.Id == id);
}
public override Author GetAuthor(string name)
{
return _authorCollection.Find(author => author.Name == name).FirstOrDefault();
}
public override void DeleteAllAuthors()
{
_authorCollection.DeleteManyAsync(Builders<Author>.Filter.Empty);
}
// Photo
public override void CreatePhoto(Photo photo)
{
if (photo.Id == 0)
{
Sequence sequence = _sequenceCollection.Find(seq => seq.Id == "Photo").FirstOrDefault();
sequence.Count++;
photo.Id = sequence.Count;
_sequenceCollection.ReplaceOne(seq => seq.Id == "Photo", sequence);
}
_photoCollection.InsertOne(photo);
}
public override List<Photo> GetPhotos()
{
return _photoCollection.Find(_ => true).ToList();
}
public override Photo GetPhoto(int id)
{
return _photoCollection.Find(photo => photo.Id == id).FirstOrDefault();
}
public override void UpdatePhoto(Photo photo)
{
_photoCollection.ReplaceOne(b => b.Id == photo.Id, photo);
}
public override void DeletePhoto(int id)
{
_photoCollection.DeleteOne(photo => photo.Id == id);
}
public override Photo GetPhoto(int AlbumId, int LocationId, int AuthorId)
{
return _photoCollection.Find(photo => (photo.AlbumId == AlbumId && photo.LocationId == LocationId && photo.AuthorId == AuthorId)).FirstOrDefault();
}
public override void DeleteAllPhotos()
{
_photoCollection.DeleteManyAsync(Builders<Photo>.Filter.Empty);
}
// Comment
public override void CreateComment(Comment comment)
{
if (comment.Id == 0)
{
Sequence sequence = _sequenceCollection.Find(seq => seq.Id == "Comment").FirstOrDefault();
sequence.Count++;
comment.Id = sequence.Count;
_sequenceCollection.ReplaceOne(seq => seq.Id == "Comment", sequence);
}
_commentCollection.InsertOne(comment);
}
public override List<Comment> GetComments()
{
return _commentCollection.Find(_ => true).ToList();
}
public override Comment GetComment(int id)
{
return _commentCollection.Find(comment => comment.Id == id).FirstOrDefault();
}
public override void UpdateComment(Comment comment)
{
_commentCollection.ReplaceOne(b => b.Id == comment.Id, comment);
}
public override void DeleteComment(int id)
{
_commentCollection.DeleteOne(comment => comment.Id == id);
}
public override Comment GetCommentPhoto(int PhotoId)
{
return _commentCollection.Find(comment => comment.PhotoId == PhotoId).FirstOrDefault();
}
public override void DeleteAllComments()
{
_commentCollection.DeleteManyAsync(Builders<Comment>.Filter.Empty);
}
public void UpdateSequence(Sequence sequence)
{
_sequenceCollection.ReplaceOne(seq => seq.Id == sequence.Id, sequence);
}
}
}

View File

@ -1,5 +1,6 @@
using Commentbase;
using Npgsql;
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
@ -8,7 +9,7 @@ using System.Threading.Tasks;
namespace Database
{
public class Implements : Abstracts
public class ImplementsPostgres : Abstracts
{
private NpgsqlConnection GetConnect()
{
@ -523,5 +524,39 @@ namespace Database
using var cmd = new NpgsqlCommand($"DELETE FROM Comment", conn);
cmd.ExecuteNonQuery();
}
private Sequence GetSequence(string nameInPg, string nameInApp)
{
using var conn = GetConnect();
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("album", "Album"),
GetSequence("location", "Location"),
GetSequence("author", "Author"),
GetSequence("photo", "Photo"),
GetSequence("comment", "Comment")
};
}
}
}

View File

@ -1,4 +1,6 @@
using System;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -8,18 +10,21 @@ namespace Database
{
public class Album
{
[BsonRepresentation(BsonType.Int32)]
public int Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
}
public class Location
{
[BsonRepresentation(BsonType.Int32)]
public int Id { get; set; }
public string Name { get; set; }
public string ShortName { get; set; }
}
public class Author
{
[BsonRepresentation(BsonType.Int32)]
public int Id { get; set; }
public string Name { get; set; }
public string PhoneNum { get; set; }
@ -27,6 +32,7 @@ namespace Database
}
public class Photo
{
[BsonRepresentation(BsonType.Int32)]
public int Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
@ -39,9 +45,16 @@ namespace Database
}
public class Comment
{
[BsonRepresentation(BsonType.Int32)]
public int Id { get; set; }
public DateTime PostDate { get; set; }
public string Content { get; set; }
public int PhotoId { get; set; }
}
public class Sequence
{
[BsonRepresentation(BsonType.String)]
public string Id { get; set; }
public int Count { get; set; }
}
}

View File

@ -28,279 +28,167 @@
/// </summary>
private void InitializeComponent()
{
button1 = new Button();
button2 = new Button();
button3 = new Button();
button4 = new Button();
button5 = new Button();
button6 = new Button();
button7 = new Button();
button8 = new Button();
button9 = new Button();
button10 = new Button();
button11 = new Button();
button12 = new Button();
button13 = new Button();
button14 = new Button();
button15 = new Button();
button16 = new Button();
button17 = new Button();
button18 = new Button();
button19 = new Button();
button20 = new Button();
menuStrip1 = new MenuStrip();
сущностиToolStripMenuItem = new ToolStripMenuItem();
albumToolStripMenuItem = new ToolStripMenuItem();
locationToolStripMenuItem = new ToolStripMenuItem();
authorToolStripMenuItem = new ToolStripMenuItem();
photoToolStripMenuItem = new ToolStripMenuItem();
commentToolStripMenuItem = new ToolStripMenuItem();
trackBarDB = new TrackBar();
labelMongoDB = new Label();
label1 = new Label();
buttonTransferDataFromPostgresToMongo = new Button();
buttonTest = new Button();
textBoxTest = new TextBox();
menuStrip1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)trackBarDB).BeginInit();
SuspendLayout();
//
// button1
// menuStrip1
//
button1.Location = new Point(41, 32);
button1.Name = "button1";
button1.Size = new Size(137, 29);
button1.TabIndex = 0;
button1.Text = "Альбом";
button1.UseVisualStyleBackColor = true;
button1.Click += button1_Click;
menuStrip1.ImageScalingSize = new Size(20, 20);
menuStrip1.Items.AddRange(new ToolStripItem[] { сущностиToolStripMenuItem });
menuStrip1.Location = new Point(0, 0);
menuStrip1.Name = "menuStrip1";
menuStrip1.Size = new Size(1135, 28);
menuStrip1.TabIndex = 20;
menuStrip1.Text = "menuStrip1";
//
// button2
// сущностиToolStripMenuItem
//
button2.Location = new Point(243, 32);
button2.Name = "button2";
button2.Size = new Size(137, 29);
button2.TabIndex = 1;
button2.Text = "Локация";
button2.UseVisualStyleBackColor = true;
button2.Click += button2_Click;
сущностиToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { albumToolStripMenuItem, locationToolStripMenuItem, authorToolStripMenuItem, photoToolStripMenuItem, commentToolStripMenuItem });
сущностиToolStripMenuItem.Name = "сущностиToolStripMenuItem";
сущностиToolStripMenuItem.Size = new Size(91, 24);
сущностиToolStripMenuItem.Text = "Сущности";
//
// button3
// albumToolStripMenuItem
//
button3.Location = new Point(451, 32);
button3.Name = "button3";
button3.Size = new Size(137, 29);
button3.TabIndex = 2;
button3.Text = "Автор";
button3.UseVisualStyleBackColor = true;
button3.Click += button3_Click;
albumToolStripMenuItem.Name = "albumToolStripMenuItem";
albumToolStripMenuItem.Size = new Size(190, 26);
albumToolStripMenuItem.Text = "Альбом";
albumToolStripMenuItem.Click += albumToolStripMenuItem_Click;
//
// button4
// locationToolStripMenuItem
//
button4.Location = new Point(675, 32);
button4.Name = "button4";
button4.Size = new Size(137, 29);
button4.TabIndex = 3;
button4.Text = "Фотография";
button4.UseVisualStyleBackColor = true;
button4.Click += button4_Click;
locationToolStripMenuItem.Name = "locationToolStripMenuItem";
locationToolStripMenuItem.Size = new Size(190, 26);
locationToolStripMenuItem.Text = "Локация";
locationToolStripMenuItem.Click += locationToolStripMenuItem_Click;
//
// button5
// authorToolStripMenuItem
//
button5.Location = new Point(922, 32);
button5.Name = "button5";
button5.Size = new Size(137, 29);
button5.TabIndex = 4;
button5.Text = "Комментарий";
button5.UseVisualStyleBackColor = true;
button5.Click += button5_Click;
authorToolStripMenuItem.Name = "authorToolStripMenuItem";
authorToolStripMenuItem.Size = new Size(190, 26);
authorToolStripMenuItem.Text = "Автор";
authorToolStripMenuItem.Click += authorToolStripMenuItem_Click;
//
// button6
// photoToolStripMenuItem
//
button6.Location = new Point(12, 120);
button6.Name = "button6";
button6.Size = new Size(189, 29);
button6.TabIndex = 5;
button6.Text = "Создать 1000 альбомов";
button6.UseVisualStyleBackColor = true;
button6.Click += button6_Click;
photoToolStripMenuItem.Name = "photoToolStripMenuItem";
photoToolStripMenuItem.Size = new Size(190, 26);
photoToolStripMenuItem.Text = "Фотография";
photoToolStripMenuItem.Click += photoToolStripMenuItem_Click;
//
// button7
// commentToolStripMenuItem
//
button7.Location = new Point(216, 120);
button7.Name = "button7";
button7.Size = new Size(189, 29);
button7.TabIndex = 6;
button7.Text = "Создать 1000 локаций";
button7.UseVisualStyleBackColor = true;
button7.Click += button7_Click;
commentToolStripMenuItem.Name = "commentToolStripMenuItem";
commentToolStripMenuItem.Size = new Size(190, 26);
commentToolStripMenuItem.Text = "Комментарий";
commentToolStripMenuItem.Click += commentToolStripMenuItem_Click;
//
// button8
// trackBarDB
//
button8.Location = new Point(424, 120);
button8.Name = "button8";
button8.Size = new Size(189, 29);
button8.TabIndex = 7;
button8.Text = "Создать 1000 авторов";
button8.UseVisualStyleBackColor = true;
button8.Click += button8_Click;
trackBarDB.LargeChange = 1;
trackBarDB.Location = new Point(31, 89);
trackBarDB.Maximum = 100;
trackBarDB.Name = "trackBarDB";
trackBarDB.Orientation = Orientation.Vertical;
trackBarDB.Size = new Size(56, 119);
trackBarDB.TabIndex = 3;
trackBarDB.MouseUp += trackBarDB_MouseUp;
//
// button9
// labelMongoDB
//
button9.Location = new Point(650, 120);
button9.Name = "button9";
button9.Size = new Size(201, 29);
button9.TabIndex = 8;
button9.Text = "Создать 1000 фотографий";
button9.UseVisualStyleBackColor = true;
button9.Click += button9_Click;
labelMongoDB.AutoSize = true;
labelMongoDB.Location = new Point(93, 89);
labelMongoDB.Name = "labelMongoDB";
labelMongoDB.Size = new Size(77, 20);
labelMongoDB.TabIndex = 22;
labelMongoDB.Text = "MongoDB";
//
// button10
// label1
//
button10.Location = new Point(880, 120);
button10.Name = "button10";
button10.Size = new Size(218, 29);
button10.TabIndex = 9;
button10.Text = "Создать 1000 комментариев";
button10.UseVisualStyleBackColor = true;
button10.Click += button10_Click;
label1.AutoSize = true;
label1.Location = new Point(93, 188);
label1.Name = "label1";
label1.Size = new Size(64, 20);
label1.TabIndex = 23;
label1.Text = "Postgres";
//
// button11
// buttonTransferDataFromPostgresToMongo
//
button11.Location = new Point(12, 187);
button11.Name = "button11";
button11.Size = new Size(198, 29);
button11.TabIndex = 10;
button11.Text = "Изменить 1000 альбомов";
button11.UseVisualStyleBackColor = true;
button11.Click += button11_Click;
buttonTransferDataFromPostgresToMongo.Location = new Point(834, 80);
buttonTransferDataFromPostgresToMongo.Name = "buttonTransferDataFromPostgresToMongo";
buttonTransferDataFromPostgresToMongo.Size = new Size(245, 128);
buttonTransferDataFromPostgresToMongo.TabIndex = 24;
buttonTransferDataFromPostgresToMongo.Text = "Transfer data from PostgresSQL to MongoDB";
buttonTransferDataFromPostgresToMongo.UseVisualStyleBackColor = true;
buttonTransferDataFromPostgresToMongo.Click += buttonTransferDataFromPostgresToMongo_Click;
//
// button12
// buttonTest
//
button12.Location = new Point(216, 187);
button12.Name = "button12";
button12.Size = new Size(189, 29);
button12.TabIndex = 11;
button12.Text = "Изменить 1000 локаций";
button12.UseVisualStyleBackColor = true;
button12.Click += button12_Click;
buttonTest.Location = new Point(439, 318);
buttonTest.Name = "buttonTest";
buttonTest.Size = new Size(94, 29);
buttonTest.TabIndex = 25;
buttonTest.Text = "test";
buttonTest.UseVisualStyleBackColor = true;
buttonTest.Click += buttonTest_Click;
//
// button13
// textBoxTest
//
button13.Location = new Point(424, 187);
button13.Name = "button13";
button13.Size = new Size(189, 29);
button13.TabIndex = 12;
button13.Text = "Изменить 1000 авторов";
button13.UseVisualStyleBackColor = true;
button13.Click += button13_Click;
//
// button14
//
button14.Location = new Point(639, 187);
button14.Name = "button14";
button14.Size = new Size(224, 29);
button14.TabIndex = 13;
button14.Text = "Изменить 1000 фотографий";
button14.UseVisualStyleBackColor = true;
button14.Click += button14_Click;
//
// button15
//
button15.Location = new Point(869, 187);
button15.Name = "button15";
button15.Size = new Size(239, 29);
button15.TabIndex = 14;
button15.Text = "Изменить 1000 комментариев";
button15.UseVisualStyleBackColor = true;
button15.Click += button15_Click;
//
// button16
//
button16.Location = new Point(12, 257);
button16.Name = "button16";
button16.Size = new Size(198, 29);
button16.TabIndex = 15;
button16.Text = "Удалить 1000 альбомов";
button16.UseVisualStyleBackColor = true;
button16.Click += button16_Click;
//
// button17
//
button17.Location = new Point(216, 257);
button17.Name = "button17";
button17.Size = new Size(189, 29);
button17.TabIndex = 16;
button17.Text = "Удалить 1000 локаций";
button17.UseVisualStyleBackColor = true;
button17.Click += button17_Click;
//
// button18
//
button18.Location = new Point(424, 257);
button18.Name = "button18";
button18.Size = new Size(189, 29);
button18.TabIndex = 17;
button18.Text = "Удалить 1000 авторов";
button18.UseVisualStyleBackColor = true;
button18.Click += button18_Click;
//
// button19
//
button19.Location = new Point(639, 257);
button19.Name = "button19";
button19.Size = new Size(224, 29);
button19.TabIndex = 18;
button19.Text = "Удалить 1000 фотографий";
button19.UseVisualStyleBackColor = true;
button19.Click += button19_Click;
//
// button20
//
button20.Location = new Point(869, 257);
button20.Name = "button20";
button20.Size = new Size(239, 29);
button20.TabIndex = 19;
button20.Text = "Удалить 1000 комментариев";
button20.UseVisualStyleBackColor = true;
button20.Click += button20_Click;
textBoxTest.Location = new Point(310, 80);
textBoxTest.Multiline = true;
textBoxTest.Name = "textBoxTest";
textBoxTest.Size = new Size(377, 128);
textBoxTest.TabIndex = 26;
//
// MainForm
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(1131, 318);
Controls.Add(button20);
Controls.Add(button19);
Controls.Add(button18);
Controls.Add(button17);
Controls.Add(button16);
Controls.Add(button15);
Controls.Add(button14);
Controls.Add(button13);
Controls.Add(button12);
Controls.Add(button11);
Controls.Add(button10);
Controls.Add(button9);
Controls.Add(button8);
Controls.Add(button7);
Controls.Add(button6);
Controls.Add(button5);
Controls.Add(button4);
Controls.Add(button3);
Controls.Add(button2);
Controls.Add(button1);
ClientSize = new Size(1135, 436);
Controls.Add(textBoxTest);
Controls.Add(buttonTest);
Controls.Add(buttonTransferDataFromPostgresToMongo);
Controls.Add(label1);
Controls.Add(labelMongoDB);
Controls.Add(trackBarDB);
Controls.Add(menuStrip1);
MainMenuStrip = menuStrip1;
Name = "MainForm";
Text = "MainForm";
menuStrip1.ResumeLayout(false);
menuStrip1.PerformLayout();
((System.ComponentModel.ISupportInitialize)trackBarDB).EndInit();
ResumeLayout(false);
PerformLayout();
}
#endregion
private Button button1;
private Button button2;
private Button button3;
private Button button4;
private Button button5;
private Button button6;
private Button button7;
private Button button8;
private Button button9;
private Button button10;
private Button button11;
private Button button12;
private Button button13;
private Button button14;
private Button button15;
private Button button16;
private Button button17;
private Button button18;
private Button button19;
private Button button20;
private MenuStrip menuStrip1;
private ToolStripMenuItem сущностиToolStripMenuItem;
private ToolStripMenuItem albumToolStripMenuItem;
private ToolStripMenuItem locationToolStripMenuItem;
private ToolStripMenuItem authorToolStripMenuItem;
private ToolStripMenuItem photoToolStripMenuItem;
private ToolStripMenuItem commentToolStripMenuItem;
private TrackBar trackBarDB;
private Label labelMongoDB;
private Label label1;
private Button buttonTransferDataFromPostgresToMongo;
private Button buttonTest;
private TextBox textBoxTest;
}
}

View File

@ -10,299 +10,162 @@ using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.DataFormats;
namespace View
{
public partial class MainForm : Form
{
private readonly Abstracts db;
public MainForm(Abstracts abstracts)
public MainForm()
{
InitializeComponent();
db = abstracts;
}
private void button1_Click(object sender, EventArgs e)
private void albumToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormAlbums));
if (service is FormAlbums form)
{
form.Show();
form.ShowDialog();
}
}
private void button2_Click(object sender, EventArgs e)
private void locationToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormLocations));
if (service is FormLocations form)
{
form.Show();
form.ShowDialog();
}
}
private void button3_Click(object sender, EventArgs e)
private void authorToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormAuthors));
if (service is FormAuthors form)
{
form.Show();
form.ShowDialog();
}
}
private void button4_Click(object sender, EventArgs e)
private void photoToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormPhotos));
if (service is FormPhotos form)
{
form.Show();
form.ShowDialog();
}
}
private void button5_Click(object sender, EventArgs e)
private void commentToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormComments));
if (service is FormComments form)
{
form.Show();
form.ShowDialog();
}
}
private void button6_Click(object sender, EventArgs e)
private void buttonTest_Click(object sender, EventArgs e)
{
string Title = "Шикарный альбом";
string Description = "Тут даже нечего сказать";
DateTime start = DateTime.Now;
for (int i = 0; i < 1000; i++)
var photo = new Photo()
{
db.CreateAlbum(new()
{
Title = $"{Title}{i}",
Description = $"{Description}{i}"
});
}
DateTime end = DateTime.Now;
MessageBox.Show((end - start).Milliseconds.ToString(), "Время работы", MessageBoxButtons.OK, MessageBoxIcon.Information);
Id = 0,
Title = "photo",
Description = "cool photo",
Privacy = "public",
UploadDate = DateTime.Now,
ImagePath = "Image/312",
AlbumId = 1,
LocationId = 1,
AuthorId = 1,
};
ImplementsPostgres bd = new();
DateTime startTime = DateTime.Now;
bd.DeletePhoto(40);
bd.DeletePhoto(41);
bd.DeletePhoto(42);
bd.DeletePhoto(43);
bd.DeletePhoto(44);
DateTime endTime = DateTime.Now;
// Выводим время выполнения запроса в консоль
textBoxTest.Text = $"Время выполнения запроса в постгресе: {(endTime - startTime).TotalMilliseconds} миллисекунд ";
ImplementsMongoDB bdMongodb = new();
startTime = DateTime.Now;
bdMongodb.DeletePhoto(40);
bdMongodb.DeletePhoto(41);
bdMongodb.DeletePhoto(42);
bdMongodb.DeletePhoto(43);
bdMongodb.DeletePhoto(44);
endTime = DateTime.Now;
textBoxTest.Text += $"Время выполнения запроса в монгоДб: {(endTime - startTime).TotalMilliseconds} миллисекунд";
}
private void button7_Click(object sender, EventArgs e)
private void trackBarDB_MouseUp(object sender, MouseEventArgs e)
{
string Name = "Москва, Россия";
string ShortName = "RUS";
DateTime start = DateTime.Now;
for (int i = 0; i < 1000; i++)
if (trackBarDB.Value > 50)
{
db.CreateLocation(new()
{
Name = $"{Name}{i}",
ShortName = $"{ShortName}{i}"
});
trackBarDB.Value = 100;
Program.ChangeDB(false);
}
DateTime end = DateTime.Now;
MessageBox.Show((end - start).Milliseconds.ToString(), "Время работы", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button8_Click(object sender, EventArgs e)
{
string Name = "Эрик Житель";
string PhoneNum = "+79999999999";
string Email = "laba@mail.ru";
DateTime start = DateTime.Now;
for (int i = 0; i < 1000; i++)
else
{
db.CreateAuthor(new()
{
Name = $"{Name}{i}",
PhoneNum = $"{PhoneNum}{i}",
Email = $"{Email}{i}"
});
trackBarDB.Value = 0;
Program.ChangeDB(true);
}
DateTime end = DateTime.Now;
MessageBox.Show((end - start).Milliseconds.ToString(), "Время работы", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button9_Click(object sender, EventArgs e)
private void buttonTransferDataFromPostgresToMongo_Click(object sender, EventArgs e)
{
string Title = "image";
string Description = "photo";
string Privacy = "Общий доступ";
DateTime UploadData = DateTime.Now;
string ImagePath = "Image/Ch8cfe8vfevb";
Album albumId = db.GetAlbum("Шикарный альбом0");
Location locationId = db.GetLocation("Москва, Россия0");
Author authorId = db.GetAuthor("Эрик Житель0");
DateTime start = DateTime.Now;
for (int i = 0; i < 1000; i++)
{
db.CreatePhoto(new()
{
Title = $"{Title}{i}",
Description = $"{Description}{i}",
Privacy = $"{Privacy}{i}",
UploadDate = UploadData,
ImagePath = $"{ImagePath}{i}",
AlbumId = albumId.Id + i,
LocationId = locationId.Id + i,
AuthorId = authorId.Id + i
});
}
DateTime end = DateTime.Now;
MessageBox.Show((end - start).Milliseconds.ToString(), "Время работы", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button10_Click(object sender, EventArgs e)
{
DateTime PostDate = DateTime.Now;
string Content = "Вай, как круто!";
Album albumId = db.GetAlbum("Шикарный альбом0");
Location locationId = db.GetLocation("Москва, Россия0");
Author authorId = db.GetAuthor("Эрик Житель0");
Photo photoId = db.GetPhoto(albumId.Id, locationId.Id, authorId.Id);
DateTime start = DateTime.Now;
for (int i = 0; i < 1000; i++)
{
db.CreateComment(new()
{
PostDate = PostDate,
Content = $"{Content}{i}",
PhotoId = photoId.Id + i
});
}
DateTime end = DateTime.Now;
MessageBox.Show((end - start).Milliseconds.ToString(), "Время работы", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button11_Click(object sender, EventArgs e)
{
Album AlbumId = db.GetAlbum("Шикарный альбом0");
string Title = "Шикарный альбом1";
string Description = "Тут даже нечего сказать опять";
DateTime start = DateTime.Now;
for (int i = 0; i < 1000; i++)
{
db.UpdateAlbum(new()
{
Id = AlbumId.Id + i,
Title = $"{Title}{i}",
Description = $"{Description}{i}"
});
}
DateTime end = DateTime.Now;
MessageBox.Show((end - start).Milliseconds.ToString(), "Время работы", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button12_Click(object sender, EventArgs e)
{
Location locationId = db.GetLocation("Москва, Россия0");
string Name = "Рим, Италия";
string ShortName = "ITA";
DateTime start = DateTime.Now;
for (int i = 0; i < 1000; i++)
{
db.UpdateLocation(new()
{
Id = locationId.Id + i,
Name = $"{Name}{i}",
ShortName = $"{ShortName}{i}"
});
}
DateTime end = DateTime.Now;
MessageBox.Show((end - start).Milliseconds.ToString(), "Время работы", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button13_Click(object sender, EventArgs e)
{
Author authorId = db.GetAuthor("Эрик Житель0");
string Name = "Chel";
string PhoneNum = "+79991112233";
string Email = "laba4@mail.ru";
DateTime start = DateTime.Now;
for (int i = 0; i < 1000; i++)
{
db.UpdateAuthor(new()
{
Id = authorId.Id + i,
Name = $"{Name}{i}",
PhoneNum = $"{PhoneNum}{i}",
Email = $"{Email}{i}"
});
}
DateTime end = DateTime.Now;
MessageBox.Show((end - start).Milliseconds.ToString(), "Время работы", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button14_Click(object sender, EventArgs e)
{
string Title = "image";
string Description = "photo";
string Privacy = "Общий доступ";
DateTime UploadData = DateTime.Now;
string ImagePath = "Image/sndbkKSB23842KJH";
Album albumId = db.GetAlbum("Шикарный альбом0");
Location locationId = db.GetLocation("Москва, Россия0");
Author authorId = db.GetAuthor("Эрик Житель0");
Photo photo = db.GetPhoto(albumId.Id, locationId.Id, authorId.Id);
DateTime start = DateTime.Now;
for (int i = 0; i < 1000; i++)
{
db.UpdatePhoto(new()
{
Id = photo.Id + i,
Title = $"{Title}{i}",
Description = $"{Description}{i}",
Privacy = $"{Privacy}{i}",
UploadDate = UploadData,
ImagePath = $"{ImagePath}{i}",
AlbumId = albumId.Id + i,
LocationId = locationId.Id + i,
AuthorId = authorId.Id + i
});
}
DateTime end = DateTime.Now;
MessageBox.Show((end - start).Milliseconds.ToString(), "Время работы", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button15_Click(object sender, EventArgs e)
{
DateTime PostDate = DateTime.Now;
string Content = "О боже мой!";
Album albumId = db.GetAlbum("Шикарный альбом0");
Location locationId = db.GetLocation("Москва, Россия0");
Author authorId = db.GetAuthor("Эрик Житель0");
Photo photo = db.GetPhoto(albumId.Id, locationId.Id, authorId.Id);
Comment comment = db.GetComment(photo.Id);
DateTime start = DateTime.Now;
for (int i = 0; i < 1000; i++)
{
db.UpdateComment(new()
{
Id = comment.Id + i,
PostDate = PostDate,
Content = $"{Content}{i}",
PhotoId = photo.Id + i
});
}
DateTime end = DateTime.Now;
MessageBox.Show((end - start).Milliseconds.ToString(), "Время работы", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button16_Click(object sender, EventArgs e)
{
DateTime start = DateTime.Now;
db.DeleteAllAlbums();
DateTime end = DateTime.Now;
MessageBox.Show((end - start).Milliseconds.ToString(), "Время работы", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button17_Click(object sender, EventArgs e)
{
DateTime start = DateTime.Now;
db.DeleteAllLocations();
DateTime end = DateTime.Now;
MessageBox.Show((end - start).Milliseconds.ToString(), "Время работы", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button18_Click(object sender, EventArgs e)
{
DateTime start = DateTime.Now;
db.DeleteAllAuthors();
DateTime end = DateTime.Now;
MessageBox.Show((end - start).Milliseconds.ToString(), "Время работы", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button19_Click(object sender, EventArgs e)
{
DateTime start = DateTime.Now;
db.DeleteAllPhotos();
DateTime end = DateTime.Now;
MessageBox.Show((end - start).Milliseconds.ToString(), "Время работы", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button20_Click(object sender, EventArgs e)
{
DateTime start = DateTime.Now;
db.DeleteAllComments();
DateTime end = DateTime.Now;
MessageBox.Show((end - start).Milliseconds.ToString(), "Время работы", MessageBoxButtons.OK, MessageBoxIcon.Information);
ImplementsMongoDB implementsMongoDB = new();
// очищаем всё
foreach (var it in implementsMongoDB.GetAlbums())
implementsMongoDB.DeleteAlbum(it.Id);
foreach (var it in implementsMongoDB.GetLocations())
implementsMongoDB.DeleteLocation(it.Id);
foreach (var it in implementsMongoDB.GetAuthors())
implementsMongoDB.DeleteAuthor(it.Id);
foreach (var it in implementsMongoDB.GetPhotos())
implementsMongoDB.DeletePhoto(it.Id);
foreach (var it in implementsMongoDB.GetComments())
implementsMongoDB.DeleteComment(it.Id);
ImplementsPostgres implementsPostgres = new();
// скачиваем из постгреса
var listAlbums = implementsPostgres.GetAlbums();
var listLocations = implementsPostgres.GetLocations();
var listAuthors = implementsPostgres.GetAuthors();
var listPhotos = implementsPostgres.GetPhotos();
var listComments = implementsPostgres.GetComments();
// вливаем данные монго дб
foreach (var it in listAlbums)
implementsMongoDB.CreateAlbum(it);
foreach (var it in listLocations)
implementsMongoDB.CreateLocation(it);
foreach (var it in listAuthors)
implementsMongoDB.CreateAuthor(it);
foreach (var it in listPhotos)
implementsMongoDB.CreatePhoto(it);
foreach (var it in listComments)
implementsMongoDB.CreateComment(it);
// забираем информацию о последовательностях
var listSequence = implementsPostgres.GetSequences();
foreach (var it in listSequence)
if (it.Id == "Album" ||
it.Id == "Location" ||
it.Id == "Author" ||
it.Id == "Photo" ||
it.Id == "Comment"
)
implementsMongoDB.UpdateSequence(it);
else
throw new Exception("неправильнй id последовательности");
}
}
}

View File

@ -117,4 +117,7 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="menuStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
</root>

View File

@ -3,6 +3,7 @@ using Database;
using System;
using Commentbase;
using Microsoft.VisualBasic;
using static System.Windows.Forms.DataFormats;
namespace View
{
@ -20,9 +21,17 @@ namespace View
Application.Run(_serviceProvider.GetRequiredService<MainForm>());
}
private static bool isPostgreSQL = true;
private static void ConfigureServices(ServiceCollection services)
{
services.AddTransient<Abstracts, Implements>();
if (isPostgreSQL)
{
services.AddTransient<Abstracts, ImplementsPostgres>();
}
else
{
services.AddTransient<Abstracts, ImplementsMongoDB>();
}
services.AddTransient<MainForm>();
services.AddTransient<FormAlbum>();
services.AddTransient<FormAlbums>();
@ -35,5 +44,21 @@ namespace View
services.AddTransient<FormComment>();
services.AddTransient<FormComments>();
}
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();
}
}
}