From 33a815d7cb1c218aa4e3e9678badb9097ac3e6ef Mon Sep 17 00:00:00 2001 From: Zakharov_Rostislav Date: Sun, 28 Apr 2024 21:09:30 +0400 Subject: [PATCH] add storages --- .../SearchModels/TransferSearchModel.cs | 3 +- .../SearchModels/WithdrawalSearchModel.cs | 3 +- .../StoragesContracts/ITransferStorage.cs | 12 +- .../ViewModels/RequestViewModel.cs | 1 - .../ViewModels/WithdrawalViewModel.cs | 2 +- .../Implements/AccountStorage.cs | 111 +++++++++++++++++ .../Implements/ManagerStorage.cs | 95 +++++++++++++++ .../Implements/TransferStorage.cs | 104 ++++++++++++++++ .../Implements/WithdrawalStorage.cs | 114 ++++++++++++++++++ 9 files changed, 435 insertions(+), 10 deletions(-) create mode 100644 Bank/BankDatabaseImplement/Implements/AccountStorage.cs create mode 100644 Bank/BankDatabaseImplement/Implements/ManagerStorage.cs create mode 100644 Bank/BankDatabaseImplement/Implements/TransferStorage.cs create mode 100644 Bank/BankDatabaseImplement/Implements/WithdrawalStorage.cs diff --git a/Bank/BankContracts/SearchModels/TransferSearchModel.cs b/Bank/BankContracts/SearchModels/TransferSearchModel.cs index 9c8438a..5816e4a 100644 --- a/Bank/BankContracts/SearchModels/TransferSearchModel.cs +++ b/Bank/BankContracts/SearchModels/TransferSearchModel.cs @@ -9,7 +9,8 @@ namespace BankContracts.SearchModels public class TransferSearchModel { public int? Id { get; set; } - public DateTime? TransferTime { get; set; } + public DateTime? DateTo { get; set; } + public DateTime? DateFrom { get; set; } public int? OperationId { get; set; } public int? SenderAccountId { get; set; } public int? RecipientAccountId { get; set; } diff --git a/Bank/BankContracts/SearchModels/WithdrawalSearchModel.cs b/Bank/BankContracts/SearchModels/WithdrawalSearchModel.cs index 6dddf98..3868d5a 100644 --- a/Bank/BankContracts/SearchModels/WithdrawalSearchModel.cs +++ b/Bank/BankContracts/SearchModels/WithdrawalSearchModel.cs @@ -9,7 +9,8 @@ namespace BankContracts.SearchModels public class WithdrawalSearchModel { public int? Id { get; set; } - public DateTime? WithdrawalTime { get; set; } + public DateTime? DateTo { get; set; } + public DateTime? DateFrom { get; set; } public int? RequestId { get; set; } } } diff --git a/Bank/BankContracts/StoragesContracts/ITransferStorage.cs b/Bank/BankContracts/StoragesContracts/ITransferStorage.cs index c1d329e..2c60342 100644 --- a/Bank/BankContracts/StoragesContracts/ITransferStorage.cs +++ b/Bank/BankContracts/StoragesContracts/ITransferStorage.cs @@ -11,11 +11,11 @@ namespace BankContracts.StoragesContracts { public interface ITransferStorage { - List GetFullList(); - List GetFilteredList(WithdrawalSearchModel model); - WithdrawalViewModel? GetElement(WithdrawalSearchModel model); - WithdrawalViewModel? Insert(WithdrawalBindingModel model); - WithdrawalViewModel? Update(WithdrawalBindingModel model); - WithdrawalViewModel? Delete(WithdrawalBindingModel model); + List GetFullList(); + List GetFilteredList(TransferSearchModel model); + TransferViewModel? GetElement(TransferSearchModel model); + TransferViewModel? Insert(TransferBindingModel model); + TransferViewModel? Update(TransferBindingModel model); + TransferViewModel? Delete(TransferBindingModel model); } } diff --git a/Bank/BankContracts/ViewModels/RequestViewModel.cs b/Bank/BankContracts/ViewModels/RequestViewModel.cs index b871f43..7fdf810 100644 --- a/Bank/BankContracts/ViewModels/RequestViewModel.cs +++ b/Bank/BankContracts/ViewModels/RequestViewModel.cs @@ -19,6 +19,5 @@ namespace BankContracts.ViewModels [DisplayName("Статус заявки")] public RequestStatus Status { get; set; } public Dictionary CardRequests { get; set; } = new(); - } } diff --git a/Bank/BankContracts/ViewModels/WithdrawalViewModel.cs b/Bank/BankContracts/ViewModels/WithdrawalViewModel.cs index b13d31b..1899a7c 100644 --- a/Bank/BankContracts/ViewModels/WithdrawalViewModel.cs +++ b/Bank/BankContracts/ViewModels/WithdrawalViewModel.cs @@ -16,6 +16,6 @@ namespace BankContracts.ViewModels public DateTime WithdrawalTime { get; set; } = DateTime.Now; [DisplayName("Номер заявки")] public int? RequestId { get; set; } - public Dictionary WithdrawalAccounts = new Dictionary(); + public Dictionary WithdrawalAccounts { get; set; } = new(); } } diff --git a/Bank/BankDatabaseImplement/Implements/AccountStorage.cs b/Bank/BankDatabaseImplement/Implements/AccountStorage.cs new file mode 100644 index 0000000..958a2e9 --- /dev/null +++ b/Bank/BankDatabaseImplement/Implements/AccountStorage.cs @@ -0,0 +1,111 @@ +using BankContracts.BindingModels; +using BankContracts.SearchModels; +using BankContracts.StoragesContracts; +using BankContracts.ViewModels; +using BankDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BankDatabaseImplement.Implements +{ + internal class AccountStorage : IAccountStorage + { + public List GetFullList() + { + using var context = new BankDatabase(); + return context.Accounts + .Include(x => x.ManagerId) + .Include(x => x.AccountWithdrawals) + .Include(x => x.SenderTransfers) + .Include(x => x.RecipientTransfers) + .Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(AccountSearchModel model) + { + using var context = new BankDatabase(); + return context.Accounts + .Include(x => x.ManagerId) + .Include(x => x.AccountWithdrawals) + .Include(x => x.SenderTransfers) + .Include(x => x.RecipientTransfers) + .Where(x => + (!model.Id.HasValue || x.Id == model.Id) && + (!model.ManagerId.HasValue || x.ManagerId == model.ManagerId) && + (string.IsNullOrEmpty(model.Number) || x.Number == model.Number) + ) + .Select(x => x.GetViewModel) + .ToList(); + } + + public AccountViewModel? GetElement(AccountSearchModel model) + { + using var context = new BankDatabase(); + if (model == null || (!model.Id.HasValue && string.IsNullOrEmpty(model.Number))) + { + return null; + } + return context.Accounts + .Include(x => x.ManagerId) + .Include(x => x.AccountWithdrawals) + .Include(x => x.SenderTransfers) + .Include(x => x.RecipientTransfers) + .FirstOrDefault(x => + (!model.Id.HasValue || x.Id == model.Id) && + (string.IsNullOrEmpty(model.Number) || x.Number == model.Number) + )?.GetViewModel; + } + + public AccountViewModel? Insert(AccountBindingModel model) + { + var newAccount = Account.Create(model); + if (newAccount == null) + { + return null; + } + using var context = new BankDatabase(); + context.Accounts.Add(newAccount); + context.SaveChanges(); + return newAccount.GetViewModel; + } + + public AccountViewModel? Update(AccountBindingModel model) + { + using var context = new BankDatabase(); + var account = context.Accounts + .Include(x => x.ManagerId) + .Include(x => x.AccountWithdrawals) + .Include(x => x.SenderTransfers) + .Include(x => x.RecipientTransfers) + .FirstOrDefault(x => x.Id == model.Id); + if (account == null) + { + return null; + } + account.Update(model); + context.SaveChanges(); + return account.GetViewModel; + } + + public AccountViewModel? Delete(AccountBindingModel model) + { + using var context = new BankDatabase(); + var account = context.Accounts + .Include(x => x.ManagerId) + .Include(x => x.AccountWithdrawals) + .Include(x => x.SenderTransfers).Include(x => x.RecipientTransfers) + .FirstOrDefault(rec => rec.Id == model.Id); + if (account != null) + { + context.Accounts.Remove(account); + context.SaveChanges(); + return account.GetViewModel; + } + return null; + } + } +} diff --git a/Bank/BankDatabaseImplement/Implements/ManagerStorage.cs b/Bank/BankDatabaseImplement/Implements/ManagerStorage.cs new file mode 100644 index 0000000..5949c8a --- /dev/null +++ b/Bank/BankDatabaseImplement/Implements/ManagerStorage.cs @@ -0,0 +1,95 @@ +using BankContracts.BindingModels; +using BankContracts.SearchModels; +using BankContracts.StoragesContracts; +using BankContracts.ViewModels; +using BankDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BankDatabaseImplement.Implements +{ + public class ManagerStorage : IManagerStorage + { + public List GetFullList() + { + using var context = new BankDatabase(); + return context.Managers.Include(x => x.Accounts).Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(ManagerSearchModel model) + { + if (string.IsNullOrEmpty(model.Fio) && string.IsNullOrEmpty(model.Email) && string.IsNullOrEmpty(model.Password)) + { + return new(); + } + using var context = new BankDatabase(); + return context.Managers + .Include(x => x.Accounts) + .Where(x => + string.IsNullOrEmpty(model.Fio) || x.Fio.Contains(model.Fio) && + (string.IsNullOrEmpty(model.Email) || x.Email.Contains(model.Email)) && + (string.IsNullOrEmpty(model.Password) || x.Password.Contains(model.Password))) + .Select(x => x.GetViewModel).ToList(); + } + + public ManagerViewModel? GetElement(ManagerSearchModel model) + { + if (string.IsNullOrEmpty(model.Fio) && string.IsNullOrEmpty(model.Email) && !model.Id.HasValue) + { + return null; + } + using var context = new BankDatabase(); + return context.Managers + .Include(x => x.Accounts) + .FirstOrDefault(x => + (string.IsNullOrEmpty(model.Fio) || x.Fio == model.Fio) && + (!model.Id.HasValue || x.Id == model.Id) && + (string.IsNullOrEmpty(model.Email) || x.Email == model.Email) && + (string.IsNullOrEmpty(model.Password) || x.Password == model.Password))? + .GetViewModel; + } + + public ManagerViewModel? Insert(ManagerBindingModel model) + { + var newManager = Manager.Create(model); + if (newManager == null) + { + return null; + } + using var context = new BankDatabase(); + context.Managers.Add(newManager); + context.SaveChanges(); + return newManager.GetViewModel; + } + + public ManagerViewModel? Update(ManagerBindingModel model) + { + using var context = new BankDatabase(); + var manager = context.Managers.FirstOrDefault(x => x.Id == model.Id); + if (manager == null) + { + return null; + } + manager.Update(model); + context.SaveChanges(); + return manager.GetViewModel; + } + + public ManagerViewModel? Delete(ManagerBindingModel model) + { + using var context = new BankDatabase(); + var element = context.Managers.Include(x => x.Accounts).FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Managers.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/Bank/BankDatabaseImplement/Implements/TransferStorage.cs b/Bank/BankDatabaseImplement/Implements/TransferStorage.cs new file mode 100644 index 0000000..5e3e8bb --- /dev/null +++ b/Bank/BankDatabaseImplement/Implements/TransferStorage.cs @@ -0,0 +1,104 @@ +using BankContracts.BindingModels; +using BankContracts.SearchModels; +using BankContracts.StoragesContracts; +using BankContracts.ViewModels; +using BankDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BankDatabaseImplement.Implements +{ + public class TransferStorage : ITransferStorage + { + public List GetFullList() + { + using var context = new BankDatabase(); + return context.Transfers + .Include(x => x.SenderAccount) + .Include(x => x.RecipientAccount) + .Include (x => x.Operation) + .Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(TransferSearchModel model) + { + using var context = new BankDatabase(); + return context.Transfers + .Include(x => x.SenderAccount) + .Include(x => x.RecipientAccount) + .Include(x => x.Operation) + .Where(x => + (!model.Id.HasValue || x.Id == model.Id) && + (!model.SenderAccountId.HasValue || x.SenderAccountId == model.SenderAccountId) && + (!model.RecipientAccountId.HasValue || x.RecipientAccountId == model.RecipientAccountId) && + (!model.OperationId.HasValue || x.OperationId == model.OperationId) && + (!model.DateFrom.HasValue || x.TransferTime >= model.DateFrom) && + (!model.DateTo.HasValue || x.TransferTime <= model.DateTo) + ).Select(x => x.GetViewModel).ToList(); + } + + public TransferViewModel? GetElement(TransferSearchModel model) + { + if (!model.Id.HasValue) + return null; + using var context = new BankDatabase(); + return context.Transfers + .Include(x => x.SenderAccount) + .Include(x => x.RecipientAccount) + .Include(x => x.Operation) + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)? + .GetViewModel; + } + + public TransferViewModel? Insert(TransferBindingModel model) + { + var newTransfer = Transfer.Create(model); + if (newTransfer == null) + { + return null; + } + using var context = new BankDatabase(); + context.Transfers.Add(newTransfer); + context.SaveChanges(); + return newTransfer.GetViewModel; + } + + public TransferViewModel? Update(TransferBindingModel model) + { + using var context = new BankDatabase(); + var operation = context.Transfers + .Include(x => x.SenderAccount) + .Include(x => x.RecipientAccount) + .Include(x => x.Operation) + .FirstOrDefault(x => x.Id == model.Id); + if (operation == null) + { + return null; + } + operation.Update(model); + context.SaveChanges(); + return operation.GetViewModel; + } + + public TransferViewModel? Delete(TransferBindingModel model) + { + using var context = new BankDatabase(); + var transfer = context.Transfers + .Include(x => x.SenderAccount) + .Include(x => x.RecipientAccount) + .Include(x => x.Operation) + .FirstOrDefault(rec => rec.Id == model.Id); + if (transfer != null) + { + context.Transfers.Remove(transfer); + context.SaveChanges(); + return transfer.GetViewModel; + } + return null; + } + } +} diff --git a/Bank/BankDatabaseImplement/Implements/WithdrawalStorage.cs b/Bank/BankDatabaseImplement/Implements/WithdrawalStorage.cs new file mode 100644 index 0000000..b83222b --- /dev/null +++ b/Bank/BankDatabaseImplement/Implements/WithdrawalStorage.cs @@ -0,0 +1,114 @@ +using BankContracts.BindingModels; +using BankContracts.SearchModels; +using BankContracts.StoragesContracts; +using BankContracts.ViewModels; +using BankDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BankDatabaseImplement.Implements +{ + public class WithdrawalStorage : IWithdrawalStorage + { + public List GetFullList() + { + using var context = new BankDatabase(); + return context.Withdrawals + .Include(x => x.Request) + .Include(x => x.Accounts) + .ThenInclude(x => x.Account) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(WithdrawalSearchModel model) + { + using var context = new BankDatabase(); + return context.Withdrawals + .Include(x => x.Request) + .Include(x => x.Accounts) + .ThenInclude(x => x.Account) + .Where(x => + (!model.Id.HasValue || x.Id == model.Id) && + (!model.RequestId.HasValue || x.RequestId == model.RequestId) && + (!model.DateFrom.HasValue || x.WithdrawalTime >= model.DateFrom) && + (!model.DateTo.HasValue || x.WithdrawalTime <= model.DateTo)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public WithdrawalViewModel? GetElement(WithdrawalSearchModel model) + { + if (!model.Id.HasValue) + return null; + using var context = new BankDatabase(); + return context.Withdrawals + .Include(x => x.Request) + .Include(x => x.Accounts) + .ThenInclude(x => x.Account) + .FirstOrDefault(x => x.Id == model.Id)? + .GetViewModel; + } + + public WithdrawalViewModel? Insert(WithdrawalBindingModel model) + { + using var context = new BankDatabase(); + var newWithdrawal = Withdrawal.Create(context, model); + if (newWithdrawal == null) + { + return null; + } + context.Withdrawals.Add(newWithdrawal); + context.SaveChanges(); + return newWithdrawal.GetViewModel; + } + + public WithdrawalViewModel? Update(WithdrawalBindingModel model) + { + using var context = new BankDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var Withdrawal = context.Withdrawals.FirstOrDefault(rec => + rec.Id == model.Id); + if (Withdrawal == null) + { + return null; + } + Withdrawal.Update(model); + context.SaveChanges(); + Withdrawal.UpdateAccounts(context, model); + transaction.Commit(); + return Withdrawal.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public WithdrawalViewModel? Delete(WithdrawalBindingModel model) + { + using var context = new BankDatabase(); + var element = context.Withdrawals + .Include(x => x.Request) + .Include(x => x.Accounts) + .ThenInclude(x => x.Account) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Withdrawals.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +}