90 lines
3.1 KiB
C#
90 lines
3.1 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.Xml;
|
|||
|
using Task = SushiBarMongoDB.Models.Task;
|
|||
|
|
|||
|
namespace SushiBarMongoDB.Implements
|
|||
|
{
|
|||
|
public class TaskStorage : ITaskStorage
|
|||
|
{
|
|||
|
|
|||
|
public void ClearEntity()
|
|||
|
{
|
|||
|
using var context = new SushiBarMongoDB();
|
|||
|
context.GetCollection<Task>("Tasks")
|
|||
|
.DeleteMany(Builders<Task>.Filter.Empty);
|
|||
|
}
|
|||
|
|
|||
|
public List<TaskViewModel> GetFullList()
|
|||
|
{
|
|||
|
using var context = new SushiBarMongoDB();
|
|||
|
var tasks = context.GetCollection<Task>("Tasks").Find(Builders<Task>.Filter.Empty).ToList();
|
|||
|
|
|||
|
return tasks.Select(x => x.GetViewModel).ToList();
|
|||
|
}
|
|||
|
public List<TaskViewModel> GetFilteredList(TaskSearchModel model)
|
|||
|
{
|
|||
|
using var context = new SushiBarMongoDB();
|
|||
|
if(!model.Id.HasValue) return new List<TaskViewModel>();
|
|||
|
|
|||
|
var filterBuilder = Builders<Task>.Filter;
|
|||
|
var filter = filterBuilder.Empty;
|
|||
|
filter = filterBuilder.Eq("Id", model.Id);
|
|||
|
|
|||
|
var tasks = context.GetCollection<Task>("Tasks").Find(filter).ToList();
|
|||
|
|
|||
|
return tasks.Select(x => x.GetViewModel).ToList();
|
|||
|
}
|
|||
|
|
|||
|
public TaskViewModel? GetElement(TaskSearchModel model)
|
|||
|
{
|
|||
|
using var context = new SushiBarMongoDB();
|
|||
|
var tasks = context.GetCollection<Task>("Tasks");
|
|||
|
if (!model.Id.HasValue) return null;
|
|||
|
return tasks.Find(x => x.Id == model.Id).FirstOrDefault()?.GetViewModel;
|
|||
|
}
|
|||
|
public TaskViewModel? Insert(TaskBindingModel model)
|
|||
|
{
|
|||
|
using var context = new SushiBarMongoDB();
|
|||
|
|
|||
|
|
|||
|
var tasks = context.GetCollection<Task>("Tasks");
|
|||
|
|
|||
|
model.Id = (int)tasks.CountDocuments(FilterDefinition<Task>.Empty);
|
|||
|
|
|||
|
var newTask = Task.Create(model);
|
|||
|
|
|||
|
if (newTask == null) return null;
|
|||
|
|
|||
|
|
|||
|
tasks.InsertOne(newTask);
|
|||
|
return newTask.GetViewModel;
|
|||
|
}
|
|||
|
public TaskViewModel? Update(TaskBindingModel model)
|
|||
|
{
|
|||
|
using var context = new SushiBarMongoDB();
|
|||
|
var task = context.GetCollection<Task>("Tasks").Find(x => x.Id == model.Id).FirstOrDefault();
|
|||
|
if (task == null) return null;
|
|||
|
task.Update(model);
|
|||
|
context.GetCollection<Task>("Tasks").ReplaceOne(x => x.Id == model.Id, task);
|
|||
|
return task.GetViewModel;
|
|||
|
}
|
|||
|
public TaskViewModel? Delete(TaskBindingModel model)
|
|||
|
{
|
|||
|
using var context = new SushiBarMongoDB();
|
|||
|
var element = context.GetCollection<Task>("Tasks").Find(x => x.Id == model.Id).FirstOrDefault();
|
|||
|
if (element != null)
|
|||
|
{
|
|||
|
context.GetCollection<Task>("Tasks").DeleteOne(x => x.Id == model.Id);
|
|||
|
return element.GetViewModel;
|
|||
|
}
|
|||
|
return null;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|