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-04-06 15:55:02 +04:00
|
|
|
|
if (!model.Id.HasValue && !model.DateTo.HasValue && !model.DateFrom.HasValue && !model.ClientId.HasValue)
|
2024-03-24 23:52:35 +04:00
|
|
|
|
{
|
2024-03-25 10:50:13 +04:00
|
|
|
|
return new();
|
2024-03-24 23:52:35 +04:00
|
|
|
|
}
|
2024-03-25 10:50:13 +04:00
|
|
|
|
using var context = new SushiBarDatabase();
|
|
|
|
|
if (model.Id.HasValue)
|
|
|
|
|
return context.Orders.Where(x => x.Id == model.Id).Select(x => AccessViewModel(x.GetViewModel)).ToList();
|
2024-04-06 15:55:02 +04:00
|
|
|
|
if (model.ClientId.HasValue)
|
|
|
|
|
return context.Orders.Where(x => x.ClientId == model.ClientId).Select(x => AccessViewModel(x.GetViewModel)).ToList();
|
2024-03-25 10:50:13 +04:00
|
|
|
|
else
|
|
|
|
|
return context.Orders.Where(x => x.DateCreate >= model.DateFrom).Where(x => x.DateCreate <= model.DateTo).Select(x => AccessViewModel(x.GetViewModel)).ToList();
|
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();
|
|
|
|
|
|
|
|
|
|
if (viewModel != null)
|
|
|
|
|
{
|
2024-04-06 15:55:02 +04:00
|
|
|
|
foreach (var sushi in context.Sushis)
|
|
|
|
|
{
|
|
|
|
|
if(sushi.Id == viewModel.Id)
|
|
|
|
|
{
|
|
|
|
|
viewModel.SushiName = sushi.SushiName;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-10 14:16:51 +04:00
|
|
|
|
|
2024-04-06 15:55:02 +04:00
|
|
|
|
foreach (var client in context.Clients)
|
|
|
|
|
{
|
|
|
|
|
if(client.Id == viewModel.ClientId)
|
|
|
|
|
{
|
|
|
|
|
viewModel.ClientFIO = client.ClientFIO;
|
|
|
|
|
return viewModel;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-10 14:16:51 +04:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|