186 lines
5.6 KiB
C#
186 lines
5.6 KiB
C#
using Microsoft.EntityFrameworkCore;
|
||
using RouteGuideContracts.BindingModels;
|
||
using RouteGuideContracts.SearchModels;
|
||
using RouteGuideContracts.StoragesModels;
|
||
using RouteGuideContracts.ViewModels;
|
||
using RouteGuideDatabaseImplements.Models;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Diagnostics;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace RouteGuideDatabaseImplements.Implements
|
||
{
|
||
public class RouteStorage : IRouteStorage
|
||
{
|
||
public RouteViewModel? Delete(RouteBindingModel model)
|
||
{
|
||
using var context = new RouteGuideDatabase();
|
||
|
||
var element = context.Routes.Include(x => x.Stops).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 RouteGuideDatabase();
|
||
|
||
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 RouteGuideDatabase();
|
||
|
||
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 RouteGuideDatabase();
|
||
|
||
return context.Routes.Include(x => x.Stops).ThenInclude(x => x.Stop).ToList().Select(x => x.GetViewModel).ToList();
|
||
}
|
||
|
||
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;
|
||
}
|
||
|
||
public RouteViewModel? Update(RouteBindingModel model)
|
||
{
|
||
using var context = new RouteGuideDatabase();
|
||
|
||
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();
|
||
route.UpdateStops(context, model);
|
||
transaction.Commit();
|
||
|
||
return route.GetViewModel;
|
||
}
|
||
catch
|
||
{
|
||
transaction.Rollback();
|
||
|
||
throw;
|
||
}
|
||
}
|
||
|
||
public string TestInsertList(int num)
|
||
{
|
||
var context = new RouteGuideDatabase();
|
||
Stopwatch stopwatch = new();
|
||
Random rnd = new Random();
|
||
long[] res = new long[num * 2];
|
||
|
||
|
||
for (int i = 0; i < num; ++i)
|
||
{
|
||
int rndId = rnd.Next();
|
||
var model = new StopBindingModel
|
||
{
|
||
Id = rndId,
|
||
Name = "Остановка " + rndId.ToString(),
|
||
};
|
||
context.Stops.Add(Stop.Create(model));
|
||
stopwatch.Start();
|
||
context.SaveChanges();
|
||
stopwatch.Stop();
|
||
res[i] = stopwatch.ElapsedMilliseconds;
|
||
}
|
||
List<StopViewModel> _stops = context.Stops.Select(x => x.GetViewModel).ToList();
|
||
|
||
for (int i = 0; i < num; ++i)
|
||
{
|
||
int rndId = rnd.Next();
|
||
var model = new RouteBindingModel
|
||
{
|
||
Id = rndId,
|
||
Name = "Maршрут №" + rndId.ToString(),
|
||
};
|
||
context.Routes.Add(Route.Create(context, model));
|
||
stopwatch.Start();
|
||
context.SaveChanges();
|
||
stopwatch.Stop();
|
||
res[i] = stopwatch.ElapsedMilliseconds;
|
||
}
|
||
|
||
long sum = 0;
|
||
for (int i = 0; i < num; i++)
|
||
{
|
||
sum += res[i];
|
||
}
|
||
int result = Convert.ToInt32(sum / num);
|
||
return result.ToString();
|
||
}
|
||
|
||
public string TestReadList(int num)
|
||
{
|
||
var context = new RouteGuideDatabase();
|
||
Stopwatch stopwatch = new();
|
||
|
||
long[] res = new long[num];
|
||
|
||
for (int i = 0; i < num; i++)
|
||
{
|
||
|
||
stopwatch.Start();
|
||
List<RouteViewModel> list = context.Routes.Include(x => x.Stops).ThenInclude(x => x.Stop).ToList().Select(x => x.GetViewModel).ToList();
|
||
stopwatch.Stop();
|
||
res[i] = stopwatch.ElapsedMilliseconds;
|
||
}
|
||
|
||
long sum = 0;
|
||
for (int i = 0; i < num; i++)
|
||
{
|
||
sum += res[i];
|
||
}
|
||
int result = Convert.ToInt32(sum / num);
|
||
return result.ToString();
|
||
}
|
||
}
|
||
}
|