diff --git a/NewsBlog/NewsBlog.sln b/NewsBlog/NewsBlog.sln index 96f30e9..1c84ad1 100644 --- a/NewsBlog/NewsBlog.sln +++ b/NewsBlog/NewsBlog.sln @@ -5,9 +5,11 @@ VisualStudioVersion = 17.8.34330.188 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NewsBlogView", "NewsBlogView\NewsBlogView.csproj", "{13D9F9B7-6693-4D26-8178-08389235382C}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NewsBlogAbstractions", "NewsBlogAbstractions\NewsBlogAbstractions.csproj", "{22CA3930-4C19-4623-9B58-C42A48B45576}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NewsBlogAbstractions", "NewsBlogAbstractions\NewsBlogAbstractions.csproj", "{22CA3930-4C19-4623-9B58-C42A48B45576}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NewsBlogDatabaseImplementation", "NewsBlogDatabaseImplementation\NewsBlogDatabaseImplementation.csproj", "{1DA49583-881B-4E05-A128-AEB692A174B4}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NewsBlogDatabaseImplementation", "NewsBlogDatabaseImplementation\NewsBlogDatabaseImplementation.csproj", "{1DA49583-881B-4E05-A128-AEB692A174B4}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NewsBlogMongoDB", "NewsBlogMongoDB\NewsBlogMongoDB.csproj", "{223BAF22-6898-427D-A5D2-6D32BC50BD30}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -27,6 +29,10 @@ Global {1DA49583-881B-4E05-A128-AEB692A174B4}.Debug|Any CPU.Build.0 = Debug|Any CPU {1DA49583-881B-4E05-A128-AEB692A174B4}.Release|Any CPU.ActiveCfg = Release|Any CPU {1DA49583-881B-4E05-A128-AEB692A174B4}.Release|Any CPU.Build.0 = Release|Any CPU + {223BAF22-6898-427D-A5D2-6D32BC50BD30}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {223BAF22-6898-427D-A5D2-6D32BC50BD30}.Debug|Any CPU.Build.0 = Debug|Any CPU + {223BAF22-6898-427D-A5D2-6D32BC50BD30}.Release|Any CPU.ActiveCfg = Release|Any CPU + {223BAF22-6898-427D-A5D2-6D32BC50BD30}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/NewsBlog/NewsBlogMongoDB/ImplementationMongoDB.cs b/NewsBlog/NewsBlogMongoDB/ImplementationMongoDB.cs new file mode 100644 index 0000000..53a456e --- /dev/null +++ b/NewsBlog/NewsBlogMongoDB/ImplementationMongoDB.cs @@ -0,0 +1,198 @@ +using MongoDB.Driver; +using NewsBlogAbstractions.Models; +using NewsBlogMongoDB.Models; +using NewsBlogMongoDB.StoragesContracts; +using Tag = NewsBlogAbstractions.Models.Tag; + +namespace NewsBlogMongoDB +{ + public class ImplementationMongoDB: StorageModel + { + private IMongoDatabase _database; + private IMongoCollection _articleCollection; + private IMongoCollection _authorCollection; + private IMongoCollection _categoryCollection; + private IMongoCollection _CommentCollection; + private IMongoCollection _TagCollection; + private IMongoCollection _SequenceCollection; + + public ImplementationMongoDB() + { + var client = new MongoClient("mongodb://localhost:27017"); + _database = client.GetDatabase("NewsBlog"); + + _articleCollection = _database.GetCollection("article"); + _authorCollection = _database.GetCollection("author"); + _categoryCollection = _database.GetCollection("category"); + _CommentCollection = _database.GetCollection("comment"); + _TagCollection = _database.GetCollection("tag"); + _SequenceCollection = _database.GetCollection("sequence"); + } + + // article + public override void AddArticle(Article article) + { + string Author_id = GetMongoId("Author", article.AuthorId.ToString()); + string Category_id = GetMongoId("Category", article.CategoryId.ToString()); + + var art = new ArticleMongo(article, Author_id, Category_id); + _articleCollection.InsertOne(art); + + var seq = new Sequence { Sql_id = article.Id.ToString(), Mongo_id = art.Id, Table_name = "Article"}; + _SequenceCollection.InsertOne(seq); + } + + public override List GetArticles() + { + return _articleCollection.Find(_ => true).ToList(); + } + + public override ArticleMongo GetArticleById(string id) + { + return _articleCollection.Find(article => article.Id == id).FirstOrDefault(); + } + + public override void UpdateArticle(ArticleMongo article) + { + _articleCollection.ReplaceOne(c => c.Id == article.Id, article); + } + + public override void DeleteArticle(string id) + { + _articleCollection.DeleteOne(article => article.Id == id); + } + + // Author + public override void AddAuthor(Author author) + { + var art = new AuthorMongo(author); + _authorCollection.InsertOne(art); + + var seq = new Sequence { Sql_id = author.Id.ToString(), Mongo_id = art.Id, Table_name = "Author" }; + _SequenceCollection.InsertOne(seq); + } + + public override List GetAuthors() + { + return _authorCollection.Find(_ => true).ToList(); + } + + public override AuthorMongo GetAuthorById(string id) + { + return _authorCollection.Find(author => author.Id == id).FirstOrDefault(); + } + + public override void UpdateAuthor(AuthorMongo author) + { + _authorCollection.ReplaceOne(c => c.Id == author.Id, author); + } + + public override void DeleteAuthor(string id) + { + _authorCollection.DeleteOne(author => author.Id == id); + } + + // Category + public override void AddCategory(Category category) + { + var cat = new CategoryMongo(category); + _categoryCollection.InsertOne(cat); + + var seq = new Sequence { Sql_id = category.Id.ToString(), Mongo_id = cat.Id, Table_name = "Category" }; + _SequenceCollection.InsertOne(seq); + } + + public override List GetCategorys() + { + return _categoryCollection.Find(_ => true).ToList(); + } + + public override CategoryMongo GetCategoryById(string id) + { + return _categoryCollection.Find(category => category.Id == id).FirstOrDefault(); + } + + public override void UpdateCategory(CategoryMongo category) + { + _categoryCollection.ReplaceOne(r => r.Id == category.Id, category); + } + + public override void DeleteCategory(string id) + { + _categoryCollection.DeleteOne(category => category.Id == id); + } + + // Comment + public override void AddComment(Comment Comment) + { + string ArticleId = GetMongoId("Article", Comment.ArticleId.ToString()); + + _CommentCollection.InsertOne(new CommentMongo(Comment, ArticleId)); + } + + public override List GetComments() + { + return _CommentCollection.Find(_ => true).ToList(); + } + + public override CommentMongo GetCommentById(string id) + { + return _CommentCollection.Find(model => model.Id == id).FirstOrDefault(); + } + + public override void UpdateComment(CommentMongo Comment) + { + _CommentCollection.ReplaceOne(m => m.Id == Comment.Id, Comment); + } + + public override void DeleteComment(string id) + { + _CommentCollection.DeleteOne(model => model.Id == id); + } + + // Tag + public override void AddTag(Tag Tag) + { + string ArticleId = GetMongoId("Article", Tag.ArticleId.ToString()); + + _TagCollection.InsertOne(new TagMongo(Tag, ArticleId)); + } + + public override List GetTags() + { + return _TagCollection.Find(_ => true).ToList(); + } + + public override TagMongo GetTagById(string id) + { + return _TagCollection.Find(Tag => Tag.Id == id).FirstOrDefault(); + } + + public override void UpdateTag(TagMongo Tag) + { + _TagCollection.ReplaceOne(b => b.Id == Tag.Id, Tag); + } + + public override void DeleteTag(string id) + { + _TagCollection.DeleteOne(Tag => Tag.Id == id); + } + + public List GetSequence() + { + return _SequenceCollection.Find(_ => true).ToList(); + } + + public override string GetMongoId(string tableName, string sqlId) + { + var sequence = _SequenceCollection.Find(s => s.Table_name == tableName && s.Sql_id == sqlId).FirstOrDefault(); + + return sequence?.Mongo_id ?? string.Empty; + } + + public void DeleteSequence(string id) + { + _SequenceCollection.DeleteOne(Sequence => Sequence.Id == id); + } + } +} diff --git a/NewsBlog/NewsBlogMongoDB/Models/ArticleMongo.cs b/NewsBlog/NewsBlogMongoDB/Models/ArticleMongo.cs new file mode 100644 index 0000000..7846f44 --- /dev/null +++ b/NewsBlog/NewsBlogMongoDB/Models/ArticleMongo.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using MongoDB.Bson.Serialization.Attributes; +using MongoDB.Bson; + +namespace NewsBlogMongoDB.Models +{ + public class ArticleMongo + { + [BsonId] + [BsonRepresentation(BsonType.ObjectId)] + public string Id { get; set; } + public string Title { get; set; } = string.Empty; + public string Content { get; set; } = string.Empty; + public string Publication_date { get; set; } = string.Empty; + public string Author_id { get; set; } = string.Empty; + public string Category_id { get; set; } = string.Empty; + + public ArticleMongo(NewsBlogAbstractions.Models.Article model, string author_id, string category_id) + { + Title = model.Title; + Content = model.Content; + Publication_date = model.PublicationDate.ToString("d"); + Author_id = author_id; + Category_id = category_id; + } + public ArticleMongo() + { + } + } +} diff --git a/NewsBlog/NewsBlogMongoDB/Models/AuthorMongo.cs b/NewsBlog/NewsBlogMongoDB/Models/AuthorMongo.cs new file mode 100644 index 0000000..ab55424 --- /dev/null +++ b/NewsBlog/NewsBlogMongoDB/Models/AuthorMongo.cs @@ -0,0 +1,31 @@ +using MongoDB.Bson.Serialization.Attributes; +using MongoDB.Bson; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using NewsBlogAbstractions.Models; + +namespace NewsBlogMongoDB.Models +{ + public class AuthorMongo + { + [BsonId] + [BsonRepresentation(BsonType.ObjectId)] + public string Id { get; set; } + public string Name { get; set; } = string.Empty; + public string Description { get; set; } = string.Empty; + public string Phone { get; set; } = string.Empty; + + public AuthorMongo(NewsBlogAbstractions.Models.Author model) + { + Name = model.Name; + Description = model.Description; + Phone = model.Phone; + } + public AuthorMongo() + { + } + } +} diff --git a/NewsBlog/NewsBlogMongoDB/Models/CategoryMongo.cs b/NewsBlog/NewsBlogMongoDB/Models/CategoryMongo.cs new file mode 100644 index 0000000..f3b5beb --- /dev/null +++ b/NewsBlog/NewsBlogMongoDB/Models/CategoryMongo.cs @@ -0,0 +1,27 @@ +using MongoDB.Bson; +using MongoDB.Bson.Serialization.Attributes; +using NewsBlogAbstractions.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NewsBlogMongoDB.Models +{ + public class CategoryMongo + { + [BsonId] + [BsonRepresentation(BsonType.ObjectId)] + public string Id { get; set; } + public string Name { get; set; } = string.Empty; + + public CategoryMongo(NewsBlogAbstractions.Models.Category model) + { + Name = model.Name; + } + public CategoryMongo() + { + } + } +} diff --git a/NewsBlog/NewsBlogMongoDB/Models/CommentMongo.cs b/NewsBlog/NewsBlogMongoDB/Models/CommentMongo.cs new file mode 100644 index 0000000..f606486 --- /dev/null +++ b/NewsBlog/NewsBlogMongoDB/Models/CommentMongo.cs @@ -0,0 +1,33 @@ +using MongoDB.Bson; +using MongoDB.Bson.Serialization.Attributes; +using NewsBlogAbstractions.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NewsBlogMongoDB.Models +{ + public class CommentMongo + { + [BsonId] + [BsonRepresentation(BsonType.ObjectId)] + public string Id { get; set; } + public string Title { get; set; } = string.Empty; + public string Content { get; set; } = string.Empty; + public string Create_date { get; set; } = string.Empty; + public string Article_id { get; set; } = string.Empty; + + public CommentMongo(NewsBlogAbstractions.Models.Comment model, string ArticleId) + { + Title = model.Title; + Content = model.Content; + Create_date = model.CreateDate.ToString("d"); + Article_id = ArticleId; + } + public CommentMongo() + { + } + } +} diff --git a/NewsBlog/NewsBlogMongoDB/Models/Sequence.cs b/NewsBlog/NewsBlogMongoDB/Models/Sequence.cs new file mode 100644 index 0000000..b0a004b --- /dev/null +++ b/NewsBlog/NewsBlogMongoDB/Models/Sequence.cs @@ -0,0 +1,20 @@ +using MongoDB.Bson.Serialization.Attributes; +using MongoDB.Bson; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NewsBlogMongoDB.Models +{ + public class Sequence + { + [BsonId] + [BsonRepresentation(BsonType.ObjectId)] + public string Id { get; set; } = string.Empty; + public string Sql_id { get; set; } = string.Empty; + public string Mongo_id { get; set; } = string.Empty; + public string Table_name { get; set;} = string.Empty; + } +} diff --git a/NewsBlog/NewsBlogMongoDB/Models/TagMongo.cs b/NewsBlog/NewsBlogMongoDB/Models/TagMongo.cs new file mode 100644 index 0000000..b0596de --- /dev/null +++ b/NewsBlog/NewsBlogMongoDB/Models/TagMongo.cs @@ -0,0 +1,31 @@ +using MongoDB.Bson.Serialization.Attributes; +using MongoDB.Bson; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using NewsBlogAbstractions.Models; + +namespace NewsBlogMongoDB.Models +{ + public class TagMongo + { + [BsonId] + [BsonRepresentation(BsonType.ObjectId)] + public string Id { get; set; } + public string Name { get; set; } = string.Empty; + public string Decsription { get; set; } = string.Empty; + public string Article_id { get; set; } = string.Empty; + + public TagMongo(NewsBlogAbstractions.Models.Tag model, string ArticleId) + { + Name = model.Name; + Decsription = model.Description; + Article_id = ArticleId; + } + public TagMongo() + { + } + } +} diff --git a/NewsBlog/NewsBlogMongoDB/NewsBlogMongoDB.csproj b/NewsBlog/NewsBlogMongoDB/NewsBlogMongoDB.csproj new file mode 100644 index 0000000..2dca2f8 --- /dev/null +++ b/NewsBlog/NewsBlogMongoDB/NewsBlogMongoDB.csproj @@ -0,0 +1,18 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + + diff --git a/NewsBlog/NewsBlogMongoDB/StoragesContracts/StorageModel.cs b/NewsBlog/NewsBlogMongoDB/StoragesContracts/StorageModel.cs new file mode 100644 index 0000000..0578b07 --- /dev/null +++ b/NewsBlog/NewsBlogMongoDB/StoragesContracts/StorageModel.cs @@ -0,0 +1,50 @@ +using NewsBlogAbstractions.Models; +using NewsBlogMongoDB.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NewsBlogMongoDB.StoragesContracts +{ + public abstract class StorageModel + { + // Article + public abstract void AddArticle(Article article); + public abstract List GetArticles(); + public abstract ArticleMongo GetArticleById(string id); + public abstract void UpdateArticle(ArticleMongo article); + public abstract void DeleteArticle(string id); + + // Author + public abstract void AddAuthor(Author author); + public abstract AuthorMongo GetAuthorById(string id); + public abstract List GetAuthors(); + public abstract void UpdateAuthor(AuthorMongo author); + public abstract void DeleteAuthor(string id); + + // Category + public abstract void AddCategory(Category category); + public abstract CategoryMongo GetCategoryById(string id); + public abstract List GetCategorys(); + public abstract void UpdateCategory(CategoryMongo category); + public abstract void DeleteCategory(string id); + + // Comment + public abstract void AddComment(Comment comment); + public abstract CommentMongo GetCommentById(string id); + public abstract List GetComments(); + public abstract void UpdateComment(CommentMongo comment); + public abstract void DeleteComment(string id); + + // Tag + public abstract void AddTag(Tag tag); + public abstract TagMongo GetTagById(string id); + public abstract List GetTags(); + public abstract void UpdateTag(TagMongo tag); + public abstract void DeleteTag(string id); + + public abstract string GetMongoId(string tableName, string sqlId); + } +} diff --git a/NewsBlog/NewsBlogView/FormArticle.cs b/NewsBlog/NewsBlogView/FormArticle.cs index 663147d..bdde998 100644 --- a/NewsBlog/NewsBlogView/FormArticle.cs +++ b/NewsBlog/NewsBlogView/FormArticle.cs @@ -1,5 +1,7 @@ using NewsBlogAbstractions.Models; using NewsBlogAbstractions.WorkAbstractions; +using NewsBlogMongoDB.Models; +using NewsBlogMongoDB.StoragesContracts; using System; using System.Collections.Generic; using System.ComponentModel; @@ -18,12 +20,14 @@ namespace NewsBlogView private readonly IArticleWork _articleLogic; private readonly IAuthorWork _authorLogic; private readonly ICategoryWork _categoryLogic; - public FormArticle(IArticleWork articleLogic, IAuthorWork authorLogic, ICategoryWork categoryLogic) + private readonly StorageModel _mongoLogic; + public FormArticle(IArticleWork articleLogic, IAuthorWork authorLogic, ICategoryWork categoryLogic, StorageModel mongoLogic) { InitializeComponent(); _articleLogic = articleLogic; _authorLogic = authorLogic; _categoryLogic = categoryLogic; + _mongoLogic = mongoLogic; } private void FormArticle_Load(object sender, EventArgs e) @@ -42,14 +46,19 @@ namespace NewsBlogView CategoryId = ((Category?)comboBoxCategory.SelectedItem)?.Id ?? 0, }; - _articleLogic.Create(newArticle); + if (Program.isPostgreSQL) + _articleLogic.Create(newArticle); + else + _mongoLogic.AddArticle(newArticle); LoadData(); } private void LoadData() { - var articles = _articleLogic.GetAll(); + var articlesSql = _articleLogic.GetAll(); + + var articlesMongo = _mongoLogic.GetArticles(); dataGridView.Rows.Clear(); @@ -82,11 +91,18 @@ namespace NewsBlogView comboBoxCategory.DisplayMember = "Category"; comboBoxCategory.ValueMember = "Name"; - foreach (var article in articles) - { - dataGridView.Rows.Add(article.Id, article.Title, article.Content, article.PublicationDate.ToString("d"), - article.AuthorId, article.CategoryId, _authorLogic.Get(article.AuthorId)?.Name, _categoryLogic.Get(article.CategoryId)?.Name); - } + if(Program.isPostgreSQL) + foreach (var article in articlesSql) + { + dataGridView.Rows.Add(article.Id, article.Title, article.Content, article.PublicationDate.ToString("d"), + article.AuthorId, article.CategoryId, _authorLogic.Get(article.AuthorId)?.Name, _categoryLogic.Get(article.CategoryId)?.Name); + } + else + foreach (var article in articlesMongo) + { + dataGridView.Rows.Add(article.Id, article.Title, article.Content, article.Publication_date, + article.Author_id, article.Category_id, _mongoLogic.GetAuthorById(article.Author_id)?.Name, _mongoLogic.GetCategoryById(article.Category_id)?.Name); + } } private void ButtonUpdate_Click(object sender, EventArgs e) @@ -94,19 +110,37 @@ namespace NewsBlogView if (dataGridView.SelectedRows.Count > 0) { DataGridViewRow selectedRow = dataGridView.SelectedRows[0]; - int articleId = Convert.ToInt32(selectedRow.Cells["Id"].Value); - Article updatedArticle = new() + if (Program.isPostgreSQL) { - Id = articleId, - Title = textBoxTitle.Text, - Content = textBoxContent.Text, - PublicationDate = DateTime.Now, - AuthorId = ((Author?)comboBoxAuthor.SelectedItem)?.Id ?? 0, - CategoryId = ((Category?)comboBoxCategory.SelectedItem)?.Id ?? 0, - }; + int articleId = Convert.ToInt32(selectedRow.Cells["Id"].Value); - _articleLogic.Update(updatedArticle); + Article updatedArticle = new() + { + Id = articleId, + Title = textBoxTitle.Text, + Content = textBoxContent.Text, + PublicationDate = DateTime.Now, + AuthorId = ((Author?)comboBoxAuthor.SelectedItem)?.Id ?? 0, + CategoryId = ((Category?)comboBoxCategory.SelectedItem)?.Id ?? 0, + }; + _articleLogic.Update(updatedArticle); + } + else + { + string? articleId = selectedRow.Cells["Id"].Value.ToString(); + + ArticleMongo updated = new() + { + Id = articleId, + Title = textBoxTitle.Text, + Content = textBoxContent.Text, + Publication_date = DateTime.Now.ToString("d"), + Author_id = _mongoLogic.GetMongoId("Author", ((Author?)comboBoxAuthor.SelectedItem)?.Id.ToString() ?? string.Empty), + Category_id = _mongoLogic.GetMongoId("Category", ((Category?)comboBoxCategory.SelectedItem)?.Id.ToString() ?? string.Empty), + }; + _mongoLogic.UpdateArticle(updated); + } LoadData(); } @@ -121,9 +155,17 @@ namespace NewsBlogView if (dataGridView.SelectedRows.Count > 0) { DataGridViewRow selectedRow = dataGridView.SelectedRows[0]; - int articleId = Convert.ToInt32(selectedRow.Cells["Id"].Value); - _articleLogic.Delete(articleId); + if (Program.isPostgreSQL) + { + int articleId = Convert.ToInt32(selectedRow.Cells["Id"].Value); + _articleLogic.Delete(articleId); + } + else + { + string? articleIdMongo = selectedRow.Cells["Id"].Value.ToString(); + _mongoLogic.DeleteArticle(articleIdMongo); + } LoadData(); } diff --git a/NewsBlog/NewsBlogView/FormAuthor.cs b/NewsBlog/NewsBlogView/FormAuthor.cs index 3d64bbe..0e8e4f2 100644 --- a/NewsBlog/NewsBlogView/FormAuthor.cs +++ b/NewsBlog/NewsBlogView/FormAuthor.cs @@ -1,5 +1,7 @@ using NewsBlogAbstractions.Models; using NewsBlogAbstractions.WorkAbstractions; +using NewsBlogMongoDB.Models; +using NewsBlogMongoDB.StoragesContracts; using System; using System.Collections.Generic; using System.ComponentModel; @@ -15,10 +17,12 @@ namespace NewsBlogView public partial class FormAuthor : Form { private readonly IAuthorWork _authorLogic; - public FormAuthor(IAuthorWork authorLogic) + private readonly StorageModel _mongoLogic; + public FormAuthor(IAuthorWork authorLogic, StorageModel mongoLogic) { InitializeComponent(); _authorLogic = authorLogic; + _mongoLogic = mongoLogic; } private void FormAuthor_Load(object sender, EventArgs e) @@ -35,7 +39,10 @@ namespace NewsBlogView Phone = textBoxPhone.Text, }; - _authorLogic.Create(newAuthor); + if (Program.isPostgreSQL) + _authorLogic.Create(newAuthor); + else + _mongoLogic.AddAuthor(newAuthor); LoadData(); } @@ -43,6 +50,7 @@ namespace NewsBlogView private void LoadData() { var authors = _authorLogic.GetAll(); + var authorsMongo = _mongoLogic.GetAuthors(); dataGridView.Rows.Clear(); @@ -59,9 +67,19 @@ namespace NewsBlogView dataGridView.Columns["Description"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; dataGridView.Columns["Phone"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - foreach (var author in authors) + if (Program.isPostgreSQL) { - dataGridView.Rows.Add(author.Id, author.Name, author.Description, author.Phone); + foreach (var author in authors) + { + dataGridView.Rows.Add(author.Id, author.Name, author.Description, author.Phone); + } + } + else + { + foreach (var author in authorsMongo) + { + dataGridView.Rows.Add(author.Id, author.Name, author.Description, author.Phone); + } } } @@ -70,17 +88,34 @@ namespace NewsBlogView if (dataGridView.SelectedRows.Count > 0) { DataGridViewRow selectedRow = dataGridView.SelectedRows[0]; - int authorId = Convert.ToInt32(selectedRow.Cells["Id"].Value); - - Author updatedAuthor = new() + if (Program.isPostgreSQL) { - Id = authorId, - Name = textBoxName.Text, - Description = textBoxDescription.Text, - Phone = textBoxPhone.Text, - }; + int authorId = Convert.ToInt32(selectedRow.Cells["Id"].Value); - _authorLogic.Update(updatedAuthor); + Author updatedAuthor = new() + { + Id = authorId, + Name = textBoxName.Text, + Description = textBoxDescription.Text, + Phone = textBoxPhone.Text, + }; + + _authorLogic.Update(updatedAuthor); + } + else + { + string? authorId = selectedRow.Cells["Id"].Value.ToString(); + + AuthorMongo updatedAuthor = new() + { + Id = authorId, + Name = textBoxName.Text, + Description = textBoxDescription.Text, + Phone = textBoxPhone.Text, + }; + + _mongoLogic.UpdateAuthor(updatedAuthor); + } LoadData(); } @@ -95,9 +130,18 @@ namespace NewsBlogView if (dataGridView.SelectedRows.Count > 0) { DataGridViewRow selectedRow = dataGridView.SelectedRows[0]; - int authorId = Convert.ToInt32(selectedRow.Cells["Id"].Value); + if (Program.isPostgreSQL) + { + int authorId = Convert.ToInt32(selectedRow.Cells["Id"].Value); - _authorLogic.Delete(authorId); + _authorLogic.Delete(authorId); + } + else + { + string? authorId = selectedRow.Cells["Id"].Value.ToString(); + + _mongoLogic.DeleteAuthor(authorId); + } LoadData(); } diff --git a/NewsBlog/NewsBlogView/FormCategory.cs b/NewsBlog/NewsBlogView/FormCategory.cs index 4fdc30e..2f8951c 100644 --- a/NewsBlog/NewsBlogView/FormCategory.cs +++ b/NewsBlog/NewsBlogView/FormCategory.cs @@ -1,5 +1,7 @@ using NewsBlogAbstractions.Models; using NewsBlogAbstractions.WorkAbstractions; +using NewsBlogMongoDB.Models; +using NewsBlogMongoDB.StoragesContracts; using System; using System.Collections.Generic; using System.ComponentModel; @@ -15,10 +17,12 @@ namespace NewsBlogView public partial class FormCategory : Form { private readonly ICategoryWork _categoryLogic; - public FormCategory(ICategoryWork categoryLogic) + private readonly StorageModel _mongoLogic; + public FormCategory(ICategoryWork categoryLogic, StorageModel mongoLogic) { InitializeComponent(); _categoryLogic = categoryLogic; + _mongoLogic = mongoLogic; } private void FormCategory_Load(object sender, EventArgs e) @@ -33,7 +37,10 @@ namespace NewsBlogView Name = textBoxName.Text, }; - _categoryLogic.Create(newCategory); + if (Program.isPostgreSQL) + _categoryLogic.Create(newCategory); + else + _mongoLogic.AddCategory(newCategory); LoadData(); } @@ -41,6 +48,7 @@ namespace NewsBlogView private void LoadData() { var categories = _categoryLogic.GetAll(); + var categoriesMongo = _mongoLogic.GetCategorys(); dataGridView.Rows.Clear(); @@ -53,9 +61,19 @@ namespace NewsBlogView dataGridView.Columns["Id"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; dataGridView.Columns["Name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - foreach (var category in categories) + if (Program.isPostgreSQL) { - dataGridView.Rows.Add(category.Id, category.Name); + foreach (var category in categories) + { + dataGridView.Rows.Add(category.Id, category.Name); + } + } + else + { + foreach (var category in categoriesMongo) + { + dataGridView.Rows.Add(category.Id, category.Name); + } } } @@ -64,15 +82,30 @@ namespace NewsBlogView if (dataGridView.SelectedRows.Count > 0) { DataGridViewRow selectedRow = dataGridView.SelectedRows[0]; - int categoryId = Convert.ToInt32(selectedRow.Cells["Id"].Value); - - Category updatedCategory = new() + if (Program.isPostgreSQL) { - Id = categoryId, - Name = textBoxName.Text, - }; + int categoryId = Convert.ToInt32(selectedRow.Cells["Id"].Value); - _categoryLogic.Update(updatedCategory); + Category updatedCategory = new() + { + Id = categoryId, + Name = textBoxName.Text, + }; + + _categoryLogic.Update(updatedCategory); + } + else + { + string? categoryId = selectedRow.Cells["Id"].Value.ToString(); + + CategoryMongo updatedCategory = new() + { + Id = categoryId, + Name = textBoxName.Text, + }; + + _mongoLogic.UpdateCategory(updatedCategory); + } LoadData(); } @@ -87,9 +120,18 @@ namespace NewsBlogView if (dataGridView.SelectedRows.Count > 0) { DataGridViewRow selectedRow = dataGridView.SelectedRows[0]; - int categoryId = Convert.ToInt32(selectedRow.Cells["Id"].Value); + if (Program.isPostgreSQL) + { + int categoryId = Convert.ToInt32(selectedRow.Cells["Id"].Value); - _categoryLogic.Delete(categoryId); + _categoryLogic.Delete(categoryId); + } + else + { + string? categoryId = selectedRow.Cells["Id"].Value.ToString(); + + _mongoLogic.DeleteCategory(categoryId); + } LoadData(); } diff --git a/NewsBlog/NewsBlogView/FormComment.cs b/NewsBlog/NewsBlogView/FormComment.cs index 181024e..e463e25 100644 --- a/NewsBlog/NewsBlogView/FormComment.cs +++ b/NewsBlog/NewsBlogView/FormComment.cs @@ -1,5 +1,7 @@ using NewsBlogAbstractions.Models; using NewsBlogAbstractions.WorkAbstractions; +using NewsBlogMongoDB.Models; +using NewsBlogMongoDB.StoragesContracts; using System; using System.Collections.Generic; using System.ComponentModel; @@ -16,11 +18,13 @@ namespace NewsBlogView { private readonly ICommentWork _commentLogic; private readonly IArticleWork _articleLogic; - public FormComment(ICommentWork commentLogic, IArticleWork articleLogic) + private readonly StorageModel _mongoLogic; + public FormComment(ICommentWork commentLogic, IArticleWork articleLogic, StorageModel mongoLogic) { InitializeComponent(); _commentLogic = commentLogic; _articleLogic = articleLogic; + _mongoLogic = mongoLogic; } private void FormComment_Load(object sender, EventArgs e) @@ -38,15 +42,16 @@ namespace NewsBlogView ArticleId = ((Article?)comboBoxArticle.SelectedItem)?.Id ?? 0, }; - _commentLogic.Create(newComment); + if (Program.isPostgreSQL) + _commentLogic.Create(newComment); + else + _mongoLogic.AddComment(newComment); LoadData(); } private void LoadData() { - var comments = _commentLogic.GetAll(); - dataGridView.Rows.Clear(); if (dataGridView.ColumnCount == 0) @@ -70,10 +75,25 @@ namespace NewsBlogView comboBoxArticle.DisplayMember = "Article"; comboBoxArticle.ValueMember = "Title"; - foreach (var comment in comments) + if (Program.isPostgreSQL) { - dataGridView.Rows.Add(comment.Id, comment.Title, comment.Content, comment.CreateDate.ToString("d"), comment.ArticleId, _articleLogic.Get(comment.ArticleId)?.Title); + var comments = _commentLogic.GetAll(); + + foreach (var comment in comments) + { + dataGridView.Rows.Add(comment.Id, comment.Title, comment.Content, comment.CreateDate.ToString("d"), comment.ArticleId, _articleLogic.Get(comment.ArticleId)?.Title); + } } + else + { + var comments = _mongoLogic.GetComments(); + + foreach (var comment in comments) + { + dataGridView.Rows.Add(comment.Id, comment.Title, comment.Content, comment.Create_date, comment.Article_id, _mongoLogic.GetArticleById(comment.Article_id)?.Title); + } + } + } private void ButtonUpdate_Click(object sender, EventArgs e) @@ -81,18 +101,36 @@ namespace NewsBlogView if (dataGridView.SelectedRows.Count > 0) { DataGridViewRow selectedRow = dataGridView.SelectedRows[0]; - int commentId = Convert.ToInt32(selectedRow.Cells["Id"].Value); - - Comment updatedComment = new() + if (Program.isPostgreSQL) { - Id = commentId, - Title = textBoxTitle.Text, - Content = textBoxContent.Text, - CreateDate = DateTime.Now, - ArticleId = ((Article?)comboBoxArticle.SelectedItem)?.Id ?? 0, - }; + int commentId = Convert.ToInt32(selectedRow.Cells["Id"].Value); - _commentLogic.Update(updatedComment); + Comment updatedComment = new() + { + Id = commentId, + Title = textBoxTitle.Text, + Content = textBoxContent.Text, + CreateDate = DateTime.Now, + ArticleId = ((Article?)comboBoxArticle.SelectedItem)?.Id ?? 0, + }; + + _commentLogic.Update(updatedComment); + } + else + { + string? commentId = selectedRow.Cells["Id"].Value.ToString(); + + CommentMongo updatedComment = new() + { + Id = commentId, + Title = textBoxTitle.Text, + Content = textBoxContent.Text, + Create_date = DateTime.Now.ToString("d"), + Article_id = _mongoLogic.GetMongoId("Article", ((Article?)comboBoxArticle.SelectedItem)?.Id.ToString() ?? String.Empty), + }; + + _mongoLogic.UpdateComment(updatedComment); + } LoadData(); } @@ -107,9 +145,18 @@ namespace NewsBlogView if (dataGridView.SelectedRows.Count > 0) { DataGridViewRow selectedRow = dataGridView.SelectedRows[0]; - int commentId = Convert.ToInt32(selectedRow.Cells["Id"].Value); + if (Program.isPostgreSQL) + { + int commentId = Convert.ToInt32(selectedRow.Cells["Id"].Value); - _commentLogic.Delete(commentId); + _commentLogic.Delete(commentId); + } + else + { + string? commentId = selectedRow.Cells["Id"].Value.ToString(); + + _mongoLogic.DeleteComment(commentId); + } LoadData(); } diff --git a/NewsBlog/NewsBlogView/FormMain.Designer.cs b/NewsBlog/NewsBlogView/FormMain.Designer.cs index f294cca..37e0740 100644 --- a/NewsBlog/NewsBlogView/FormMain.Designer.cs +++ b/NewsBlog/NewsBlogView/FormMain.Designer.cs @@ -28,6 +28,7 @@ /// private void InitializeComponent() { + CheckBox MongoDB; menuStrip1 = new MenuStrip(); справочникиToolStripMenuItem = new ToolStripMenuItem(); articleToolStripMenuItem = new ToolStripMenuItem(); @@ -41,7 +42,9 @@ get1000ToolStripMenuItem = new ToolStripMenuItem(); update1000ToolStripMenuItem = new ToolStripMenuItem(); delete1000ToolStripMenuItem = new ToolStripMenuItem(); + updateMongoDBToolStripMenuItem = new ToolStripMenuItem(); labelTest = new Label(); + MongoDB = new CheckBox(); menuStrip1.SuspendLayout(); SuspendLayout(); // @@ -51,8 +54,7 @@ menuStrip1.Items.AddRange(new ToolStripItem[] { справочникиToolStripMenuItem, замерВремениToolStripMenuItem, замерыВремениToolStripMenuItem }); menuStrip1.Location = new Point(0, 0); menuStrip1.Name = "menuStrip1"; - menuStrip1.Padding = new Padding(7, 3, 0, 3); - menuStrip1.Size = new Size(633, 30); + menuStrip1.Size = new Size(554, 24); menuStrip1.TabIndex = 0; menuStrip1.Text = "menuStrip1"; // @@ -60,102 +62,120 @@ // справочникиToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { articleToolStripMenuItem, authorToolStripMenuItem, categoryToolStripMenuItem, commentToolStripMenuItem, tagToolStripMenuItem }); справочникиToolStripMenuItem.Name = "справочникиToolStripMenuItem"; - справочникиToolStripMenuItem.Size = new Size(117, 24); + справочникиToolStripMenuItem.Size = new Size(94, 20); справочникиToolStripMenuItem.Text = "Справочники"; // // articleToolStripMenuItem // articleToolStripMenuItem.Name = "articleToolStripMenuItem"; - articleToolStripMenuItem.Size = new Size(157, 26); + articleToolStripMenuItem.Size = new Size(151, 22); articleToolStripMenuItem.Text = "Статьи"; articleToolStripMenuItem.Click += articleToolStripMenuItem_Click; // // authorToolStripMenuItem // authorToolStripMenuItem.Name = "authorToolStripMenuItem"; - authorToolStripMenuItem.Size = new Size(157, 26); + authorToolStripMenuItem.Size = new Size(151, 22); authorToolStripMenuItem.Text = "Авторы"; authorToolStripMenuItem.Click += authorToolStripMenuItem_Click; // - // CategoryToolStripMenuItem + // categoryToolStripMenuItem // categoryToolStripMenuItem.Name = "categoryToolStripMenuItem"; - categoryToolStripMenuItem.Size = new Size(157, 26); + categoryToolStripMenuItem.Size = new Size(151, 22); categoryToolStripMenuItem.Text = "Категории"; categoryToolStripMenuItem.Click += categoryToolStripMenuItem_Click; // // commentToolStripMenuItem // commentToolStripMenuItem.Name = "commentToolStripMenuItem"; - commentToolStripMenuItem.Size = new Size(157, 26); + commentToolStripMenuItem.Size = new Size(151, 22); commentToolStripMenuItem.Text = "Комментарии"; commentToolStripMenuItem.Click += commentToolStripMenuItem_Click; // // tagToolStripMenuItem // tagToolStripMenuItem.Name = "tagToolStripMenuItem"; - tagToolStripMenuItem.Size = new Size(157, 26); + tagToolStripMenuItem.Size = new Size(151, 22); tagToolStripMenuItem.Text = "Тэги"; tagToolStripMenuItem.Click += tagToolStripMenuItem_Click; // // замерВремениToolStripMenuItem // замерВремениToolStripMenuItem.Name = "замерВремениToolStripMenuItem"; - замерВремениToolStripMenuItem.Size = new Size(14, 24); + замерВремениToolStripMenuItem.Size = new Size(12, 20); // // замерыВремениToolStripMenuItem // - замерыВремениToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { add1000ToolStripMenuItem, get1000ToolStripMenuItem, update1000ToolStripMenuItem, delete1000ToolStripMenuItem }); + замерыВремениToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { add1000ToolStripMenuItem, get1000ToolStripMenuItem, update1000ToolStripMenuItem, delete1000ToolStripMenuItem, updateMongoDBToolStripMenuItem }); замерыВремениToolStripMenuItem.Name = "замерыВремениToolStripMenuItem"; - замерыВремениToolStripMenuItem.Size = new Size(144, 24); + замерыВремениToolStripMenuItem.Size = new Size(114, 20); замерыВремениToolStripMenuItem.Text = "Замеры времени"; // // add1000ToolStripMenuItem // add1000ToolStripMenuItem.Name = "add1000ToolStripMenuItem"; - add1000ToolStripMenuItem.Size = new Size(224, 26); + add1000ToolStripMenuItem.Size = new Size(185, 22); add1000ToolStripMenuItem.Text = "Добавление 1000"; add1000ToolStripMenuItem.Click += add1000ToolStripMenuItem_Click; // // get1000ToolStripMenuItem // get1000ToolStripMenuItem.Name = "get1000ToolStripMenuItem"; - get1000ToolStripMenuItem.Size = new Size(224, 26); + get1000ToolStripMenuItem.Size = new Size(185, 22); get1000ToolStripMenuItem.Text = "Получение 1000"; get1000ToolStripMenuItem.Click += get1000ToolStripMenuItem_Click; // // update1000ToolStripMenuItem // update1000ToolStripMenuItem.Name = "update1000ToolStripMenuItem"; - update1000ToolStripMenuItem.Size = new Size(224, 26); + update1000ToolStripMenuItem.Size = new Size(185, 22); update1000ToolStripMenuItem.Text = "Обновление 1000"; update1000ToolStripMenuItem.Click += update1000ToolStripMenuItem_Click; // // delete1000ToolStripMenuItem // delete1000ToolStripMenuItem.Name = "delete1000ToolStripMenuItem"; - delete1000ToolStripMenuItem.Size = new Size(224, 26); + delete1000ToolStripMenuItem.Size = new Size(185, 22); delete1000ToolStripMenuItem.Text = "Удаление 1000"; delete1000ToolStripMenuItem.Click += delete1000ToolStripMenuItem_Click; // + // updateMongoDBToolStripMenuItem + // + updateMongoDBToolStripMenuItem.Name = "updateMongoDBToolStripMenuItem"; + updateMongoDBToolStripMenuItem.Size = new Size(185, 22); + updateMongoDBToolStripMenuItem.Text = "Обновить MongoDB"; + updateMongoDBToolStripMenuItem.Click += updateMongoDBToolStripMenuItem_Click; + // // labelTest // labelTest.BorderStyle = BorderStyle.FixedSingle; - labelTest.Location = new Point(178, 109); + labelTest.Location = new Point(156, 82); labelTest.Name = "labelTest"; - labelTest.Size = new Size(285, 133); + labelTest.Size = new Size(250, 100); labelTest.TabIndex = 3; labelTest.TextAlign = ContentAlignment.MiddleCenter; // + // MongoDB + // + MongoDB.AutoSize = true; + MongoDB.Location = new Point(459, 230); + MongoDB.Name = "MongoDB"; + MongoDB.Size = new Size(80, 19); + MongoDB.TabIndex = 4; + MongoDB.Text = "MongoDB"; + MongoDB.UseVisualStyleBackColor = true; + MongoDB.CheckedChanged += MongoDB_CheckedChanged; + // // FormMain // - AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(633, 348); + ClientSize = new Size(554, 261); + Controls.Add(MongoDB); Controls.Add(labelTest); Controls.Add(menuStrip1); MainMenuStrip = menuStrip1; - Margin = new Padding(3, 4, 3, 4); Name = "FormMain"; StartPosition = FormStartPosition.CenterScreen; Text = "Электронный дневник"; @@ -181,5 +201,6 @@ private ToolStripMenuItem get1000ToolStripMenuItem; private ToolStripMenuItem update1000ToolStripMenuItem; private ToolStripMenuItem delete1000ToolStripMenuItem; + private ToolStripMenuItem updateMongoDBToolStripMenuItem; } } diff --git a/NewsBlog/NewsBlogView/FormMain.cs b/NewsBlog/NewsBlogView/FormMain.cs index 0ca8c5e..8c1eb8c 100644 --- a/NewsBlog/NewsBlogView/FormMain.cs +++ b/NewsBlog/NewsBlogView/FormMain.cs @@ -1,5 +1,6 @@ using NewsBlogAbstractions.Models; using NewsBlogAbstractions.WorkAbstractions; +using NewsBlogMongoDB; using System.Security.Policy; namespace NewsBlogView @@ -85,7 +86,7 @@ namespace NewsBlogView if (service is IAuthorWork authorLogic) { DateTime startTime = DateTime.Now; - for (int i =0; i < 1000; i++) + for (int i = 0; i < 1000; i++) authorLogic.Get(i + 1000); DateTime endTime = DateTime.Now; @@ -131,5 +132,73 @@ namespace NewsBlogView labelTest.Text = $" 1000 {(endTime - startTime).TotalMilliseconds} "; } } + + private void updateMongoDBToolStripMenuItem_Click(object sender, EventArgs e) + { + ImplementationMongoDB implementationMongoDB = new(); + + // + foreach (var it in implementationMongoDB.GetArticles()) + implementationMongoDB.DeleteArticle(it.Id); + foreach (var it in implementationMongoDB.GetAuthors()) + implementationMongoDB.DeleteAuthor(it.Id); + foreach (var it in implementationMongoDB.GetCategorys()) + implementationMongoDB.DeleteCategory(it.Id); + foreach (var it in implementationMongoDB.GetComments()) + implementationMongoDB.DeleteComment(it.Id); + foreach (var it in implementationMongoDB.GetTags()) + implementationMongoDB.DeleteTag(it.Id); + foreach (var it in implementationMongoDB.GetSequence()) + implementationMongoDB.DeleteSequence(it.Id); + + // + List
listArticle = null; + List listAuthor = null; + List listCategory = null; + List listComment = null; + List listTag = null; + + var service = Program.ServiceProvider?.GetService(typeof(IArticleWork)); + if (service is IArticleWork articleLogic) + listArticle = articleLogic.GetAll(); + + service = Program.ServiceProvider?.GetService(typeof(IAuthorWork)); + if (service is IAuthorWork authorLogic) + listAuthor = authorLogic.GetAll(); + + service = Program.ServiceProvider?.GetService(typeof(ICategoryWork)); + if (service is ICategoryWork categoryLogic) + listCategory = categoryLogic.GetAll(); + + service = Program.ServiceProvider?.GetService(typeof(ICommentWork)); + if (service is ICommentWork commentLogic) + listComment = commentLogic.GetAll(); + + service = Program.ServiceProvider?.GetService(typeof(ITagWork)); + if (service is ITagWork tagLogic) + listTag = tagLogic.GetAll(); + + // + if (listAuthor != null) + foreach (var it in listAuthor) + implementationMongoDB.AddAuthor(it); + if (listCategory != null) + foreach (var it in listCategory) + implementationMongoDB.AddCategory(it); + if (listArticle != null) + foreach (var it in listArticle) + implementationMongoDB.AddArticle(it); + if (listComment != null) + foreach (var it in listComment) + implementationMongoDB.AddComment(it); + if (listTag != null) + foreach (var it in listTag) + implementationMongoDB.AddTag(it); + } + + private void MongoDB_CheckedChanged(object sender, EventArgs e) + { + Program.isPostgreSQL = !Program.isPostgreSQL; + } } } diff --git a/NewsBlog/NewsBlogView/FormMain.resx b/NewsBlog/NewsBlogView/FormMain.resx index 1af7de1..f4ea782 100644 --- a/NewsBlog/NewsBlogView/FormMain.resx +++ b/NewsBlog/NewsBlogView/FormMain.resx @@ -1,17 +1,17 @@  - @@ -117,4 +117,10 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + False + \ No newline at end of file diff --git a/NewsBlog/NewsBlogView/FormTag.cs b/NewsBlog/NewsBlogView/FormTag.cs index 9e3b9b2..40f37c3 100644 --- a/NewsBlog/NewsBlogView/FormTag.cs +++ b/NewsBlog/NewsBlogView/FormTag.cs @@ -1,5 +1,7 @@ using NewsBlogAbstractions.Models; using NewsBlogAbstractions.WorkAbstractions; +using NewsBlogMongoDB.Models; +using NewsBlogMongoDB.StoragesContracts; using System; using System.Collections.Generic; using System.ComponentModel; @@ -16,12 +18,14 @@ namespace NewsBlogView { private readonly ITagWork _tagLogic; private readonly IArticleWork _articleLogic; + private readonly StorageModel _mongoLogic; - public FormTag(ITagWork logic, IArticleWork articleLogic) + public FormTag(ITagWork logic, IArticleWork articleLogic, StorageModel mongoLogic) { InitializeComponent(); _tagLogic = logic; _articleLogic = articleLogic; + _mongoLogic = mongoLogic; } private void FormTag_Load(object sender, EventArgs e) @@ -38,15 +42,16 @@ namespace NewsBlogView ArticleId = ((Article?)comboBoxArticle.SelectedItem)?.Id ?? 0, }; - _tagLogic.Create(newTag); + if (Program.isPostgreSQL) + _tagLogic.Create(newTag); + else + _mongoLogic.AddTag(newTag); LoadData(); } private void LoadData() { - var tags = _tagLogic.GetAll(); - dataGridView.Rows.Clear(); if (dataGridView.ColumnCount == 0) @@ -68,9 +73,21 @@ namespace NewsBlogView comboBoxArticle.DisplayMember = "Article"; comboBoxArticle.ValueMember = "Title"; - foreach (var tag in tags) + if (Program.isPostgreSQL) { - dataGridView.Rows.Add(tag.Id, tag.Name, tag.Description, tag.ArticleId, _articleLogic.Get(tag.ArticleId)?.Title); + var tags = _tagLogic.GetAll(); + foreach (var tag in tags) + { + dataGridView.Rows.Add(tag.Id, tag.Name, tag.Description, tag.ArticleId, _articleLogic.Get(tag.ArticleId)?.Title); + } + } + else + { + var tags = _mongoLogic.GetTags(); + foreach (var tag in tags) + { + dataGridView.Rows.Add(tag.Id, tag.Name, tag.Decsription, tag.Article_id, _mongoLogic.GetArticleById(tag.Article_id)?.Title); + } } } @@ -79,17 +96,34 @@ namespace NewsBlogView if (dataGridView.SelectedRows.Count > 0) { DataGridViewRow selectedRow = dataGridView.SelectedRows[0]; - int tagId = Convert.ToInt32(selectedRow.Cells["Id"].Value); - - Tag updatedTag = new() + if (Program.isPostgreSQL) { - Id = tagId, - Name = textBoxName.Text, - Description = textBoxDescription.Text, - ArticleId = ((Article?)comboBoxArticle.SelectedItem)?.Id ?? 0, - }; + int tagId = Convert.ToInt32(selectedRow.Cells["Id"].Value); - _tagLogic.Update(updatedTag); + Tag updatedTag = new() + { + Id = tagId, + Name = textBoxName.Text, + Description = textBoxDescription.Text, + ArticleId = ((Article?)comboBoxArticle.SelectedItem)?.Id ?? 0, + }; + + _tagLogic.Update(updatedTag); + } + else + { + string? tagId = selectedRow.Cells["Id"].Value.ToString(); + + TagMongo updatedTag = new() + { + Id = tagId, + Name = textBoxName.Text, + Decsription = textBoxDescription.Text, + Article_id = _mongoLogic.GetMongoId("Article", ((Article?)comboBoxArticle.SelectedItem)?.Id.ToString() ?? String.Empty), + }; + + _mongoLogic.UpdateTag(updatedTag); + } LoadData(); } @@ -104,9 +138,18 @@ namespace NewsBlogView if (dataGridView.SelectedRows.Count > 0) { DataGridViewRow selectedRow = dataGridView.SelectedRows[0]; - int tagId = Convert.ToInt32(selectedRow.Cells["Id"].Value); + if (Program.isPostgreSQL) + { + int tagId = Convert.ToInt32(selectedRow.Cells["Id"].Value); - _tagLogic.Delete(tagId); + _tagLogic.Delete(tagId); + } + else + { + string? tagId = selectedRow.Cells["Id"].Value.ToString(); + + _mongoLogic.DeleteTag(tagId); + } LoadData(); } diff --git a/NewsBlog/NewsBlogView/NewsBlogView.csproj b/NewsBlog/NewsBlogView/NewsBlogView.csproj index 540e8f2..691e4ac 100644 --- a/NewsBlog/NewsBlogView/NewsBlogView.csproj +++ b/NewsBlog/NewsBlogView/NewsBlogView.csproj @@ -15,6 +15,7 @@ + \ No newline at end of file diff --git a/NewsBlog/NewsBlogView/Program.cs b/NewsBlog/NewsBlogView/Program.cs index 5edbf96..422fba5 100644 --- a/NewsBlog/NewsBlogView/Program.cs +++ b/NewsBlog/NewsBlogView/Program.cs @@ -1,6 +1,8 @@ using Microsoft.Extensions.DependencyInjection; using NewsBlogAbstractions.WorkAbstractions; using NewsBlogDatabaseImplementation.WorkImplementation; +using NewsBlogMongoDB.StoragesContracts; +using NewsBlogMongoDB; namespace NewsBlogView { @@ -9,6 +11,8 @@ namespace NewsBlogView private static ServiceProvider? _serviceProvider; public static ServiceProvider? ServiceProvider => _serviceProvider; + public static bool isPostgreSQL = true; + /// /// The main entry point for the application. /// @@ -32,6 +36,8 @@ namespace NewsBlogView services.AddTransient(); services.AddTransient(); + services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient();