126 lines
3.9 KiB
C#
126 lines
3.9 KiB
C#
using RouteGuideContracts.BindingModels;
|
|
using RouteGuideContracts.SearchModels;
|
|
using RouteGuideContracts.StoragesContracts;
|
|
using RouteGuideContracts.ViewModels;
|
|
using RouteGuideDatabaseImplement.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace RouteGuideDatabaseImplement.Implements
|
|
{
|
|
/// <summary>
|
|
/// Хранилище для сущности "Водитель"
|
|
/// </summary>
|
|
public class DriverStorage : IDriverStorage
|
|
{
|
|
/// <summary>
|
|
/// Получение полного списка
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public List<DriverViewModel> GetFullList()
|
|
{
|
|
using var context = new RouteGuideDatabase();
|
|
return context.Drivers
|
|
.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();
|
|
return context.Drivers
|
|
.Where(x => x.Phone.Contains(model.Phone))
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Получение элемента
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public DriverViewModel? GetElement(DriverSearchModel model)
|
|
{
|
|
if (!model.Id.HasValue && string.IsNullOrEmpty(model.Phone))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
using var context = new RouteGuideDatabase();
|
|
return context.Drivers
|
|
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Phone) && x.Phone.Contains(model.Phone)) || (model.Id.HasValue && x.Id == model.Id))
|
|
?.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.Add(newDriver);
|
|
context.SaveChanges();
|
|
return newDriver.GetViewModel;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Редактирование элемента
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public DriverViewModel? Update(DriverBindingModel model)
|
|
{
|
|
using var context = new RouteGuideDatabase();
|
|
var driver = context.Drivers.FirstOrDefault(x => x.Id == model.Id);
|
|
if (driver == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
driver.Update(model);
|
|
context.SaveChanges();
|
|
return driver.GetViewModel;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Удаление элемента
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public DriverViewModel? Delete(DriverBindingModel model)
|
|
{
|
|
using var context = new RouteGuideDatabase();
|
|
var driver = context.Drivers.FirstOrDefault(x => x.Id == model.Id);
|
|
if (driver == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
context.Drivers.Remove(driver);
|
|
context.SaveChanges();
|
|
return driver.GetViewModel;
|
|
}
|
|
}
|
|
}
|