2023-04-22 19:42:51 +04:00
|
|
|
|
using Contracts.BindingModels;
|
|
|
|
|
using Contracts.SearchModel;
|
|
|
|
|
using Contracts.Storage;
|
|
|
|
|
using Contracts.ViewModels;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace DataBase.Implements
|
|
|
|
|
{
|
|
|
|
|
public class RouteStorage : IRouteStorage
|
|
|
|
|
{
|
|
|
|
|
public List<RouteVM> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new LogisticContext();
|
|
|
|
|
return context.Routes.FromSqlRaw("Select * From Route")
|
|
|
|
|
.ToList()
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<RouteVM> GetFilteredList(RouteSM model)
|
|
|
|
|
{
|
2023-04-22 20:49:27 +04:00
|
|
|
|
if (!model.Length.HasValue && !model.Start.HasValue && !model.End.HasValue)
|
2023-04-22 19:42:51 +04:00
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
|
|
|
|
using var context = new LogisticContext();
|
2023-04-22 20:49:27 +04:00
|
|
|
|
if(model.Length.HasValue)
|
2023-04-22 19:42:51 +04:00
|
|
|
|
return context.Routes
|
|
|
|
|
.FromSqlRaw("Select * FROM Route WHERE Length <= {0}", model.Length)
|
|
|
|
|
.ToList()
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
2023-04-22 20:49:27 +04:00
|
|
|
|
else if(model.Start.HasValue)
|
|
|
|
|
return context.Routes
|
|
|
|
|
.FromSqlRaw("Select * FROM Route WHERE PlaceStart = {0} ", model.Start)
|
|
|
|
|
.ToList()
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
return context.Routes
|
|
|
|
|
.FromSqlRaw("Select * FROM Route WHERE PlaceEnd = {0} ", model.End)
|
|
|
|
|
.ToList()
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
2023-04-22 19:42:51 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RouteVM? GetElement(RouteSM model)
|
|
|
|
|
{
|
2023-04-22 20:49:27 +04:00
|
|
|
|
if (!model.Id.HasValue && !model.Start.HasValue && !model.End.HasValue)
|
2023-04-22 19:42:51 +04:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
using var context = new LogisticContext();
|
|
|
|
|
return context.Routes
|
2023-04-22 20:49:27 +04:00
|
|
|
|
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id) || (model.Start.HasValue && model.End.HasValue && x.PlaceEnd == model.End && x.PlaceStart == model.Start ))
|
2023-04-22 19:42:51 +04:00
|
|
|
|
?.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RouteVM? Insert(RouteBM model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new LogisticContext();
|
|
|
|
|
var newRoute = Route.Create(context, model);
|
|
|
|
|
if (newRoute == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
context.Routes.Add(newRoute);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return newRoute.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RouteVM? Update(RouteBM model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new LogisticContext();
|
|
|
|
|
using var transaction = context.Database.BeginTransaction();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var route = context.Routes.FirstOrDefault(rec => rec.Id == model.Id);
|
|
|
|
|
if (route == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
route.Update(model);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
transaction.Commit();
|
|
|
|
|
return route.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RouteVM? Delete(RouteBM model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new LogisticContext();
|
|
|
|
|
var element = context.Routes
|
|
|
|
|
.FirstOrDefault(rec => rec.Id == model.Id);
|
|
|
|
|
if (element != null)
|
|
|
|
|
{
|
|
|
|
|
context.Routes.Remove(element);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return element.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|