151 lines
4.7 KiB
C#
151 lines
4.7 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
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 RouteStorage : IRouteStorage
|
|
{
|
|
/// <summary>
|
|
/// Получение полного списка
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public List<RouteViewModel> GetFullList()
|
|
{
|
|
using var context = new RouteGuideDatabase();
|
|
return context.Routes
|
|
.Include(x => x.Transport)
|
|
.Include(x => x.Stops)
|
|
.ThenInclude(x => x.Stop)
|
|
.ToList()
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Получение фильтрованного списка
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public List<RouteViewModel> GetFilteredList(RouteSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.Name))
|
|
{
|
|
return new();
|
|
}
|
|
|
|
using var context = new RouteGuideDatabase();
|
|
return context.Routes
|
|
.Include(x => x.Transport)
|
|
.Include(x => x.Stops)
|
|
.ThenInclude(x => x.Stop)
|
|
.ToList()
|
|
.Where(x => x.Name.Contains(model.Name))
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Получение элемента
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public RouteViewModel? GetElement(RouteSearchModel model)
|
|
{
|
|
if (!model.Id.HasValue && string.IsNullOrEmpty(model.Name))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
using var context = new RouteGuideDatabase();
|
|
return context.Routes
|
|
.Include(x => x.Transport)
|
|
.Include(x => x.Stops)
|
|
.ThenInclude(x => x.Stop)
|
|
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name.Contains(model.Name)) || (model.Id.HasValue && x.Id == model.Id))
|
|
?.GetViewModel;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Добавление элемента
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public RouteViewModel? Insert(RouteBindingModel model)
|
|
{
|
|
using var context = new RouteGuideDatabase();
|
|
var newRoute = Route.Create(context, model);
|
|
if (newRoute == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
context.Routes.Add(newRoute);
|
|
context.SaveChanges();
|
|
return newRoute.GetViewModel;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Редактирование элемента
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public RouteViewModel? Update(RouteBindingModel model)
|
|
{
|
|
using var context = new RouteGuideDatabase();
|
|
using var transaction = context.Database.BeginTransaction();
|
|
try
|
|
{
|
|
var route = context.Routes.FirstOrDefault(x => x.Id == model.Id);
|
|
if (route == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
route.Update(model);
|
|
context.SaveChanges();
|
|
route.UpdateStops(context, model);
|
|
transaction.Commit();
|
|
return route.GetViewModel;
|
|
}
|
|
catch
|
|
{
|
|
transaction.Rollback();
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Удаление элемента
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public RouteViewModel? Delete(RouteBindingModel model)
|
|
{
|
|
using var context = new RouteGuideDatabase();
|
|
var route = context.Routes
|
|
.Include(x => x.Stops)
|
|
.FirstOrDefault(rec => rec.Id == model.Id);
|
|
if (route == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
context.Routes.Remove(route);
|
|
context.SaveChanges();
|
|
return route.GetViewModel;
|
|
}
|
|
}
|
|
}
|