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 ScheduleStorage : IScheduleStorage
{
///
/// Получение полного списка
///
///
public List GetFullList()
{
using var context = new RouteGuideDatabase();
var filterBuilder = Builders.Filter;
var filter = filterBuilder.Empty;
return context.Schedules
.Find(filter)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
///
/// Получение фильтрованного списка
///
///
///
public List GetFilteredList(ScheduleSearchModel model)
{
if (string.IsNullOrEmpty(model.Id?.ToString()))
{
return new();
}
using var context = new RouteGuideDatabase();
var filterBuilder = Builders.Filter;
var filter = filterBuilder.Eq(x => x.Id, model.Id);
return context.Schedules
.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.Schedules
.Find(filter)
.Limit(count)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
///
/// Получение элемента
///
///
///
public ScheduleViewModel? GetElement(ScheduleSearchModel model)
{
if (string.IsNullOrEmpty(model.Id?.ToString()))
{
return null;
}
using var context = new RouteGuideDatabase();
var filterBuilder = Builders.Filter;
var filter = filterBuilder.Eq(x => x.Id, model.Id);
return context.Schedules
.Find(filter)
.FirstOrDefault()
?.GetViewModel;
}
///
/// Добавление элемента
///
///
///
public ScheduleViewModel? Insert(ScheduleBindingModel model)
{
using var context = new RouteGuideDatabase();
var newSchedule = Schedule.Create(context, model);
if (newSchedule == null)
{
return null;
}
context.Schedules.InsertOne(newSchedule);
return newSchedule.GetViewModel;
}
///
/// Редактирование элемента
///
///
///
public ScheduleViewModel? Update(ScheduleBindingModel model)
{
using var context = new RouteGuideDatabase();
var filterBuilder = Builders.Filter;
var filter = filterBuilder.Eq(x => x.Id, model.Id);
var schedule = context.Schedules.Find(filter).FirstOrDefault();
if (schedule == null)
{
return null;
}
schedule.Update(model);
context.Schedules.ReplaceOne(filter, schedule);
return schedule.GetViewModel;
}
///
/// Удаление элемента
///
///
///
public ScheduleViewModel? Delete(ScheduleBindingModel model)
{
using var context = new RouteGuideDatabase();
var filterBuilder = Builders.Filter;
var filter = filterBuilder.Eq(x => x.Id, model.Id);
var schedule = context.Schedules.Find(filter).FirstOrDefault();
if (schedule == null)
{
return null;
}
context.Schedules.FindOneAndDelete(filter);
return schedule.GetViewModel;
}
///
/// Удаление записи
///
///
public ScheduleViewModel? Delete()
{
if (!GetFullList().Any())
{
return null;
}
using var context = new RouteGuideDatabase();
var filterBuilder = Builders.Filter;
var filter = filterBuilder.Empty;
var element = context.Schedules.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.Schedules.DeleteMany(filter);
return count;
}
}
}