using BankYouBankruptContracts.BindingModels; using BankYouBankruptContracts.SearchModels; using BankYouBankruptContracts.StoragesContracts; using BankYouBankruptContracts.ViewModels; using BankYouBankruptDatabaseImplement.Models; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BankYouBankruptDatabaseImplement.Implements { public class CashWithdrawalStorage : ICashWithdrawalStorage { public List GetFullList() { using var context = new BankYouBancruptDatabase(); return context.CashWithdrawals .Include(x => x.Cashier) .Include(x => x.Debiting) .Include(x => x.Account) .Select(x => x.GetViewModel) .ToList(); } public List GetFilteredList(CashWithdrawalSearchModel model) { using var context = new BankYouBancruptDatabase(); var result = context.CashWithdrawals.Include(x => x.Cashier) .Include(x => x.Debiting) .Include(x => x.Account) .ToList(); if (model.AccountId.HasValue) result = result.Where(x => x.AccountId == model.AccountId).ToList(); if (model.CashierId.HasValue) result = result.Where(x => x.CashierId == model.CashierId).ToList(); if (model.DateFrom.HasValue) result = result.Where(x => x.DateOperation >= model.DateFrom).ToList(); if (model.DateTo.HasValue) result = result.Where(x => x.DateOperation <= model.DateTo).ToList(); if (model.ClientId.HasValue) result = result.Where(x => x.Account.ClientId == model.ClientId).ToList(); return result.Select(x => x.GetViewModel).ToList(); } public CashWithdrawalViewModel? GetElement(CashWithdrawalSearchModel model) { using var context = new BankYouBancruptDatabase(); return context.CashWithdrawals .Include(x => x.Cashier) .Include(x => x.Debiting) .Include(x => x.Account) .FirstOrDefault(x => (!(model.AccountId < 0) && x.AccountId == model.AccountId) || (model.Id.HasValue && x.Id == model.Id)) ?.GetViewModel; } public CashWithdrawalViewModel? Insert(CashWithdrawalBindingModel model) { using var context = new BankYouBancruptDatabase(); var newCashWithdrawal = CashWithdrawal.Create(context, model); if (newCashWithdrawal == null) { return null; } context.CashWithdrawals.Add(newCashWithdrawal); context.SaveChanges(); return newCashWithdrawal.GetViewModel; } public CashWithdrawalViewModel? Update(CashWithdrawalBindingModel model) { using var context = new BankYouBancruptDatabase(); using var transaction = context.Database.BeginTransaction(); try { var cashWithdrawal = context.CashWithdrawals.FirstOrDefault(rec => rec.Id == model.Id); if (cashWithdrawal == null) { return null; } cashWithdrawal.Update(model); context.SaveChanges(); transaction.Commit(); return cashWithdrawal.GetViewModel; } catch { transaction.Rollback(); throw; } } public CashWithdrawalViewModel? Delete(CashWithdrawalBindingModel model) { using var context = new BankYouBancruptDatabase(); var element = context.CashWithdrawals .Include(x => x.AccountId) .FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.CashWithdrawals.Remove(element); context.SaveChanges(); return element.GetViewModel; } return null; } } }