223 lines
5.2 KiB
C#
223 lines
5.2 KiB
C#
using MongoDB.Driver;
|
|
using MongoDB.Bson;
|
|
using RouteGuideContracts.BindingModels;
|
|
using RouteGuideContracts.SearchModels;
|
|
using RouteGuideContracts.StoragesContracts;
|
|
using RouteGuideContracts.ViewModels;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using RouteGuideMongoDBImplement.Models;
|
|
|
|
namespace RouteGuideMongoDBImplement.Implements
|
|
{
|
|
/// <summary>
|
|
/// Хранилище для сущности "Водитель"
|
|
/// </summary>
|
|
public class DriverStorage : IDriverStorage
|
|
{
|
|
/// <summary>
|
|
/// Получение полного списка
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public List<DriverViewModel> GetFullList()
|
|
{
|
|
using var context = new RouteGuideDatabase();
|
|
|
|
var filterBuilder = Builders<Driver>.Filter;
|
|
var filter = filterBuilder.Empty;
|
|
|
|
return context.Drivers
|
|
.Find(filter)
|
|
.ToList()
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Получение фильтрованного списка
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public List<DriverViewModel> GetFilteredList(DriverSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.Phone))
|
|
{
|
|
return new();
|
|
}
|
|
|
|
using var context = new RouteGuideDatabase();
|
|
|
|
var filterBuilder = Builders<Driver>.Filter;
|
|
var filter = filterBuilder.Eq(x => x.Phone, model.Phone);
|
|
|
|
return context.Drivers
|
|
.Find(filter)
|
|
.ToList()
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Получить список из заданного количества элементов
|
|
/// </summary>
|
|
/// <param name="count"></param>
|
|
/// <returns></returns>
|
|
public List<DriverViewModel> GetList(int count)
|
|
{
|
|
if (count <= 0)
|
|
{
|
|
return new();
|
|
}
|
|
|
|
using var context = new RouteGuideDatabase();
|
|
|
|
var filterBuilder = Builders<Driver>.Filter;
|
|
var filter = filterBuilder.Empty;
|
|
|
|
return context.Drivers
|
|
.Find(filter)
|
|
.Limit(count)
|
|
.ToList()
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Получение элемента
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public DriverViewModel? GetElement(DriverSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.Id?.ToString()) && string.IsNullOrEmpty(model.Phone))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
using var context = new RouteGuideDatabase();
|
|
|
|
var filterBuilder = Builders<Driver>.Filter;
|
|
var filter = filterBuilder.Empty;
|
|
if (!string.IsNullOrEmpty(model.Phone))
|
|
{
|
|
filter &= filterBuilder.Eq(x => x.Phone, model.Phone);
|
|
}
|
|
if (!string.IsNullOrEmpty(model.Id?.ToString()))
|
|
{
|
|
filter &= filterBuilder.Eq(x => x.Id, model.Id);
|
|
}
|
|
|
|
return context.Drivers
|
|
.Find(filter)
|
|
.FirstOrDefault()
|
|
?.GetViewModel;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Добавление элемента
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public DriverViewModel? Insert(DriverBindingModel model)
|
|
{
|
|
var newDriver = Driver.Create(model);
|
|
if (newDriver == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
using var context = new RouteGuideDatabase();
|
|
context.Drivers.InsertOne(newDriver);
|
|
return newDriver.GetViewModel;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Редактирование элемента
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public DriverViewModel? Update(DriverBindingModel model)
|
|
{
|
|
using var context = new RouteGuideDatabase();
|
|
|
|
var filterBuilder = Builders<Driver>.Filter;
|
|
var filter = filterBuilder.Eq(x => x.Id, model.Id);
|
|
|
|
var driver = context.Drivers.Find(filter).FirstOrDefault();
|
|
if (driver == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
driver.Update(model);
|
|
context.Drivers.ReplaceOne(filter, driver);
|
|
return driver.GetViewModel;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Удаление элемента
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public DriverViewModel? Delete(DriverBindingModel model)
|
|
{
|
|
using var context = new RouteGuideDatabase();
|
|
|
|
var filterBuilder = Builders<Driver>.Filter;
|
|
var filter = filterBuilder.Eq(x => x.Id, model.Id);
|
|
|
|
var driver = context.Drivers.Find(filter).FirstOrDefault();
|
|
if (driver == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
context.Drivers.FindOneAndDelete(filter);
|
|
return driver.GetViewModel;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Удаление записи
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public DriverViewModel? Delete()
|
|
{
|
|
if (!GetFullList().Any())
|
|
{
|
|
return null;
|
|
}
|
|
|
|
using var context = new RouteGuideDatabase();
|
|
|
|
var filterBuilder = Builders<Driver>.Filter;
|
|
var filter = filterBuilder.Empty;
|
|
|
|
var element = context.Drivers.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<Driver>.Filter;
|
|
var filter = filterBuilder.Empty;
|
|
|
|
context.Drivers.DeleteMany(filter);
|
|
return count;
|
|
}
|
|
}
|
|
}
|