PIbd-22_Shabunov_O.A._SushiBar/SushiBarDatabaseImplement/Storages/ChequeStorage.cs

134 lines
4.3 KiB
C#
Raw Permalink Normal View History

2024-05-16 17:13:55 +04:00
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)
2024-05-24 01:56:34 +04:00
.ThenInclude(x => x.Dish)
.Include(x => x.ChequeItems)
.ThenInclude(x => x.Cook)
2024-05-16 17:13:55 +04:00
.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)
2024-05-24 01:56:34 +04:00
.ThenInclude(x => x.Dish)
.Include(x => x.ChequeItems)
.ThenInclude(x => x.Cook)
2024-05-16 17:13:55 +04:00
.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)
2024-05-24 01:56:34 +04:00
.ThenInclude(x => x.Dish)
.Include(x => x.ChequeItems)
.ThenInclude(x => x.Cook)
2024-05-16 17:13:55 +04:00
.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)
2024-05-24 01:56:34 +04:00
.ThenInclude(x => x.Dish)
.Include(x => x.ChequeItems)
.ThenInclude(x => x.Cook)
2024-05-16 17:13:55 +04:00
.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();
2024-05-24 01:56:34 +04:00
NewCheque.UpdateChequeItems(Context, Model);
2024-05-16 17:13:55 +04:00
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;
}
}
}