2023-04-01 23:04:09 +04:00
|
|
|
|
using BankYouBankruptContracts.BindingModels;
|
|
|
|
|
using BankYouBankruptContracts.SearchModels;
|
|
|
|
|
using BankYouBankruptContracts.StoragesContracts;
|
|
|
|
|
using BankYouBankruptContracts.ViewModels;
|
|
|
|
|
using BankYouBankruptDatabaseImplement.Models;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using System;
|
2023-04-01 22:54:15 +04:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace BankYouBankruptDatabaseImplement.Implements
|
|
|
|
|
{
|
2023-04-01 23:04:09 +04:00
|
|
|
|
public class CashWithdrawalStorage : ICashWithdrawalStorage
|
2023-04-01 22:54:15 +04:00
|
|
|
|
{
|
2023-04-01 23:04:09 +04:00
|
|
|
|
public List<CashWithdrawalViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new BankYouBancruptDatabase();
|
|
|
|
|
|
|
|
|
|
return context.CashWithdrawals
|
2023-05-14 15:28:22 +04:00
|
|
|
|
.Include(x => x.Cashier)
|
|
|
|
|
.Include(x => x.Debiting)
|
2023-04-01 23:04:09 +04:00
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<CashWithdrawalViewModel> GetFilteredList(CashWithdrawalSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model.AccountId < 0)
|
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using var context = new BankYouBancruptDatabase();
|
|
|
|
|
|
2023-05-14 15:28:22 +04:00
|
|
|
|
if (model.CashierId.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return context.CashWithdrawals
|
|
|
|
|
.Include(x => x.Cashier)
|
|
|
|
|
.Include(x => x.Debiting)
|
|
|
|
|
.Where(x => x.CashierId == model.CashierId)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-01 23:04:09 +04:00
|
|
|
|
return context.CashWithdrawals
|
2023-05-14 15:28:22 +04:00
|
|
|
|
.Include(x => x.Cashier)
|
|
|
|
|
.Include(x => x.Debiting)
|
2023-04-01 23:04:09 +04:00
|
|
|
|
.Where(x => x.AccountId == model.AccountId)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CashWithdrawalViewModel? GetElement(CashWithdrawalSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model.AccountId < 0 && !model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using var context = new BankYouBancruptDatabase();
|
|
|
|
|
|
|
|
|
|
return context.CashWithdrawals
|
2023-05-14 15:28:22 +04:00
|
|
|
|
.Include(x => x.Cashier)
|
|
|
|
|
.Include(x => x.Debiting)
|
2023-04-01 23:04:09 +04:00
|
|
|
|
.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;
|
|
|
|
|
}
|
2023-04-01 22:54:15 +04:00
|
|
|
|
}
|
|
|
|
|
}
|