115 lines
3.6 KiB
C#
115 lines
3.6 KiB
C#
using MongoDB.Bson;
|
|
using MongoDB.Driver;
|
|
using SushiBarContracts.BindingModels;
|
|
using SushiBarContracts.SearchModels;
|
|
using SushiBarContracts.StoragesContracts;
|
|
using SushiBarContracts.ViewModels;
|
|
using SushiBarMongoDB.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace SushiBarMongoDB.Implements
|
|
{
|
|
public class CookStorage : ICookStorage
|
|
{
|
|
|
|
public void ClearEntity()
|
|
{
|
|
using var context = new SushiBarMongoDB();
|
|
context.GetCollection<Cook>("Cooks")
|
|
.DeleteMany(Builders<Cook>.Filter.Empty);
|
|
}
|
|
|
|
public List<CookViewModel> GetFullList()
|
|
{
|
|
using var context = new SushiBarMongoDB();
|
|
var cooks = context.GetCollection<Cook>("Cooks");
|
|
return cooks.Find(Builders<Cook>.Filter.Empty)
|
|
.ToList()
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
public List<CookViewModel> GetFilteredList(CookSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.CookName))
|
|
{
|
|
return new List<CookViewModel>();
|
|
}
|
|
using var context = new SushiBarMongoDB();
|
|
var cooks = context.GetCollection<Cook>("Cooks");
|
|
|
|
var filterBuilder = Builders<Cook>.Filter;
|
|
var filter = filterBuilder
|
|
.Regex(x => x.CookName, new BsonRegularExpression(model.CookName));
|
|
return cooks
|
|
.Find(filter)
|
|
.ToList()
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public CookViewModel? GetElement(CookSearchModel model)
|
|
{
|
|
if (!model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
using (var context = new SushiBarMongoDB())
|
|
{
|
|
var cooks = context.GetCollection<Cook>("Cooks");
|
|
|
|
var filterBuilder = Builders<Cook>.Filter;
|
|
var filter = filterBuilder.Empty;
|
|
|
|
if (!model.Id.HasValue)
|
|
{
|
|
filter &= filterBuilder.Eq(x => x.Id, model.Id);
|
|
}
|
|
|
|
return cooks.Find(filter)
|
|
.FirstOrDefault()
|
|
?.GetViewModel;
|
|
}
|
|
}
|
|
public CookViewModel? Insert(CookBindingModel model)
|
|
{
|
|
using var context = new SushiBarMongoDB();
|
|
|
|
var cooks = context.GetCollection<Cook>("Cooks");
|
|
|
|
model.Id = (int)cooks.CountDocuments(FilterDefinition<Cook>.Empty) + 1;
|
|
|
|
var cook = Cook.Create(model);
|
|
cooks.InsertOne(cook);
|
|
return cook.GetViewModel;
|
|
}
|
|
public CookViewModel? Update(CookBindingModel model)
|
|
{
|
|
using var context = new SushiBarMongoDB();
|
|
var cooks = context.GetCollection<Cook>("Cooks");
|
|
|
|
var filter = Builders<Cook>.Filter.Eq(x => x.Id, model.Id);
|
|
var cook = cooks.Find(filter).FirstOrDefault();
|
|
if (cook == null)
|
|
{
|
|
return null;
|
|
}
|
|
cook.Update(model);
|
|
cooks.ReplaceOne(filter, cook);
|
|
return cook.GetViewModel;
|
|
}
|
|
public CookViewModel? Delete(CookBindingModel model)
|
|
{
|
|
using var context = new SushiBarMongoDB();
|
|
var cooks = context.GetCollection<Cook>("Cooks");
|
|
|
|
var filter = Builders<Cook>.Filter.Eq(x => x.Id, model.Id);
|
|
var cook = cooks.FindOneAndDelete(filter);
|
|
return cook?.GetViewModel;
|
|
}
|
|
}
|
|
}
|