2023-05-04 21:13:06 +04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using System;
|
2023-05-04 20:42:59 +04:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2023-05-04 21:13:06 +04:00
|
|
|
|
using TransportGuideContracts.BindingModels;
|
|
|
|
|
using TransportGuideContracts.SearchModels;
|
|
|
|
|
using TransportGuideContracts.StoragesContracts;
|
|
|
|
|
using TransportGuideContracts.ViewModels;
|
|
|
|
|
using TransportGuideDatabaseImplements.Models;
|
2023-05-04 20:42:59 +04:00
|
|
|
|
|
|
|
|
|
namespace TransportGuideDatabaseImplements.Implements
|
|
|
|
|
{
|
2023-05-04 21:13:06 +04:00
|
|
|
|
public class RouteStorage : IRouteStorage
|
2023-05-04 20:42:59 +04:00
|
|
|
|
{
|
2023-05-04 21:13:06 +04:00
|
|
|
|
public RouteViewModel? Delete(RouteBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new TransportGuideDB();
|
|
|
|
|
|
|
|
|
|
var element = context.Routes.Include(x => x.Stops).Include(x => x.TransportType).FirstOrDefault(rec => rec.Id == model.Id);
|
|
|
|
|
|
|
|
|
|
if (element != null)
|
|
|
|
|
{
|
|
|
|
|
context.Routes.Remove(element);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return element.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RouteViewModel? GetElement(RouteSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using var context = new TransportGuideDB();
|
|
|
|
|
|
|
|
|
|
return context.Routes.Include(x => x.Stops).ThenInclude(x => x.Stop).FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<RouteViewModel> GetFilteredList(RouteSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(model.Name))
|
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using var context = new TransportGuideDB();
|
|
|
|
|
|
|
|
|
|
return context.Routes.Include(x => x.Stops).ThenInclude(x => x.Stop).Where(x => x.Name.Contains(model.Name)).ToList().Select(x => x.GetViewModel).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<RouteViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new TransportGuideDB();
|
|
|
|
|
|
|
|
|
|
return context.Routes.Include(x => x.TransportType).Include(x => x.Stops).ThenInclude(x => x.Stop).ToList().Select(x => x.GetViewModel).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RouteViewModel? Insert(RouteBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new TransportGuideDB();
|
|
|
|
|
|
|
|
|
|
var newRoute = Route.Create(context, model);
|
|
|
|
|
|
|
|
|
|
if (newRoute == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
context.Routes.Add(newRoute);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
|
|
|
|
|
return newRoute.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RouteViewModel? Update(RouteBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new TransportGuideDB();
|
|
|
|
|
|
|
|
|
|
using var transaction = context.Database.BeginTransaction();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var route = context.Routes.Include(x => x.TransportType).FirstOrDefault(rec => rec.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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-04 20:42:59 +04:00
|
|
|
|
}
|
|
|
|
|
}
|