2024-03-10 14:16:51 +04:00
|
|
|
|
|
2024-04-06 21:47:06 +04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
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();
|
2024-04-06 21:47:06 +04:00
|
|
|
|
return context.Orders.Include(x => x.Sushi).Include(x => x.Client).Select(x => x.GetViewModel).ToList();
|
2024-03-10 14:16:51 +04:00
|
|
|
|
}
|
|
|
|
|
|
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 21:47:06 +04:00
|
|
|
|
if (!model.Id.HasValue && !model.DateFrom.HasValue && !model.DateTo.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();
|
2024-04-06 21:47:06 +04:00
|
|
|
|
return context.Orders
|
|
|
|
|
.Where(x => x.Id == model.Id || model.DateFrom <= x.DateCreate && x.DateCreate <= model.DateTo || x.ClientId == model.ClientId)
|
|
|
|
|
.Include(x => x.Sushi)
|
|
|
|
|
.Include(x => x.Client)
|
|
|
|
|
.Select(x => 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();
|
2024-04-06 21:47:06 +04:00
|
|
|
|
return context.Orders.Include(x => x.Sushi).Include(x => x.Client).FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
|
2024-03-10 14:16:51 +04:00
|
|
|
|
}
|
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();
|
2024-04-06 21:47:06 +04:00
|
|
|
|
return context.Orders.Include(x => x.Sushi).Include(x => x.Client).FirstOrDefault(x => x.Id == newOrder.Id)?.GetViewModel;
|
2024-03-10 14:16:51 +04:00
|
|
|
|
}
|
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();
|
2024-04-06 21:47:06 +04:00
|
|
|
|
return context.Orders.Include(x => x.Sushi).Include(x => x.Client).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
|
2024-03-10 14:16:51 +04:00
|
|
|
|
}
|
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();
|
2024-04-06 21:47:06 +04:00
|
|
|
|
var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id);
|
|
|
|
|
if (element != null)
|
2024-03-10 14:16:51 +04:00
|
|
|
|
{
|
2024-04-06 21:47:06 +04:00
|
|
|
|
context.Orders.Remove(element);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return element.GetViewModel;
|
2024-04-06 15:55:02 +04:00
|
|
|
|
}
|
2024-03-10 14:16:51 +04:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|