199 lines
5.5 KiB
C#
199 lines
5.5 KiB
C#
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<ArticleMongo> _articleCollection;
|
|
private IMongoCollection<AuthorMongo> _authorCollection;
|
|
private IMongoCollection<CategoryMongo> _categoryCollection;
|
|
private IMongoCollection<CommentMongo> _CommentCollection;
|
|
private IMongoCollection<TagMongo> _TagCollection;
|
|
private IMongoCollection<Sequence> _SequenceCollection;
|
|
|
|
public ImplementationMongoDB()
|
|
{
|
|
var client = new MongoClient("mongodb://localhost:27017");
|
|
_database = client.GetDatabase("NewsBlog");
|
|
|
|
_articleCollection = _database.GetCollection<ArticleMongo>("article");
|
|
_authorCollection = _database.GetCollection<AuthorMongo>("author");
|
|
_categoryCollection = _database.GetCollection<CategoryMongo>("category");
|
|
_CommentCollection = _database.GetCollection<CommentMongo>("comment");
|
|
_TagCollection = _database.GetCollection<TagMongo>("tag");
|
|
_SequenceCollection = _database.GetCollection<Sequence>("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<ArticleMongo> 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<AuthorMongo> 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<CategoryMongo> 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<CommentMongo> 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<TagMongo> 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<Sequence> 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);
|
|
}
|
|
}
|
|
}
|