214 lines
5.0 KiB
C#
214 lines
5.0 KiB
C#
|
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
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// Хранилище для сущности "Расписание"
|
|||
|
/// </summary>
|
|||
|
public class ScheduleStorage : IScheduleStorage
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// Получение полного списка
|
|||
|
/// </summary>
|
|||
|
/// <returns></returns>
|
|||
|
public List<ScheduleViewModel> GetFullList()
|
|||
|
{
|
|||
|
using var context = new RouteGuideDatabase();
|
|||
|
|
|||
|
var filterBuilder = Builders<Schedule>.Filter;
|
|||
|
var filter = filterBuilder.Empty;
|
|||
|
|
|||
|
return context.Schedules
|
|||
|
.Find(filter)
|
|||
|
.ToList()
|
|||
|
.Select(x => x.GetViewModel)
|
|||
|
.ToList();
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Получение фильтрованного списка
|
|||
|
/// </summary>
|
|||
|
/// <param name="model"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public List<ScheduleViewModel> GetFilteredList(ScheduleSearchModel model)
|
|||
|
{
|
|||
|
if (string.IsNullOrEmpty(model.Id?.ToString()))
|
|||
|
{
|
|||
|
return new();
|
|||
|
}
|
|||
|
|
|||
|
using var context = new RouteGuideDatabase();
|
|||
|
|
|||
|
var filterBuilder = Builders<Schedule>.Filter;
|
|||
|
var filter = filterBuilder.Eq(x => x.Id, model.Id);
|
|||
|
|
|||
|
return context.Schedules
|
|||
|
.Find(filter)
|
|||
|
.ToList()
|
|||
|
.Select(x => x.GetViewModel)
|
|||
|
.ToList();
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Получить список из заданного количества элементов
|
|||
|
/// </summary>
|
|||
|
/// <param name="count"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public List<ScheduleViewModel> GetList(int count)
|
|||
|
{
|
|||
|
if (count <= 0)
|
|||
|
{
|
|||
|
return new();
|
|||
|
}
|
|||
|
|
|||
|
using var context = new RouteGuideDatabase();
|
|||
|
|
|||
|
var filterBuilder = Builders<Schedule>.Filter;
|
|||
|
var filter = filterBuilder.Empty;
|
|||
|
|
|||
|
return context.Schedules
|
|||
|
.Find(filter)
|
|||
|
.Limit(count)
|
|||
|
.ToList()
|
|||
|
.Select(x => x.GetViewModel)
|
|||
|
.ToList();
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Получение элемента
|
|||
|
/// </summary>
|
|||
|
/// <param name="model"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public ScheduleViewModel? GetElement(ScheduleSearchModel model)
|
|||
|
{
|
|||
|
if (string.IsNullOrEmpty(model.Id?.ToString()))
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
using var context = new RouteGuideDatabase();
|
|||
|
|
|||
|
var filterBuilder = Builders<Schedule>.Filter;
|
|||
|
var filter = filterBuilder.Eq(x => x.Id, model.Id);
|
|||
|
|
|||
|
return context.Schedules
|
|||
|
.Find(filter)
|
|||
|
.FirstOrDefault()
|
|||
|
?.GetViewModel;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Добавление элемента
|
|||
|
/// </summary>
|
|||
|
/// <param name="model"></param>
|
|||
|
/// <returns></returns>
|
|||
|
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;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Редактирование элемента
|
|||
|
/// </summary>
|
|||
|
/// <param name="model"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public ScheduleViewModel? Update(ScheduleBindingModel model)
|
|||
|
{
|
|||
|
using var context = new RouteGuideDatabase();
|
|||
|
|
|||
|
var filterBuilder = Builders<Schedule>.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;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Удаление элемента
|
|||
|
/// </summary>
|
|||
|
/// <param name="model"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public ScheduleViewModel? Delete(ScheduleBindingModel model)
|
|||
|
{
|
|||
|
using var context = new RouteGuideDatabase();
|
|||
|
|
|||
|
var filterBuilder = Builders<Schedule>.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;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Удаление записи
|
|||
|
/// </summary>
|
|||
|
/// <returns></returns>
|
|||
|
public ScheduleViewModel? Delete()
|
|||
|
{
|
|||
|
if (!GetFullList().Any())
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
using var context = new RouteGuideDatabase();
|
|||
|
|
|||
|
var filterBuilder = Builders<Schedule>.Filter;
|
|||
|
var filter = filterBuilder.Empty;
|
|||
|
|
|||
|
var element = context.Schedules.FindOneAndDelete(filter);
|
|||
|
return element.GetViewModel;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Удаление всех элементов
|
|||
|
/// </summary>
|
|||
|
/// <returns></returns>
|
|||
|
public int Clear()
|
|||
|
{
|
|||
|
using var context = new RouteGuideDatabase();
|
|||
|
int count = GetFullList().Count;
|
|||
|
if (count <= 0)
|
|||
|
{
|
|||
|
return 0;
|
|||
|
}
|
|||
|
|
|||
|
var filterBuilder = Builders<Schedule>.Filter;
|
|||
|
var filter = filterBuilder.Empty;
|
|||
|
|
|||
|
context.Schedules.DeleteMany(filter);
|
|||
|
return count;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|