134 lines
4.3 KiB
C#
134 lines
4.3 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using SushiBarContracts.BindingModels;
|
|
using SushiBarContracts.SearchModels;
|
|
using SushiBarContracts.ViewModels;
|
|
using SushiBarDatabaseImplement.Models;
|
|
|
|
namespace SushiBarDatabaseImplement.Storages
|
|
{
|
|
public class ChequeStorage
|
|
{
|
|
public List<ChequeViewModel> GetFullList()
|
|
{
|
|
using var Context = new SushiBarDatabase();
|
|
|
|
return Context.Cheques
|
|
.Include(x => x.Customer)
|
|
.Include(x => x.Promotion)
|
|
.Include(x => x.ChequeItems)
|
|
.ThenInclude(x => x.Dish)
|
|
.Include(x => x.ChequeItems)
|
|
.ThenInclude(x => x.Cook)
|
|
.Select(x => x.ViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public List<ChequeViewModel> GetFilteredList(ChequeSearchModel Model)
|
|
{
|
|
using var Context = new SushiBarDatabase();
|
|
|
|
if (Model.CustomerId.HasValue)
|
|
{
|
|
return Context.Cheques
|
|
.Include(x => x.Customer)
|
|
.Include(x => x.Promotion)
|
|
.Include(x => x.ChequeItems)
|
|
.ThenInclude(x => x.Dish)
|
|
.Include(x => x.ChequeItems)
|
|
.ThenInclude(x => x.Cook)
|
|
.Where(x => x.CustomerId == Model.CustomerId)
|
|
.Select(x => x.ViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
if (Model.OrderDateFrom.HasValue)
|
|
{
|
|
return Context.Cheques
|
|
.Include(x => x.Customer)
|
|
.Include(x => x.Promotion)
|
|
.Include(x => x.ChequeItems)
|
|
.ThenInclude(x => x.Dish)
|
|
.Include(x => x.ChequeItems)
|
|
.ThenInclude(x => x.Cook)
|
|
.Where(x => x.OrderDate >= Model.OrderDateFrom && x.OrderDate <= Model.OrderDateTo)
|
|
.Select(x => x.ViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
return new();
|
|
}
|
|
|
|
public ChequeViewModel? GetElement(ChequeSearchModel Model)
|
|
{
|
|
if (!Model.Id.HasValue)
|
|
return null;
|
|
|
|
using var Context = new SushiBarDatabase();
|
|
return Context.Cheques
|
|
.Include(x => x.Customer)
|
|
.Include(x => x.Promotion)
|
|
.Include(x => x.ChequeItems)
|
|
.ThenInclude(x => x.Dish)
|
|
.Include(x => x.ChequeItems)
|
|
.ThenInclude(x => x.Cook)
|
|
.FirstOrDefault(x => x.Id == Model.Id)?.ViewModel;
|
|
}
|
|
|
|
public ChequeViewModel? Insert(ChequeBindingModel Model)
|
|
{
|
|
using var Context = new SushiBarDatabase();
|
|
var NewCheque = Cheque.Create(Context, Model);
|
|
|
|
if (NewCheque == null)
|
|
return null;
|
|
|
|
Context.Cheques.Add(NewCheque);
|
|
Context.SaveChanges();
|
|
NewCheque.UpdateChequeItems(Context, Model);
|
|
|
|
return NewCheque.ViewModel;
|
|
}
|
|
|
|
public ChequeViewModel? Update(ChequeBindingModel Model)
|
|
{
|
|
using var Context = new SushiBarDatabase();
|
|
|
|
using var Transaction = Context.Database.BeginTransaction();
|
|
try
|
|
{
|
|
var Cheque = Context.Cheques.FirstOrDefault(x => x.Id == Model.Id);
|
|
|
|
if (Cheque == null)
|
|
return null;
|
|
|
|
Cheque.Update(Model);
|
|
Context.SaveChanges();
|
|
Cheque.UpdateChequeItems(Context, Model);
|
|
|
|
Transaction.Commit();
|
|
return Cheque.ViewModel;
|
|
}
|
|
catch
|
|
{
|
|
Transaction.Rollback();
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public ChequeViewModel? Delete(ChequeBindingModel Model)
|
|
{
|
|
using var Context = new SushiBarDatabase();
|
|
var Cheque = Context.Cheques
|
|
.Include(x => x.ChequeItems)
|
|
.FirstOrDefault(rec => rec.Id == Model.Id);
|
|
|
|
if (Cheque == null)
|
|
return null;
|
|
|
|
Context.Cheques.Remove(Cheque);
|
|
Context.SaveChanges();
|
|
return Cheque.ViewModel;
|
|
}
|
|
}
|
|
}
|