222 lines
5.0 KiB
C#
222 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 StopStorage : IStopStorage
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// Получение полного списка
|
|||
|
/// </summary>
|
|||
|
/// <returns></returns>
|
|||
|
public List<StopViewModel> GetFullList()
|
|||
|
{
|
|||
|
using var context = new RouteGuideDatabase();
|
|||
|
|
|||
|
var filterBuilder = Builders<Stop>.Filter;
|
|||
|
var filter = filterBuilder.Empty;
|
|||
|
|
|||
|
return context.Stops
|
|||
|
.Find(filter)
|
|||
|
.ToList()
|
|||
|
.Select(x => x.GetViewModel)
|
|||
|
.ToList();
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Получение фильтрованного списка
|
|||
|
/// </summary>
|
|||
|
/// <param name="model"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public List<StopViewModel> GetFilteredList(StopSearchModel model)
|
|||
|
{
|
|||
|
if (string.IsNullOrEmpty(model.Name))
|
|||
|
{
|
|||
|
return new();
|
|||
|
}
|
|||
|
|
|||
|
using var context = new RouteGuideDatabase();
|
|||
|
|
|||
|
var filterBuilder = Builders<Stop>.Filter;
|
|||
|
var filter = filterBuilder.Eq(x => x.Name, model.Name);
|
|||
|
|
|||
|
return context.Stops
|
|||
|
.Find(filter)
|
|||
|
.ToList()
|
|||
|
.Select(x => x.GetViewModel)
|
|||
|
.ToList();
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Получить список из заданного количества элементов
|
|||
|
/// </summary>
|
|||
|
/// <param name="count"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public List<StopViewModel> GetList(int count)
|
|||
|
{
|
|||
|
if (count <= 0)
|
|||
|
{
|
|||
|
return new();
|
|||
|
}
|
|||
|
|
|||
|
using var context = new RouteGuideDatabase();
|
|||
|
|
|||
|
var filterBuilder = Builders<Stop>.Filter;
|
|||
|
var filter = filterBuilder.Empty;
|
|||
|
|
|||
|
return context.Stops
|
|||
|
.Find(filter)
|
|||
|
.Limit(count)
|
|||
|
.ToList()
|
|||
|
.Select(x => x.GetViewModel)
|
|||
|
.ToList();
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Получение элемента
|
|||
|
/// </summary>
|
|||
|
/// <param name="model"></param>
|
|||
|
/// <returns></returns>
|
|||
|
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<Stop>.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;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Добавление элемента
|
|||
|
/// </summary>
|
|||
|
/// <param name="model"></param>
|
|||
|
/// <returns></returns>
|
|||
|
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;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Редактирование элемента
|
|||
|
/// </summary>
|
|||
|
/// <param name="model"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public StopViewModel? Update(StopBindingModel model)
|
|||
|
{
|
|||
|
using var context = new RouteGuideDatabase();
|
|||
|
|
|||
|
var filterBuilder = Builders<Stop>.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;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Удаление элемента
|
|||
|
/// </summary>
|
|||
|
/// <param name="model"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public StopViewModel? Delete(StopBindingModel model)
|
|||
|
{
|
|||
|
using var context = new RouteGuideDatabase();
|
|||
|
|
|||
|
var filterBuilder = Builders<Stop>.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;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Удаление записи
|
|||
|
/// </summary>
|
|||
|
/// <returns></returns>
|
|||
|
public StopViewModel? Delete()
|
|||
|
{
|
|||
|
if (!GetFullList().Any())
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
using var context = new RouteGuideDatabase();
|
|||
|
|
|||
|
var filterBuilder = Builders<Stop>.Filter;
|
|||
|
var filter = filterBuilder.Empty;
|
|||
|
|
|||
|
var element = context.Stops.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<Stop>.Filter;
|
|||
|
var filter = filterBuilder.Empty;
|
|||
|
|
|||
|
context.Stops.DeleteMany(filter);
|
|||
|
return count;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|