SUBD_Aleikin/Restaurant/RestaurantDatabaseImplement/Implements/OrderStorage.cs

204 lines
6.8 KiB
C#
Raw Permalink Normal View History

using Microsoft.EntityFrameworkCore;
using RestaurantContracts.BindingModels;
using RestaurantContracts.SearchModels;
using RestaurantContracts.StoragesContracts;
using RestaurantContracts.ViewModels;
using RestaurantDatabaseImplement.Models;
using System;
using System.Collections.Generic;
2023-05-17 00:48:03 +04:00
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RestaurantDatabaseImplement.Implements
{
public class OrderStorage : IOrderStorage
{
public List<OrderViewModel> GetFullList()
{
using var context = new RestaurantDatabase();
return context.Orders
.Include(x => x.Client)
.Include(x => x.Products)
.ThenInclude(x => x.Product)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
using var context = new RestaurantDatabase();
if (model.Id.HasValue)
{
return context.Orders
.Include(x => x.Products)
.ThenInclude(x => x.Product)
.Include(x => x.Client)
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
else if (model.ClientId.HasValue)
{
return context.Orders
.Include(x => x.Products)
.ThenInclude(x => x.Product)
.Include(x => x.Client)
.Where(x => x.ClientId == model.ClientId)
.Select(x => x.GetViewModel)
.ToList();
}
else if (model.Date.HasValue)
{
return context.Orders
.Include(x => x.Products)
.ThenInclude(x => x.Product)
.Include(x => x.Client)
.Where(x => x.Date == model.Date)
.Select(x => x.GetViewModel)
.ToList();
}
else
{
return new();
}
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new RestaurantDatabase();
return context.Orders
.Include(x => x.Client)
.Include(x => x.Products)
.ThenInclude(x => x.Product)
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel;
}
public OrderViewModel? Insert(OrderBindingModel model)
{
using var context = new RestaurantDatabase();
var newOrder = Order.Create(context, model);
if (newOrder == null)
{
return null;
}
context.Orders.Add(newOrder);
context.SaveChanges();
return newOrder.GetViewModel;
}
public OrderViewModel? Update(OrderBindingModel model)
{
using var context = new RestaurantDatabase();
var element = context.Orders
.Include(x => x.Client)
.Include(x => x.Products)
.ThenInclude(x => x.Product)
.FirstOrDefault(x => x.Id == model.Id);
if (element == null)
{
return null;
}
element.Update(model);
context.SaveChanges();
return GetElement(new OrderSearchModel { Id = element.Id });
}
public OrderViewModel? Delete(OrderBindingModel model)
{
using var context = new RestaurantDatabase();
var element = context.Orders
.Include(x => x.Client)
.Include(x => x.Products)
.ThenInclude(x => x.Product)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Orders.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
2023-05-17 00:48:03 +04:00
public string DiffGetTest(int count)
{
using var context = new RestaurantDatabase();
Stopwatch stopwatch = new();
stopwatch.Start();
var list = context.Orders
.Include(x => x.Client)
.Join(context.Clients,
order => order.ClientId,
client => client.Id,
(order, client) => new { Order = order, Client = client })
.Select(order => new
{
Order = order.Order.GetViewModel,
Client = order.Client.GetViewModel,
})
.Take(count)
.ToList();
stopwatch.Stop();
return stopwatch.ElapsedMilliseconds.ToString();
}
public string GetTest(int count)
{
using var context = new RestaurantDatabase();
Stopwatch stopwatch = new();
stopwatch.Start();
var list = context.Orders
.Include(x => x.Client)
.Take(count)
.Select(x => x.GetViewModel)
.ToList();
stopwatch.Stop();
return stopwatch.ElapsedMilliseconds.ToString();
}
public string SetTest(int count)
{
Random rnd = new Random();
using var context = new RestaurantDatabase();
var listClients = context.Clients.Select(x => x.Id).ToList();
if (listClients.Count < 1)
{
throw new Exception("Недостаточно для генерации!");
}
for (int i = 0; i < count; ++i)
{
context.Orders.Add(Order.Create(context, new OrderBindingModel
{
Id = 0,
Date = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc),
Price = rnd.NextDouble(),
ClientId = listClients[rnd.Next(listClients.Count)],
}));
}
Stopwatch stopwatch = new();
stopwatch.Start();
context.SaveChanges();
stopwatch.Stop();
return stopwatch.ElapsedMilliseconds.ToString();
}
public bool ClearList()
{
try
{
using var context = new RestaurantDatabase();
var tableName = context.Model.FindEntityType(typeof(Order)).GetTableName();
context.Database.ExecuteSqlRaw($"DELETE FROM \"{tableName}\"");
}
catch (Exception)
{
return false;
}
return true;
}
}
}