142 lines
3.2 KiB
C#
142 lines
3.2 KiB
C#
|
using ForumContracts.BindingModels;
|
|||
|
using ForumContracts.SearchModels;
|
|||
|
using ForumContracts.StorageContracts;
|
|||
|
using ForumContracts.ViewModels;
|
|||
|
using ForumDatabaseImplement.Models;
|
|||
|
using MongoDB.Bson;
|
|||
|
using MongoDB.Driver;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace ForumMongoDataBase.Implements
|
|||
|
{
|
|||
|
public class QuestionStorage : IQuestionStorage
|
|||
|
{
|
|||
|
public MongoClient context;
|
|||
|
private readonly IMongoCollection<Question> _collection;
|
|||
|
|
|||
|
public QuestionStorage()
|
|||
|
{
|
|||
|
context = ForumMongoDataBase.getInstance().client;
|
|||
|
_collection = context.GetDatabase("forum").GetCollection<Question>("questions");
|
|||
|
}
|
|||
|
|
|||
|
public QuestionViewModel? Delete(QuestionBindingModel model)
|
|||
|
{
|
|||
|
var filter = Builders<Question>.Filter.Eq(q => q.Id, model.Id);
|
|||
|
var question = _collection.FindOneAndDelete(filter);
|
|||
|
|
|||
|
if (question == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
return question.GetViewModel();
|
|||
|
}
|
|||
|
|
|||
|
public QuestionViewModel? GetElement(QuestionSearchModel model)
|
|||
|
{
|
|||
|
if (!model.Id.HasValue)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
var question = _collection.Find(q => q.Id == model.Id.Value)
|
|||
|
.FirstOrDefault();
|
|||
|
|
|||
|
if (question == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
return question.GetViewModel();
|
|||
|
}
|
|||
|
|
|||
|
public List<QuestionViewModel> GetFilteredList(QuestionSearchModel model)
|
|||
|
{
|
|||
|
var filter = Builders<Question>.Filter.Empty;
|
|||
|
|
|||
|
if (model.Id.HasValue)
|
|||
|
{
|
|||
|
filter &= Builders<Question>.Filter.Eq(q => q.Id, model.Id.Value);
|
|||
|
}
|
|||
|
|
|||
|
if (model.CategoryId.HasValue)
|
|||
|
{
|
|||
|
filter &= Builders<Question>.Filter.Eq(q => q.CategoryId, model.CategoryId.Value);
|
|||
|
}
|
|||
|
|
|||
|
if (model.UserId.HasValue)
|
|||
|
{
|
|||
|
filter &= Builders<Question>.Filter.Eq(q => q.UserId, model.UserId.Value);
|
|||
|
}
|
|||
|
|
|||
|
var questions = _collection.Find(filter)
|
|||
|
.ToList()
|
|||
|
.Select(q => q.GetViewModel())
|
|||
|
.ToList();
|
|||
|
|
|||
|
return questions;
|
|||
|
}
|
|||
|
|
|||
|
public List<QuestionViewModel> HardRequest(QuestionSearchModel model)
|
|||
|
{
|
|||
|
var filter = Builders<Question>.Filter.And(
|
|||
|
Builders<Question>.Filter.Gte(q => q.CreateDate, model.DateFrom),
|
|||
|
Builders<Question>.Filter.Lte(q => q.CreateDate, model.DateTo));
|
|||
|
|
|||
|
var questions = _collection.Find(filter)
|
|||
|
.SortBy(q => q.CreateDate)
|
|||
|
.ToList()
|
|||
|
.Select(q => q.GetViewModel())
|
|||
|
.ToList();
|
|||
|
|
|||
|
return questions;
|
|||
|
}
|
|||
|
|
|||
|
public List<QuestionViewModel> GetFullList()
|
|||
|
{
|
|||
|
var questions = _collection.Find(_ => true)
|
|||
|
.ToList()
|
|||
|
.Select(q => q.GetViewModel())
|
|||
|
.ToList();
|
|||
|
|
|||
|
return questions;
|
|||
|
}
|
|||
|
|
|||
|
public QuestionViewModel? Insert(QuestionBindingModel model)
|
|||
|
{
|
|||
|
var question = Question.Create(model);
|
|||
|
|
|||
|
if (question == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
_collection.InsertOne(question);
|
|||
|
|
|||
|
return question.GetViewModel();
|
|||
|
}
|
|||
|
|
|||
|
public QuestionViewModel? Update(QuestionBindingModel model)
|
|||
|
{
|
|||
|
var filter = Builders<Question>.Filter.Eq(q => q.Id, model.Id);
|
|||
|
var question = _collection.Find(filter).FirstOrDefault();
|
|||
|
|
|||
|
if (question == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
question.Update(model);
|
|||
|
_collection.ReplaceOne(filter, question);
|
|||
|
|
|||
|
return question.GetViewModel();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|