2024-03-13 10:24:19 +04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using SushiBarContracts.BindingModels;
|
|
|
|
|
using SushiBarContracts.SearchModels;
|
|
|
|
|
using SushiBarContracts.StoragesContracts;
|
|
|
|
|
using SushiBarContracts.ViewModels;
|
|
|
|
|
using SushiBarDatabaseImplement.Models;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
namespace SushiBarDatabaseImplement.Implements
|
|
|
|
|
{
|
|
|
|
|
public class OrderStorage : IOrderStorage
|
|
|
|
|
{
|
|
|
|
|
public List<OrderViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new SushiBarDatabase();
|
|
|
|
|
return context.Orders
|
2024-04-28 11:49:26 +04:00
|
|
|
|
.Include(o => o.Sushi)
|
2024-05-04 21:53:45 +04:00
|
|
|
|
.Include(o => o.Client)
|
2024-04-28 11:49:26 +04:00
|
|
|
|
.Select(o => o.GetViewModel)
|
2024-03-13 10:24:19 +04:00
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
|
|
|
|
|
{
|
2024-04-28 11:49:26 +04:00
|
|
|
|
using var context = new SushiBarDatabase();
|
|
|
|
|
return context.Orders
|
|
|
|
|
.Include(o => o.Sushi)
|
2024-05-04 21:53:45 +04:00
|
|
|
|
.Include(o => o.Client)
|
|
|
|
|
.Where(o => (
|
|
|
|
|
(!model.Id.HasValue || o.Id == model.Id) &&
|
|
|
|
|
(!model.DateFrom.HasValue || o.DateCreate >= model.DateFrom) &&
|
|
|
|
|
(!model.DateTo.HasValue || o.DateCreate <= model.DateTo) &&
|
|
|
|
|
(!model.ClientId.HasValue || o.ClientId == model.ClientId)
|
|
|
|
|
))
|
2024-04-28 11:49:26 +04:00
|
|
|
|
.Select(o => o.GetViewModel)
|
|
|
|
|
.ToList();
|
2024-03-13 10:24:19 +04:00
|
|
|
|
}
|
|
|
|
|
public OrderViewModel? GetElement(OrderSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (!model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
using var context = new SushiBarDatabase();
|
|
|
|
|
return context.Orders
|
2024-04-28 11:49:26 +04:00
|
|
|
|
.Include (o => o.Sushi)
|
2024-05-04 21:53:45 +04:00
|
|
|
|
.Include(o => o.Client)
|
2024-04-28 11:49:26 +04:00
|
|
|
|
.FirstOrDefault(o => o.Id == model.Id)?.GetViewModel;
|
2024-03-13 10:24:19 +04:00
|
|
|
|
}
|
|
|
|
|
public OrderViewModel? Insert(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new SushiBarDatabase();
|
|
|
|
|
var newOrder = Order.Create(context, model);
|
|
|
|
|
if (newOrder == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
context.Orders.Add(newOrder);
|
|
|
|
|
context.SaveChanges();
|
2024-04-24 18:42:24 +04:00
|
|
|
|
return newOrder.GetViewModel;
|
2024-03-13 10:24:19 +04:00
|
|
|
|
}
|
|
|
|
|
public OrderViewModel? Update(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new SushiBarDatabase();
|
|
|
|
|
using var transaction = context.Database.BeginTransaction();
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-05-04 21:53:45 +04:00
|
|
|
|
var order = context.Orders
|
|
|
|
|
.Include(o => o.Sushi)
|
|
|
|
|
.Include(o => o.Client)
|
|
|
|
|
.FirstOrDefault(o => o.Id == model.Id);
|
2024-03-13 10:24:19 +04:00
|
|
|
|
if (order == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
order.Update(model);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
transaction.Commit();
|
2024-04-24 18:42:24 +04:00
|
|
|
|
return order.GetViewModel;
|
2024-03-13 10:24:19 +04:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public OrderViewModel? Delete(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new SushiBarDatabase();
|
|
|
|
|
var element = context.Orders
|
2024-04-28 13:08:31 +04:00
|
|
|
|
.Include(o => o.Sushi)
|
2024-05-04 21:53:45 +04:00
|
|
|
|
.Include(o => o.Client)
|
2024-04-28 11:49:26 +04:00
|
|
|
|
.FirstOrDefault(o => o.Id == model.Id);
|
2024-03-13 10:24:19 +04:00
|
|
|
|
if (element != null)
|
|
|
|
|
{
|
|
|
|
|
context.Orders.Remove(element);
|
|
|
|
|
context.SaveChanges();
|
2024-04-24 18:42:24 +04:00
|
|
|
|
return element.GetViewModel;
|
2024-03-13 10:24:19 +04:00
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|