LabSubd/Subd/DataBase/Implements/RouteStorage.cs

117 lines
4.0 KiB
C#
Raw Normal View History

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();
2023-05-05 21:47:09 +04:00
return context.Routes.FromSqlRaw("Select * From Route").Include(u => u.PlaceStartNavigation).Include(a => a.PlaceEndNavigation)
.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)
{
return new();
}
using var context = new LogisticContext();
2023-04-22 20:49:27 +04:00
if(model.Length.HasValue)
return context.Routes
2023-05-05 21:47:09 +04:00
.FromSqlRaw("Select * FROM Route WHERE Length <= {0}", model.Length).Include(u => u.PlaceStartNavigation).Include(a => a.PlaceEndNavigation)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
2023-04-22 20:49:27 +04:00
else if(model.Start.HasValue)
return context.Routes
2023-05-05 21:47:09 +04:00
.FromSqlRaw("Select * FROM Route WHERE PlaceStart = {0} ", model.Start).Include(u => u.PlaceStartNavigation).Include(a => a.PlaceEndNavigation)
2023-04-22 20:49:27 +04:00
.ToList()
.Select(x => x.GetViewModel)
.ToList();
return context.Routes
2023-05-05 21:47:09 +04:00
.FromSqlRaw("Select * FROM Route WHERE PlaceEnd = {0} ", model.End).Include(u => u.PlaceStartNavigation).Include(a => a.PlaceEndNavigation)
2023-04-22 20:49:27 +04:00
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public RouteVM? GetElement(RouteSM model)
{
2023-04-22 20:49:27 +04:00
if (!model.Id.HasValue && !model.Start.HasValue && !model.End.HasValue)
{
return null;
}
using var context = new LogisticContext();
2023-05-05 21:47:09 +04:00
var mid = context.Routes.FromSqlRaw("Select * from Route r where r.id = {0}", model.Id).Include(u => u.PlaceStartNavigation).Include(a => a.PlaceEndNavigation).ToList();
if (mid.Count == 0)
return null;
var res = mid[0]?.GetViewModel;
return res;
}
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;
}
}
}