diff --git a/Bank/BankContracts/SearchModels/Cashier/AccountSearchModel.cs b/Bank/BankContracts/SearchModels/Cashier/AccountSearchModel.cs index 523f7c7..4066d2f 100644 --- a/Bank/BankContracts/SearchModels/Cashier/AccountSearchModel.cs +++ b/Bank/BankContracts/SearchModels/Cashier/AccountSearchModel.cs @@ -10,18 +10,18 @@ namespace BankContracts.SearchModels.Cashier // Для поиска сущности "Счёт" public class AccountSearchModel { - public int Id { get; set; } + public int? Id { get; set; } - public int CashierId { get; set; } + public int? CashierId { get; set; } - public int ClientId { get; set; } + public int? ClientId { get; set; } - public string AccountNumber { get; set; } = string.Empty; + public string? AccountNumber { get; set; } = string.Empty; - public double Balance { get; set; } + public double? Balance { get; set; } - public DateTime DateOpen { get; set; } + public DateTime? DateOpen { get; set; } - public StatusAccount StatusAccount { get; set; } + public StatusAccount? StatusAccount { get; set; } } } diff --git a/Bank/BankContracts/SearchModels/Cashier/CashierSearchModel.cs b/Bank/BankContracts/SearchModels/Cashier/CashierSearchModel.cs index 6571d9b..e88c2a1 100644 --- a/Bank/BankContracts/SearchModels/Cashier/CashierSearchModel.cs +++ b/Bank/BankContracts/SearchModels/Cashier/CashierSearchModel.cs @@ -9,18 +9,18 @@ namespace BankContracts.SearchModels.Cashier // Для поиска сущности "Кассир" public class CashierSearchModel { - public int Id { get; set; } + public int? Id { get; set; } - public string Name { get; set; } = string.Empty; + public string? Name { get; set; } = string.Empty; - public string Surname { get; set; } = string.Empty; + public string? Surname { get; set; } = string.Empty; - public string Patronymic { get; set; } = string.Empty; + public string? Patronymic { get; set; } = string.Empty; - public string Email { get; set; } = string.Empty; + public string? Email { get; set; } = string.Empty; - public string Password { get; set; } = string.Empty; + public string? Password { get; set; } = string.Empty; - public string MobilePhone { get; set; } = string.Empty; + public string? MobilePhone { get; set; } = string.Empty; } } diff --git a/Bank/BankContracts/SearchModels/Client/ClientSearchModel.cs b/Bank/BankContracts/SearchModels/Client/ClientSearchModel.cs index 6b19a4c..caf5960 100644 --- a/Bank/BankContracts/SearchModels/Client/ClientSearchModel.cs +++ b/Bank/BankContracts/SearchModels/Client/ClientSearchModel.cs @@ -9,18 +9,18 @@ namespace BankContracts.SearchModels.Client // Для поиска сущности "Клиент" public class ClientSearchModel { - public int Id { get; set; } + public int? Id { get; set; } - public string Name { get; set; } = string.Empty; + public string? Name { get; set; } = string.Empty; - public string Surname { get; set; } = string.Empty; + public string? Surname { get; set; } = string.Empty; - public string Patronymic { get; set; } = string.Empty; + public string? Patronymic { get; set; } = string.Empty; - public string Email { get; set; } = string.Empty; + public string? Email { get; set; } = string.Empty; - public string Password { get; set; } = string.Empty; + public string? Password { get; set; } = string.Empty; - public string MobilePhone { get; set; } = string.Empty; + public string? MobilePhone { get; set; } = string.Empty; } } diff --git a/Bank/BankDatabaseImplement/BankDatabaseImplement.csproj b/Bank/BankDatabaseImplement/BankDatabaseImplement.csproj index 3a35192..6ba6f73 100644 --- a/Bank/BankDatabaseImplement/BankDatabaseImplement.csproj +++ b/Bank/BankDatabaseImplement/BankDatabaseImplement.csproj @@ -21,8 +21,4 @@ - - - - diff --git a/Bank/BankDatabaseImplement/Implements/CashierImplements/AccountStorage.cs b/Bank/BankDatabaseImplement/Implements/CashierImplements/AccountStorage.cs new file mode 100644 index 0000000..0ec1513 --- /dev/null +++ b/Bank/BankDatabaseImplement/Implements/CashierImplements/AccountStorage.cs @@ -0,0 +1,145 @@ +using BankContracts.BindingModels.Cashier; +using BankContracts.SearchModels.Cashier; +using BankContracts.StoragesModels.Cashier; +using BankContracts.ViewModels; +using BankDatabaseImplement.Models.CashierModels; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BankDatabaseImplement.Implements.CashierImplements +{ + public class AccountStorage : IAccountStorage + { + public List GetFullList() + { + using var context = new BankDatabase(); + + return context.Accounts + .Include(x => x.Client) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(AccountSearchModel model) + { + using var context = new BankDatabase(); + + if (!string.IsNullOrEmpty(model.AccountNumber)) + { + return context.Accounts + .Include(x => x.Client) + .Where(x => x.AccountNumber.Contains(model.AccountNumber)) + .Select(x => x.GetViewModel) + .ToList(); + } + + if (model.CashierId.HasValue) + { + return context.Accounts + .Include(x => x.Client) + .Where(x => x.CashierId == model.CashierId) + .Select(x => x.GetViewModel) + .ToList(); + } + + if (model.ClientId.HasValue) + { + return context.Accounts + .Include(x => x.Client) + .Where(x => x.ClientId == model.ClientId) + .Select(x => x.GetViewModel) + .ToList(); + } + + return context.Accounts + .Include(x => x.Client) + .Select(x => x.GetViewModel) + .ToList(); + } + + public AccountViewModel? GetElement(AccountSearchModel model) + { + if (string.IsNullOrEmpty(model.AccountNumber) && !model.Id.HasValue) + { + return null; + } + + using var context = new BankDatabase(); + + return context.Accounts + .Include(x => x.Client) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.AccountNumber) && x.AccountNumber == model.AccountNumber) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public AccountViewModel? Insert(AccountBindingModel model) + { + using var context = new BankDatabase(); + + var newAccount = Account.Create(context, model); + + if (newAccount == null) + { + return null; + } + + context.Accounts.Add(newAccount); + context.SaveChanges(); + + return newAccount.GetViewModel; + } + + public AccountViewModel? Update(AccountBindingModel model) + { + using var context = new BankDatabase(); + using var transaction = context.Database.BeginTransaction(); + + try + { + var account = context.Accounts.FirstOrDefault(rec => rec.Id == model.Id); + + if (account == null) + { + return null; + } + + account.Update(model); + context.SaveChanges(); + transaction.Commit(); + + return context.Accounts + .Include(x => x.Client) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public AccountViewModel? Delete(AccountBindingModel model) + { + using var context = new BankDatabase(); + var element = context.Accounts + .Include(x => x.AccountNumber) + .FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.Accounts.Remove(element); + context.SaveChanges(); + + return element.GetViewModel; + } + + return null; + } + } +} diff --git a/Bank/BankDatabaseImplement/Implements/CashierImplements/CashWithdrawalStorage.cs b/Bank/BankDatabaseImplement/Implements/CashierImplements/CashWithdrawalStorage.cs new file mode 100644 index 0000000..876ad2f --- /dev/null +++ b/Bank/BankDatabaseImplement/Implements/CashierImplements/CashWithdrawalStorage.cs @@ -0,0 +1,123 @@ +using BankContracts.BindingModels.Cashier; +using BankContracts.SearchModels.Cashier; +using BankContracts.StoragesModels.Cashier; +using BankContracts.ViewModels.Cashier.ViewModels; +using BankDatabaseImplement.Models.CashierModels; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BankDatabaseImplement.Implements.CashierImplements +{ + public class CashWithdrawalStorage : ICashWithdrawalStorage + { + public List GetFullList() + { + using var context = new BankDatabase(); + + 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 BankDatabase(); + 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.DateWithdrawal.HasValue) result = result.Where(x => x.DateWithdrawal >= model.DateWithdrawal).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 BankDatabase(); + + 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 BankDatabase(); + + 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 BankDatabase(); + 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 BankDatabase(); + 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/Bank/BankDatabaseImplement/Implements/CashierImplements/CashierStorage.cs b/Bank/BankDatabaseImplement/Implements/CashierImplements/CashierStorage.cs new file mode 100644 index 0000000..393d299 --- /dev/null +++ b/Bank/BankDatabaseImplement/Implements/CashierImplements/CashierStorage.cs @@ -0,0 +1,104 @@ +using BankContracts.BindingModels.Cashier; +using BankContracts.SearchModels.Cashier; +using BankContracts.StoragesModels.Cashier; +using BankContracts.ViewModels.Cashier.ViewModels; +using BankDatabaseImplement.Models.CashierModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BankDatabaseImplement.Implements.CashierImplements +{ + public class CashierStorage : ICashierStorage + { + public List GetFullList() + { + using var context = new BankDatabase(); + + return context.Cashiers + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(CashierSearchModel model) + { + if (string.IsNullOrEmpty(model.Email)) + { + return new(); + } + + using var context = new BankDatabase(); + + return context.Cashiers + .Where(x => x.Email.Contains(model.Email)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public CashierViewModel? GetElement(CashierSearchModel model) + { + if (string.IsNullOrEmpty(model.Email) && !model.Id.HasValue) + { + return null; + } + + using var context = new BankDatabase(); + + return context.Cashiers + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Email) && x.Email == model.Email) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public CashierViewModel? Insert(CashierBindingModel model) + { + using var context = new BankDatabase(); + + var newCashier = Cashier.Create(context, model); + + if (newCashier == null) + { + return null; + } + + context.Cashiers.Add(newCashier); + context.SaveChanges(); + + return newCashier.GetViewModel; + } + + public CashierViewModel? Update(CashierBindingModel model) + { + using var context = new BankDatabase(); + var cashier = context.Cashiers.FirstOrDefault(x => x.Id == model.Id); + + if (cashier == null) + { + return null; + } + + cashier.Update(model); + context.SaveChanges(); + + return cashier.GetViewModel; + } + + public CashierViewModel? Delete(CashierBindingModel model) + { + using var context = new BankDatabase(); + var element = context.Cashiers.FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.Cashiers.Remove(element); + context.SaveChanges(); + + return element.GetViewModel; + } + + return null; + } + } +} diff --git a/Bank/BankDatabaseImplement/Implements/CashierImplements/MoneyTransferStorage.cs b/Bank/BankDatabaseImplement/Implements/CashierImplements/MoneyTransferStorage.cs new file mode 100644 index 0000000..fa294fd --- /dev/null +++ b/Bank/BankDatabaseImplement/Implements/CashierImplements/MoneyTransferStorage.cs @@ -0,0 +1,145 @@ +using BankContracts.BindingModels.Cashier; +using BankContracts.SearchModels.Cashier; +using BankContracts.StoragesModels.Cashier; +using BankContracts.ViewModels.Cashier.ViewModels; +using BankDatabaseImplement.Models.CashierModels; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BankDatabaseImplement.Implements.CashierImplements +{ + public class MoneyTransferStorage : IMoneyTransferStorage + { + public List GetFullList() + { + using var context = new BankDatabase(); + + return context.MoneyTransfers + .Include(x => x.AccountPayeer) + .Include(x => x.AccountSender) + .Include(x => x.Cashier) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(MoneyTransferSearchModel model) + { + using var context = new BankDatabase(); + var result = context.MoneyTransfers.Include(x => x.Cashier) + .Include(x => x.AccountPayeer) + .Include(x => x.AccountSender) + .ToList(); + + if (model.AccountPayeeId.HasValue && model.AccountSenderId.HasValue && model.AccountPayeeId == model.AccountSenderId) + { + return result.Where(x => (x.AccountSenderId == model.AccountSenderId || x.AccountPayeeId == model.AccountPayeeId) + && x.AccountSender != null) + .Select(x => x.GetViewModel) + .ToList(); + } + + if (model.AccountPayeeId.HasValue) result = result.Where(x => x.AccountPayeeId == model.AccountPayeeId).ToList(); + + if (model.AccountSenderId.HasValue) result = result.Where(x => x.AccountSenderId == model.AccountSenderId).ToList(); + + if (model.CashierId.HasValue) result = result.Where(x => x.CashierId == model.CashierId).ToList(); + + if (model.DateTransfer.HasValue) result = result.Where(x => x.DateTransfer >= model.DateTransfer).ToList(); + + if (model.ClientId.HasValue) + { + result = result.Where(x => x.AccountSender != null) + .Where(x => (x.AccountPayeer.ClientId == model.ClientId || x.AccountSender.ClientId == model.ClientId)) + .ToList(); + } + + return result.Select(x => x.GetViewModel).ToList(); + } + + public MoneyTransferViewModel? GetElement(MoneyTransferSearchModel model) + { + if (model.AccountSenderId < 0 && !model.Id.HasValue) + { + return null; + } + + using var context = new BankDatabase(); + + return context.MoneyTransfers + .Include(x => x.AccountPayeer) + .Include(x => x.AccountSender) + .Include(x => x.Cashier) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + } + + public MoneyTransferViewModel? Insert(MoneyTransferBindingModel model) + { + using var context = new BankDatabase(); + + var newMoneyTransfer = MoneyTransfer.Create(context, model); + + if (newMoneyTransfer == null) + { + return null; + } + + context.MoneyTransfers.Add(newMoneyTransfer); + context.SaveChanges(); + + return newMoneyTransfer.GetViewModel; + } + + public MoneyTransferViewModel? Update(MoneyTransferBindingModel model) + { + using var context = new BankDatabase(); + using var transaction = context.Database.BeginTransaction(); + + try + { + var moneyTransfer = context.MoneyTransfers.FirstOrDefault(rec => rec.Id == model.Id); + + if (moneyTransfer == null) + { + return null; + } + + moneyTransfer.Update(model); + context.SaveChanges(); + transaction.Commit(); + + return moneyTransfer.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public MoneyTransferViewModel? Delete(MoneyTransferBindingModel model) + { + using var context = new BankDatabase(); + var element = context.MoneyTransfers + .Include(x => x.AccountPayeeId) + .Include(x => x.AccountSenderId) + .Include(x => x.Sum) + .Include(x => x.DateTransfer) + .FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.MoneyTransfers.Remove(element); + context.SaveChanges(); + + return element.GetViewModel; + } + + return null; + } + } +} diff --git a/Bank/BankDatabaseImplement/Implements/ClientImplements/CardStorage.cs b/Bank/BankDatabaseImplement/Implements/ClientImplements/CardStorage.cs new file mode 100644 index 0000000..582f434 --- /dev/null +++ b/Bank/BankDatabaseImplement/Implements/ClientImplements/CardStorage.cs @@ -0,0 +1,143 @@ +using BankContracts.BindingModels.Client; +using BankContracts.SearchModels.Client; +using BankContracts.StoragesModels.Client; +using BankContracts.ViewModels.Client.ViewModels; +using BankDatabaseImplement.Models.ClientModels; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BankDatabaseImplement.Implements.ClientImplements +{ + public class CardStorage : ICardStorage + { + public CardViewModel? Delete(CardBindingModel model) + { + using var context = new BankDatabase(); + var element = context.Cards.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Cards.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public CardViewModel? GetElement(CardSearchModel model) + { + if (string.IsNullOrEmpty(model.Number) && !model.Id.HasValue) + { + return null; + } + + using var context = new BankDatabase(); + + return context.Cards + .Include(x => x.Client) + .Include(x => x.Account) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Number) && x.Number == model.Number) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public List GetFilteredList(CardSearchModel model) + { + using var context = new BankDatabase(); + + if (!string.IsNullOrEmpty(model.Number)) + { + return context.Cards + .Include(x => x.Client) + .Include(x => x.Account) + .Where(x => x.Number.Contains(model.Number)) + .Select(x => x.GetViewModel) + .ToList(); + } + + if (model.ClientID.HasValue) + { + return context.Cards + .Include(x => x.Client) + .Include(x => x.Account) + .Where(x => x.ClientId == model.ClientID) + .Select(x => x.GetViewModel) + .ToList(); + } + + if (model.AccountId.HasValue) + { + return context.Cards + .Include(x => x.Client) + .Include(x => x.Account) + .Where(x => x.AccountId == model.AccountId) + .Select(x => x.GetViewModel) + .ToList(); + } + + return context.Cards + .Include(x => x.Client) + .Include(x => x.Account) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new BankDatabase(); + + return context.Cards + .Include(x => x.Client) + .Include(x => x.Account) + .Select(x => x.GetViewModel) + .ToList(); + } + + public CardViewModel? Insert(CardBindingModel model) + { + using var context = new BankDatabase(); + + var newCard = Card.Create(context, model); + + if (newCard == null) + { + return null; + } + + context.Cards.Add(newCard); + context.SaveChanges(); + + return newCard.GetViewModel; + } + + public CardViewModel? Update(CardBindingModel model) + { + using var context = new BankDatabase(); + using var transaction = context.Database.BeginTransaction(); + + try + { + var card = context.Cards.FirstOrDefault(rec => rec.Id == model.Id); + + if (card == null) + { + return null; + } + + card.Update(model); + context.SaveChanges(); + transaction.Commit(); + + return card.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + } +} diff --git a/Bank/BankDatabaseImplement/Implements/ClientImplements/ClientStorage.cs b/Bank/BankDatabaseImplement/Implements/ClientImplements/ClientStorage.cs new file mode 100644 index 0000000..b8be733 --- /dev/null +++ b/Bank/BankDatabaseImplement/Implements/ClientImplements/ClientStorage.cs @@ -0,0 +1,92 @@ +using BankContracts.BindingModels.Client; +using BankContracts.SearchModels.Client; +using BankContracts.StoragesModels.Client; +using BankContracts.ViewModels.Client.ViewModels; +using BankDatabaseImplement.Models.ClientModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BankDatabaseImplement.Implements.ClientImplements +{ + public class ClientStorage : IClientStorage + { + public ClientViewModel? Delete(ClientBindingModel model) + { + using var context = new BankDatabase(); + var element = context.Clients.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Clients.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public ClientViewModel? GetElement(ClientSearchModel model) + { + if (string.IsNullOrEmpty(model.Email) && string.IsNullOrEmpty(model.Password) && !model.Id.HasValue) + { + return null; + } + + using var context = new BankDatabase(); + + //сработает для поиска почты для отправки файла + if (model.Id.HasValue && string.IsNullOrEmpty(model.Password)) + { + return context.Clients.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + } + + return context.Clients.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.Email) && x.Email == model.Email && !string.IsNullOrEmpty(model.Password) && x.Password == model.Password) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public List GetFilteredList(ClientSearchModel model) + { + if (string.IsNullOrEmpty(model.Name)) + { + return new(); + } + using var context = new BankDatabase(); + return context.Clients.Where(x => x.Name.Contains(model.Name)).Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + using var context = new BankDatabase(); + return context.Clients.Select(x => x.GetViewModel).ToList(); + } + + public ClientViewModel? Insert(ClientBindingModel model) + { + var newComponent = Client.Create(model); + if (newComponent == null) + { + return null; + } + using var context = new BankDatabase(); + context.Clients.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + + public ClientViewModel? Update(ClientBindingModel model) + { + using var context = new BankDatabase(); + var component = context.Clients.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + } +} diff --git a/Bank/BankDatabaseImplement/Implements/ClientImplements/CreditingStorage.cs b/Bank/BankDatabaseImplement/Implements/ClientImplements/CreditingStorage.cs new file mode 100644 index 0000000..6577b4e --- /dev/null +++ b/Bank/BankDatabaseImplement/Implements/ClientImplements/CreditingStorage.cs @@ -0,0 +1,124 @@ +using BankContracts.BindingModels.Client; +using BankContracts.SearchModels.Client; +using BankContracts.StoragesModels.Client; +using BankContracts.ViewModels.Client.ViewModels; +using BankDatabaseImplement.Models.ClientModels; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BankDatabaseImplement.Implements.ClientImplements +{ + public class CreditingStorage : ICreditingStorage + { + public List GetFullList() + { + using var context = new BankDatabase(); + + return context.Creditings + .Include(x => x.Card) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(CreditingSearchModel model) + { + using var context = new BankDatabase(); + var result = context.Creditings.Include(x => x.Card).ToList(); + + + if (model.CardId.HasValue) result = result.Where(x => x.CardId == model.CardId).ToList(); + + if (model.ClientId.HasValue) + { + List cards = context.Cards.Where(x => x.ClientId == model.ClientId).Select(x => x.Id).ToList(); + result = result.Where(x => cards.Contains(x.CardId)).ToList(); + } + + return result.Select(x => x.GetViewModel).ToList(); + } + + public CreditingViewModel? GetElement(CreditingSearchModel model) + { + if (model.CardId < 0 && !model.Id.HasValue) + { + return null; + } + + using var context = new BankDatabase(); + + return context.Creditings + .Include(x => x.Card) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + } + + public CreditingViewModel? Insert(CreditingBindingModel model) + { + using var context = new BankDatabase(); + + var newCrediting = Crediting.Create(context, model); + + if (newCrediting == null) + { + return null; + } + + context.Creditings.Add(newCrediting); + context.SaveChanges(); + + return newCrediting.GetViewModel; + } + + public CreditingViewModel? Update(CreditingBindingModel model) + { + using var context = new BankDatabase(); + using var transaction = context.Database.BeginTransaction(); + + try + { + var crediting = context.Creditings.FirstOrDefault(rec => rec.Id == model.Id); + + if (crediting == null) + { + return null; + } + + crediting.Update(model); + context.SaveChanges(); + transaction.Commit(); + + return context.Creditings + .Include(x => x.Card) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public CreditingViewModel? Delete(CreditingBindingModel model) + { + using var context = new BankDatabase(); + var element = context.Creditings + .Include(x => x.CardId) + .FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.Creditings.Remove(element); + context.SaveChanges(); + + return element.GetViewModel; + } + + return null; + } + } +} diff --git a/Bank/BankDatabaseImplement/Implements/ClientImplements/DebitingStorage.cs b/Bank/BankDatabaseImplement/Implements/ClientImplements/DebitingStorage.cs new file mode 100644 index 0000000..7bee161 --- /dev/null +++ b/Bank/BankDatabaseImplement/Implements/ClientImplements/DebitingStorage.cs @@ -0,0 +1,122 @@ +using BankContracts.BindingModels.Client; +using BankContracts.SearchModels.Client; +using BankContracts.StoragesModels.Client; +using BankContracts.ViewModels.Client.ViewModels; +using BankDatabaseImplement.Models.ClientModels; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BankDatabaseImplement.Implements.ClientImplements +{ + public class DebitingStorage : IDebitingStorage + { + public List GetFullList() + { + using var context = new BankDatabase(); + + return context.Debitings + .Include(x => x.Card) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(DebitingSearchModel model) + { + using var context = new BankDatabase(); + + var result = context.Debitings.Include(x => x.Card).ToList(); + + //для получения всех заявок на стнятие + + if (model.CardId.HasValue) result = result.Where(x => x.CardId == model.CardId).ToList(); + + if (model.ClientId.HasValue) + { + List cards = context.Cards.Where(x => x.ClientId == model.ClientId).Select(x => x.Id).ToList(); + result = result.Where(x => cards.Contains(x.CardId)).ToList(); + } + + return result.Select(x => x.GetViewModel).ToList(); + } + + public DebitingViewModel? GetElement(DebitingSearchModel model) + { + if (model.CardId < 0 && !model.Id.HasValue) + { + return null; + } + + using var context = new BankDatabase(); + + return context.Debitings + .Include(x => x.Card) + .FirstOrDefault(x => (!(model.CardId < 0) && x.CardId == model.CardId) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public DebitingViewModel? Insert(DebitingBindingModel model) + { + using var context = new BankDatabase(); + + var newDebiting = Debiting.Create(context, model); + + if (newDebiting == null) + { + return null; + } + + context.Debitings.Add(newDebiting); + context.SaveChanges(); + + return newDebiting.GetViewModel; + } + + public DebitingViewModel? Update(DebitingBindingModel model) + { + using var context = new BankDatabase(); + using var transaction = context.Database.BeginTransaction(); + + try + { + var debiting = context.Debitings.FirstOrDefault(rec => rec.Id == model.Id); + + debiting.Update(model); + context.SaveChanges(); + transaction.Commit(); + + return context.Debitings + .Include(x => x.Card) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public DebitingViewModel? Delete(DebitingBindingModel model) + { + using var context = new BankDatabase(); + var element = context.Debitings + .Include(x => x.CardId) + .FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.Debitings.Remove(element); + context.SaveChanges(); + + return element.GetViewModel; + } + + return null; + } + } +} diff --git a/Bank/BankDatabaseImplement/Models/CashierModels/Account.cs b/Bank/BankDatabaseImplement/Models/CashierModels/Account.cs index 7400355..fa29e3b 100644 --- a/Bank/BankDatabaseImplement/Models/CashierModels/Account.cs +++ b/Bank/BankDatabaseImplement/Models/CashierModels/Account.cs @@ -1,4 +1,6 @@ -using BankDatabaseImplement.Models.ClientModels; +using BankContracts.BindingModels.Cashier; +using BankContracts.ViewModels; +using BankDatabaseImplement.Models.ClientModels; using BankDataModels.Enums; using BankDataModels.Models.Cashier; using System; @@ -15,22 +17,76 @@ namespace BankDatabaseImplement.Models.CashierModels public class Account : IAccountModel { public int Id { get; set; } + [Required] public string AccountNumber { get; set; } = string.Empty; + [Required] public int CashierId { get; set; } + [Required] public int ClientId { get; set; } - //чтобы передать ФИО клиета + + //для передачи ФИО клиента public virtual Client Client { get; set; } + [Required] public double Balance { get; set; } + [Required] - public DateTime DateOpen { get; set; } + public DateTime DateOpen { get; set; } = DateTime.Now; + [Required] - public StatusAccount StatusAccount { get; set; } + public StatusAccount StatusAccount { get; set; } = StatusAccount.Открыт; + //для реализации связи один ко многим со Снятием наличных [ForeignKey("AccountId")] public virtual List CashWithdrawals { get; set; } = new(); + + //для реализации связи один ко многим с Переводом денег + [NotMapped] + [ForeignKey("AccountSenderId")] + public virtual List MoneyTransferSenders { get; set; } = new(); + + [NotMapped] + [ForeignKey("AccountPayeeId")] + public virtual List MoneyTransferPayees { get; set; } = new(); + + //для реализации связи один ко многим с Картами + [ForeignKey("AccountId")] + public virtual List Cards { get; set; } = new(); + + public static Account Create(BankDatabase context, AccountBindingModel model) + { + return new Account() + { + Id = model.Id, + ClientId = model.ClientId, + Client = context.Clients.First(x => x.Id == model.ClientId), + Balance = model.Balance, + DateOpen = model.DateOpen, + CashierId = model.CashierId, + AccountNumber = model.AccountNumber, + StatusAccount = model.StatusAccount + }; + } + + public void Update(AccountBindingModel model) + { + Balance = model.Balance; + } + + public AccountViewModel GetViewModel => new() + { + Id = Id, + CashierId = CashierId, + ClientId = ClientId, + Name = Client.Name, + Patronymic = Client.Patronymic, + AccountNumber = AccountNumber, + Balance = Balance, + DateOpen = DateOpen, + StatusAccount = StatusAccount + }; } } diff --git a/Bank/BankDatabaseImplement/Models/ClientModels/Card.cs b/Bank/BankDatabaseImplement/Models/ClientModels/Card.cs index f0e742a..5df923b 100644 --- a/Bank/BankDatabaseImplement/Models/ClientModels/Card.cs +++ b/Bank/BankDatabaseImplement/Models/ClientModels/Card.cs @@ -38,7 +38,7 @@ namespace BankDatabaseImplement.Models.ClientModels public DateTime Period { get; set; } = DateTime.Now; [Required] - public StatusCard StatusCard { get; set; } = StatusCard.Закрыта; + public StatusCard StatusCard { get; set; } = StatusCard.Открыта; [Required] public string? ClientSurname { get; set; }