This commit is contained in:
shadowik 2023-04-01 19:09:33 +04:00
commit 218dfa76bf
2 changed files with 113 additions and 2 deletions

View File

@ -1,4 +1,10 @@
using System; 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.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@ -6,7 +12,107 @@ using System.Threading.Tasks;
namespace BankYouBankruptDatabaseImplement.Implements namespace BankYouBankruptDatabaseImplement.Implements
{ {
internal class CashWithdrawalStorage public class CashWithdrawalStorage : ICashWithdrawalStorage
{ {
public List<CashWithdrawalViewModel> GetFullList()
{
using var context = new BankYouBancruptDatabase();
return context.CashWithdrawals
.Select(x => x.GetViewModel)
.ToList();
}
public List<CashWithdrawalViewModel> GetFilteredList(CashWithdrawalSearchModel model)
{
if (model.AccountId < 0)
{
return new();
}
using var context = new BankYouBancruptDatabase();
return context.CashWithdrawals
.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
.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;
}
} }
} }

View File

@ -39,6 +39,11 @@ namespace BankYouBankruptDatabaseImplement.Models
}; };
} }
public void Update(CashWithdrawalBindingModel model)
{
DateOperation = model.DateOperation;
}
public CashWithdrawalViewModel GetViewModel => new() public CashWithdrawalViewModel GetViewModel => new()
{ {
Id = Id, Id = Id,