2024-03-10 14:16:51 +04:00
|
|
|
|
|
|
|
|
|
using SushiBarContracts.BindingModel;
|
|
|
|
|
using SushiBarContracts.SearchModel;
|
|
|
|
|
using SushiBarContracts.StoragesContracts;
|
|
|
|
|
using SushiBarContracts.ViewModels;
|
|
|
|
|
using SushiBarDatabaseImplement.Models;
|
|
|
|
|
|
|
|
|
|
namespace SushiBarDatabaseImplement.Implements
|
|
|
|
|
{
|
|
|
|
|
public class OrderStorage : IOrderStorage
|
|
|
|
|
{
|
2024-03-24 18:34:36 +04:00
|
|
|
|
public List<OrderViewModel> GetFullList()
|
2024-03-10 14:16:51 +04:00
|
|
|
|
{
|
|
|
|
|
using var context = new SushiBarDatabase();
|
|
|
|
|
return context.Orders.Select(x => AccessViewModel(x.GetViewModel)).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-24 18:34:36 +04:00
|
|
|
|
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
|
2024-03-10 14:16:51 +04:00
|
|
|
|
{
|
2024-03-25 09:56:01 +04:00
|
|
|
|
using var context = new SushiBarDatabase();
|
2024-03-24 23:52:35 +04:00
|
|
|
|
var orders = context.Orders
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
if (model.Id.HasValue)
|
2024-03-10 14:16:51 +04:00
|
|
|
|
{
|
2024-03-24 23:52:35 +04:00
|
|
|
|
orders = orders.Where(x => x.Id == model.Id.Value).ToList();
|
2024-03-10 14:16:51 +04:00
|
|
|
|
}
|
2024-03-24 23:52:35 +04:00
|
|
|
|
if (model.DateFrom.HasValue)
|
|
|
|
|
{
|
|
|
|
|
orders = orders.Where(x => x.DateCreate >= model.DateFrom.Value).ToList();
|
|
|
|
|
}
|
|
|
|
|
if (model.DateTo.HasValue)
|
|
|
|
|
{
|
|
|
|
|
orders = orders.Where(x => x.DateCreate <= model.DateTo.Value).ToList();
|
|
|
|
|
}
|
|
|
|
|
return orders;
|
2024-03-10 14:16:51 +04:00
|
|
|
|
}
|
2024-03-24 18:34:36 +04:00
|
|
|
|
public OrderViewModel? GetElement(OrderSearchModel model)
|
2024-03-10 14:16:51 +04:00
|
|
|
|
{
|
|
|
|
|
if (!model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
using var context = new SushiBarDatabase();
|
|
|
|
|
return AccessViewModel(context.Orders.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id).GetViewModel);
|
|
|
|
|
}
|
2024-03-24 18:34:36 +04:00
|
|
|
|
public OrderViewModel? Insert(OrderBindingModel model)
|
2024-03-10 14:16:51 +04:00
|
|
|
|
{
|
|
|
|
|
using var context = new SushiBarDatabase();
|
|
|
|
|
var newOrder = Order.Create(model);
|
|
|
|
|
if (newOrder == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
context.Orders.Add(newOrder);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return AccessViewModel(newOrder.GetViewModel);
|
|
|
|
|
}
|
2024-03-24 18:34:36 +04:00
|
|
|
|
public OrderViewModel? Update(OrderBindingModel model)
|
2024-03-10 14:16:51 +04:00
|
|
|
|
{
|
|
|
|
|
using var context = new SushiBarDatabase();
|
|
|
|
|
var order = context.Orders.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (order == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
order.Update(model);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return AccessViewModel(order.GetViewModel);
|
|
|
|
|
}
|
2024-03-24 18:34:36 +04:00
|
|
|
|
public OrderViewModel? Delete(OrderBindingModel model)
|
2024-03-10 14:16:51 +04:00
|
|
|
|
{
|
|
|
|
|
using var context = new SushiBarDatabase();
|
|
|
|
|
var order = context.Orders.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (order == null) return null;
|
|
|
|
|
context.Remove(order);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return AccessViewModel(order.GetViewModel);
|
|
|
|
|
}
|
|
|
|
|
public static OrderViewModel? AccessViewModel(OrderViewModel viewModel)
|
|
|
|
|
{
|
|
|
|
|
using var context = new SushiBarDatabase();
|
|
|
|
|
|
|
|
|
|
var sushi = context.Sushis.FirstOrDefault(x => x.Id == viewModel.SushiId);
|
|
|
|
|
if (viewModel != null)
|
|
|
|
|
{
|
|
|
|
|
viewModel.SushiName = sushi!.SushiName;
|
|
|
|
|
return viewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|