дай бог

This commit is contained in:
VictoriaPresnyakova 2023-05-04 21:13:06 +04:00
parent ea1841f8bb
commit f709d8cd0e
3 changed files with 287 additions and 5 deletions

View File

@ -1,12 +1,111 @@
using System;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TransportGuideContracts.BindingModels;
using TransportGuideContracts.SearchModels;
using TransportGuideContracts.StoragesContracts;
using TransportGuideContracts.ViewModels;
using TransportGuideDatabaseImplements.Models;
namespace TransportGuideDatabaseImplements.Implements
{
internal class RouteStorage
public class RouteStorage : IRouteStorage
{
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;
}
}
}
}

View File

@ -3,10 +3,95 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TransportGuideContracts.BindingModels;
using TransportGuideContracts.SearchModels;
using TransportGuideContracts.StoragesContracts;
using TransportGuideContracts.ViewModels;
using TransportGuideDatabaseImplements.Models;
namespace TransportGuideDatabaseImplements.Implements
{
public class StopStorage
public class StopStorage : IStopStorage
{
public StopViewModel? Delete(StopBindingModel model)
{
using var context = new TransportGuideDB();
var element = context.Stops.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Stops.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public StopViewModel? GetElement(StopSearchModel model)
{
if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue)
{
return null;
}
using var context = new TransportGuideDB();
return context.Stops.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<StopViewModel> GetFilteredList(StopSearchModel model)
{
if (string.IsNullOrEmpty(model.Name))
{
return new();
}
using var context = new TransportGuideDB();
return context.Stops.Where(x => x.Name.Contains(model.Name)).Select(x => x.GetViewModel).ToList();
}
public List<StopViewModel> GetFullList()
{
using var context = new TransportGuideDB();
return context.Stops.Select(x => x.GetViewModel).ToList();
}
public StopViewModel? Insert(StopBindingModel model)
{
var newStop = Stop.Create(model);
if (newStop == null)
{
return null;
}
using var context = new TransportGuideDB();
context.Stops.Add(newStop);
context.SaveChanges();
return newStop.GetViewModel;
}
public StopViewModel? Update(StopBindingModel model)
{
using var context = new TransportGuideDB();
var Stop = context.Stops.FirstOrDefault(x => x.Id == model.Id);
if (Stop == null)
{
return null;
}
Stop.Update(model);
context.SaveChanges();
return Stop.GetViewModel;
}
}
}

View File

@ -1,12 +1,110 @@
using System;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TransportGuideContracts.BindingModels;
using TransportGuideContracts.SearchModels;
using TransportGuideContracts.StoragesContracts;
using TransportGuideContracts.ViewModels;
using TransportGuideDatabaseImplements.Models;
namespace TransportGuideDatabaseImplements.Implements
{
internal class TransportTypeStorage
public class TransportTypeStorage : ITransportTypeStorage
{
public TransportTypeViewModel? Delete(TransportTypeBindingModel model)
{
using var context = new TransportGuideDB();
var element = context.TransportTypes.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.TransportTypes.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public TransportTypeViewModel? GetElement(TransportTypeSearchModel model)
{
if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue)
{
return null;
}
using var context = new TransportGuideDB();
return context.TransportTypes.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<TransportTypeViewModel> GetFilteredList(TransportTypeSearchModel model)
{
if (string.IsNullOrEmpty(model.Name))
{
return new();
}
using var context = new TransportGuideDB();
return context.TransportTypes.Where(x => x.Name.Contains(model.Name)).ToList().Select(x => x.GetViewModel).ToList();
}
public List<TransportTypeViewModel> GetFullList()
{
using var context = new TransportGuideDB();
return context.TransportTypes.ToList().Select(x => x.GetViewModel).ToList();
}
public TransportTypeViewModel? Insert(TransportTypeBindingModel model)
{
using var context = new TransportGuideDB();
var newTransportType = TransportType.Create(model);
if (newTransportType == null)
{
return null;
}
context.TransportTypes.Add(newTransportType);
context.SaveChanges();
return newTransportType.GetViewModel;
}
public TransportTypeViewModel? Update(TransportTypeBindingModel model)
{
using var context = new TransportGuideDB();
using var transaction = context.Database.BeginTransaction();
try
{
var transportType = context.TransportTypes.FirstOrDefault(rec => rec.Id == model.Id);
if (transportType == null)
{
return null;
}
transportType.Update(model);
context.SaveChanges();
transaction.Commit();
return transportType.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
}
}