From c27f9432f4fd92c44fb96aceaae8d5a4802e2eeb Mon Sep 17 00:00:00 2001 From: Programmist73 Date: Sat, 1 Apr 2023 23:04:09 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BF=D0=BE=D1=81=D0=BB=D0=B5=D0=B4=D0=BD?= =?UTF-8?q?=D0=B5=D0=B3=D0=BE=20Storage=20=D0=B4=D0=BB=D1=8F=20=D0=BA?= =?UTF-8?q?=D0=B0=D1=81=D1=81=D0=B8=D1=80=D0=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/CashWithdrawalStorage.cs | 110 +++++++++++++++++- .../Models/CashWithdrawal.cs | 5 + 2 files changed, 113 insertions(+), 2 deletions(-) diff --git a/BankYouBankrupt/BankYouBankruptDatabaseImplement/Implements/CashWithdrawalStorage.cs b/BankYouBankrupt/BankYouBankruptDatabaseImplement/Implements/CashWithdrawalStorage.cs index 391cb55..87f9bd4 100644 --- a/BankYouBankrupt/BankYouBankruptDatabaseImplement/Implements/CashWithdrawalStorage.cs +++ b/BankYouBankrupt/BankYouBankruptDatabaseImplement/Implements/CashWithdrawalStorage.cs @@ -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.Linq; using System.Text; @@ -6,7 +12,107 @@ using System.Threading.Tasks; namespace BankYouBankruptDatabaseImplement.Implements { - internal class CashWithdrawalStorage + public class CashWithdrawalStorage : ICashWithdrawalStorage { + public List GetFullList() + { + using var context = new BankYouBancruptDatabase(); + + return context.CashWithdrawals + .Select(x => x.GetViewModel) + .ToList(); + } + + public List 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; + } } } diff --git a/BankYouBankrupt/BankYouBankruptDatabaseImplement/Models/CashWithdrawal.cs b/BankYouBankrupt/BankYouBankruptDatabaseImplement/Models/CashWithdrawal.cs index a5f0f14..331e694 100644 --- a/BankYouBankrupt/BankYouBankruptDatabaseImplement/Models/CashWithdrawal.cs +++ b/BankYouBankrupt/BankYouBankruptDatabaseImplement/Models/CashWithdrawal.cs @@ -39,6 +39,11 @@ namespace BankYouBankruptDatabaseImplement.Models }; } + public void Update(CashWithdrawalBindingModel model) + { + DateOperation = model.DateOperation; + } + public CashWithdrawalViewModel GetViewModel => new() { Id = Id,