120 lines
2.5 KiB
C#
120 lines
2.5 KiB
C#
|
using ForumContracts.BindingModels;
|
|||
|
using ForumContracts.SearchModels;
|
|||
|
using ForumContracts.StorageContracts;
|
|||
|
using ForumContracts.ViewModels;
|
|||
|
using MongoDB.Driver;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using ForumDatabaseImplement.Models;
|
|||
|
|
|||
|
namespace ForumMongoDataBase.Implements
|
|||
|
{
|
|||
|
public class CategoryStorage : ICategoryStorage
|
|||
|
{
|
|||
|
private readonly IMongoCollection<Category> _collection;
|
|||
|
public MongoClient context;
|
|||
|
|
|||
|
public CategoryStorage()
|
|||
|
{
|
|||
|
context = ForumMongoDataBase.getInstance().client;
|
|||
|
_collection = context.GetDatabase("forum").GetCollection<Category>("categories");
|
|||
|
}
|
|||
|
|
|||
|
public CategoryViewModel? Delete(CategoryBindingModel model)
|
|||
|
{
|
|||
|
var filter = Builders<Category>.Filter.Eq(c => c.Id, model.Id);
|
|||
|
var category = _collection.FindOneAndDelete(filter);
|
|||
|
|
|||
|
if (category == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
return category.GetViewModel();
|
|||
|
}
|
|||
|
|
|||
|
public CategoryViewModel? GetElement(CategorySearchModel model)
|
|||
|
{
|
|||
|
if (!model.Id.HasValue)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
var category = _collection.Find(c => c.Id == model.Id.Value)
|
|||
|
.FirstOrDefault();
|
|||
|
|
|||
|
if (category == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
return category.GetViewModel();
|
|||
|
}
|
|||
|
|
|||
|
public List<CategoryViewModel> GetFilteredList(CategorySearchModel model)
|
|||
|
{
|
|||
|
var filter = Builders<Category>.Filter.Empty;
|
|||
|
|
|||
|
if (model.Id.HasValue)
|
|||
|
{
|
|||
|
filter &= Builders<Category>.Filter.Eq(c => c.Id, model.Id.Value);
|
|||
|
}
|
|||
|
|
|||
|
if (model.Name != null)
|
|||
|
{
|
|||
|
filter &= Builders<Category>.Filter.Eq(c => c.Name, model.Name);
|
|||
|
}
|
|||
|
|
|||
|
var categories = _collection.Find(filter)
|
|||
|
.ToList()
|
|||
|
.Select(c => c.GetViewModel())
|
|||
|
.ToList();
|
|||
|
|
|||
|
return categories;
|
|||
|
}
|
|||
|
|
|||
|
public List<CategoryViewModel> GetFullList()
|
|||
|
{
|
|||
|
var categories = _collection.Find(_ => true)
|
|||
|
.ToList()
|
|||
|
.Select(c => c.GetViewModel())
|
|||
|
.ToList();
|
|||
|
|
|||
|
return categories;
|
|||
|
}
|
|||
|
|
|||
|
public CategoryViewModel? Insert(CategoryBindingModel model)
|
|||
|
{
|
|||
|
var category = Category.Create(model);
|
|||
|
|
|||
|
if (category == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
_collection.InsertOne(category);
|
|||
|
|
|||
|
return category.GetViewModel();
|
|||
|
}
|
|||
|
|
|||
|
public CategoryViewModel? Update(CategoryBindingModel model)
|
|||
|
{
|
|||
|
var filter = Builders<Category>.Filter.Eq(c => c.Id, model.Id);
|
|||
|
var category = _collection.Find(filter).FirstOrDefault();
|
|||
|
|
|||
|
if (category == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
category.Update(model);
|
|||
|
_collection.ReplaceOne(filter, category);
|
|||
|
|
|||
|
return category.GetViewModel();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|