126 lines
3.7 KiB
C#
126 lines
3.7 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 StopStorage : IStopStorage
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// Получение полного списка
|
|||
|
/// </summary>
|
|||
|
/// <returns></returns>
|
|||
|
public List<StopViewModel> GetFullList()
|
|||
|
{
|
|||
|
using var context = new RouteGuideDatabase();
|
|||
|
return context.Stops
|
|||
|
.Select(x => x.GetViewModel)
|
|||
|
.ToList();
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Получение фильтрованного списка
|
|||
|
/// </summary>
|
|||
|
/// <param name="model"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public List<StopViewModel> GetFilteredList(StopSearchModel model)
|
|||
|
{
|
|||
|
if (!model.Id.HasValue)
|
|||
|
{
|
|||
|
return new();
|
|||
|
}
|
|||
|
|
|||
|
using var context = new RouteGuideDatabase();
|
|||
|
return context.Stops
|
|||
|
.Where(x => x.Id == model.Id)
|
|||
|
.Select(x => x.GetViewModel)
|
|||
|
.ToList();
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Получение элемента
|
|||
|
/// </summary>
|
|||
|
/// <param name="model"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public StopViewModel? GetElement(StopSearchModel model)
|
|||
|
{
|
|||
|
if (!model.Id.HasValue)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
using var context = new RouteGuideDatabase();
|
|||
|
return context.Stops
|
|||
|
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))
|
|||
|
?.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.Add(newStop);
|
|||
|
context.SaveChanges();
|
|||
|
return newStop.GetViewModel;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Редактирование элемента
|
|||
|
/// </summary>
|
|||
|
/// <param name="model"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public StopViewModel? Update(StopBindingModel model)
|
|||
|
{
|
|||
|
using var context = new RouteGuideDatabase();
|
|||
|
var stop = context.Stops.FirstOrDefault(x => x.Id == model.Id);
|
|||
|
if (stop == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
stop.Update(model);
|
|||
|
context.SaveChanges();
|
|||
|
return stop.GetViewModel;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Удаление элемента
|
|||
|
/// </summary>
|
|||
|
/// <param name="model"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public StopViewModel? Delete(StopBindingModel model)
|
|||
|
{
|
|||
|
using var context = new RouteGuideDatabase();
|
|||
|
var stop = context.Stops.FirstOrDefault(x => x.Id == model.Id);
|
|||
|
if (stop == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
context.Stops.Remove(stop);
|
|||
|
context.SaveChanges();
|
|||
|
return stop.GetViewModel;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|