using MongoDB.Driver;
using RouteGuideContracts.BindingModels;
using RouteGuideContracts.SearchModels;
using RouteGuideContracts.StoragesContracts;
using RouteGuideContracts.ViewModels;
using RouteGuideMongoDBImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RouteGuideMongoDBImplement.Implements
{
///
/// Хранилище для сущности "Остановка"
///
public class StopStorage : IStopStorage
{
///
/// Получение полного списка
///
///
public List GetFullList()
{
using var context = new RouteGuideDatabase();
var filterBuilder = Builders.Filter;
var filter = filterBuilder.Empty;
return context.Stops
.Find(filter)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
///
/// Получение фильтрованного списка
///
///
///
public List GetFilteredList(StopSearchModel model)
{
if (string.IsNullOrEmpty(model.Name))
{
return new();
}
using var context = new RouteGuideDatabase();
var filterBuilder = Builders.Filter;
var filter = filterBuilder.Eq(x => x.Name, model.Name);
return context.Stops
.Find(filter)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
///
/// Получить список из заданного количества элементов
///
///
///
public List GetList(int count)
{
if (count <= 0)
{
return new();
}
using var context = new RouteGuideDatabase();
var filterBuilder = Builders.Filter;
var filter = filterBuilder.Empty;
return context.Stops
.Find(filter)
.Limit(count)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
///
/// Получение элемента
///
///
///
public StopViewModel? GetElement(StopSearchModel model)
{
if (string.IsNullOrEmpty(model.Id?.ToString()) && string.IsNullOrEmpty(model.Name))
{
return null;
}
using var context = new RouteGuideDatabase();
var filterBuilder = Builders.Filter;
var filter = filterBuilder.Empty;
if (!string.IsNullOrEmpty(model.Name))
{
filter &= filterBuilder.Eq(x => x.Name, model.Name);
}
if (!string.IsNullOrEmpty(model.Id?.ToString()))
{
filter &= filterBuilder.Eq(x => x.Id, model.Id);
}
return context.Stops
.Find(filter)
.FirstOrDefault()
?.GetViewModel;
}
///
/// Добавление элемента
///
///
///
public StopViewModel? Insert(StopBindingModel model)
{
var newStop = Stop.Create(model);
if (newStop == null)
{
return null;
}
using var context = new RouteGuideDatabase();
context.Stops.InsertOne(newStop);
return newStop.GetViewModel;
}
///
/// Редактирование элемента
///
///
///
public StopViewModel? Update(StopBindingModel model)
{
using var context = new RouteGuideDatabase();
var filterBuilder = Builders.Filter;
var filter = filterBuilder.Eq(x => x.Id, model.Id);
var stop = context.Stops.Find(filter).FirstOrDefault();
if (stop == null)
{
return null;
}
stop.Update(model);
context.Stops.ReplaceOne(filter, stop);
return stop.GetViewModel;
}
///
/// Удаление элемента
///
///
///
public StopViewModel? Delete(StopBindingModel model)
{
using var context = new RouteGuideDatabase();
var filterBuilder = Builders.Filter;
var filter = filterBuilder.Eq(x => x.Id, model.Id);
var stop = context.Stops.Find(filter).FirstOrDefault();
if (stop == null)
{
return null;
}
context.Stops.FindOneAndDelete(filter);
return stop.GetViewModel;
}
///
/// Удаление записи
///
///
public StopViewModel? Delete()
{
if (!GetFullList().Any())
{
return null;
}
using var context = new RouteGuideDatabase();
var filterBuilder = Builders.Filter;
var filter = filterBuilder.Empty;
var element = context.Stops.FindOneAndDelete(filter);
return element.GetViewModel;
}
///
/// Удаление всех элементов
///
///
public int Clear()
{
using var context = new RouteGuideDatabase();
int count = GetFullList().Count;
if (count <= 0)
{
return 0;
}
var filterBuilder = Builders.Filter;
var filter = filterBuilder.Empty;
context.Stops.DeleteMany(filter);
return count;
}
}
}