From 2016514ca0ec51ce724b8b28968ac81bc2685565 Mon Sep 17 00:00:00 2001 From: Zakharov_Rostislav Date: Sun, 28 Apr 2024 13:03:17 +0400 Subject: [PATCH] add models in Database project --- .../BindingModels/WithdrawalBindingModel.cs | 2 + .../ViewModels/AccountViewModel.cs | 2 + .../ViewModels/WithdrawalViewModel.cs | 2 +- Bank/BankDataModels/Models/IRequestModel.cs | 1 - Bank/BankDatabaseImplement/BankDatabase.cs | 7 +- Bank/BankDatabaseImplement/Models/Account.cs | 67 +++++++++++++++ .../Models/AccountWithdrawal.cs | 20 +++++ Bank/BankDatabaseImplement/Models/Manager.cs | 63 ++++++++++++++ Bank/BankDatabaseImplement/Models/Transfer.cs | 65 ++++++++++++++ .../Models/Withdrawal.cs | 86 +++++++++++++++++++ 10 files changed, 312 insertions(+), 3 deletions(-) create mode 100644 Bank/BankDatabaseImplement/Models/Account.cs create mode 100644 Bank/BankDatabaseImplement/Models/AccountWithdrawal.cs create mode 100644 Bank/BankDatabaseImplement/Models/Manager.cs create mode 100644 Bank/BankDatabaseImplement/Models/Transfer.cs create mode 100644 Bank/BankDatabaseImplement/Models/Withdrawal.cs diff --git a/Bank/BankContracts/BindingModels/WithdrawalBindingModel.cs b/Bank/BankContracts/BindingModels/WithdrawalBindingModel.cs index ca6b48c..076bda7 100644 --- a/Bank/BankContracts/BindingModels/WithdrawalBindingModel.cs +++ b/Bank/BankContracts/BindingModels/WithdrawalBindingModel.cs @@ -12,5 +12,7 @@ namespace BankContracts.BindingModels public int Id { get; set; } public DateTime WithdrawalTime { get; set; } = DateTime.Now; public int? RequestId { get; set; } + + public Dictionary WithdrawalAccounts = new Dictionary(); } } diff --git a/Bank/BankContracts/ViewModels/AccountViewModel.cs b/Bank/BankContracts/ViewModels/AccountViewModel.cs index fafb0a9..3cb1db1 100644 --- a/Bank/BankContracts/ViewModels/AccountViewModel.cs +++ b/Bank/BankContracts/ViewModels/AccountViewModel.cs @@ -17,6 +17,8 @@ namespace BankContracts.ViewModels public int Money { get; set; } [DisplayName("Дата открытия")] public DateOnly ReleaseDate { get; set; } = DateOnly.FromDateTime(DateTime.Now); + [DisplayName("Менеджер")] + public string ManagerName { get; set; } = string.Empty; public int ManagerId { get; set; } } } diff --git a/Bank/BankContracts/ViewModels/WithdrawalViewModel.cs b/Bank/BankContracts/ViewModels/WithdrawalViewModel.cs index 8f9b26c..bfd8462 100644 --- a/Bank/BankContracts/ViewModels/WithdrawalViewModel.cs +++ b/Bank/BankContracts/ViewModels/WithdrawalViewModel.cs @@ -16,5 +16,5 @@ namespace BankContracts.ViewModels public DateTime WithdrawalTime { get; set; } = DateTime.Now; [DisplayName("Номер заявки")] public int? RequestId { get; set; } - } + public Dictionary WithdrawalAccounts = new Dictionary(); } diff --git a/Bank/BankDataModels/Models/IRequestModel.cs b/Bank/BankDataModels/Models/IRequestModel.cs index e4dd12b..070c34c 100644 --- a/Bank/BankDataModels/Models/IRequestModel.cs +++ b/Bank/BankDataModels/Models/IRequestModel.cs @@ -12,6 +12,5 @@ namespace BankDataModels.Models DateTime RequestTime { get; set; } RequestStatus Status { get; set; } Dictionary CardRequests { get; } - } } diff --git a/Bank/BankDatabaseImplement/BankDatabase.cs b/Bank/BankDatabaseImplement/BankDatabase.cs index bee8ae3..b9cfb0a 100644 --- a/Bank/BankDatabaseImplement/BankDatabase.cs +++ b/Bank/BankDatabaseImplement/BankDatabase.cs @@ -20,5 +20,10 @@ namespace BankDatabaseImplement public virtual DbSet Operations { get; set; } public virtual DbSet Requests { get; set; } public virtual DbSet CardRequests { get; set; } - } + public virtual DbSet Managers { get; set; } + public virtual DbSet Accounts { get; set; } + public virtual DbSet Transfers { get; set; } + public virtual DbSet Withdrawals { get; set; } + public virtual DbSet AccountWithdrawals { get; set; } + } } \ No newline at end of file diff --git a/Bank/BankDatabaseImplement/Models/Account.cs b/Bank/BankDatabaseImplement/Models/Account.cs new file mode 100644 index 0000000..2a6bec8 --- /dev/null +++ b/Bank/BankDatabaseImplement/Models/Account.cs @@ -0,0 +1,67 @@ +using BankContracts.BindingModels; +using BankContracts.ViewModels; +using BankDataModels.Models; +using Microsoft.Identity.Client; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Net.NetworkInformation; +using System.Text; +using System.Threading.Tasks; + +namespace BankDatabaseImplement.Models +{ + public class Account : IAccountModel + { + public int Id { get; set; } + [Required] + public string Number { get; set; } = string.Empty; + [Required] + public int Money { get; set; } + [Required] + public DateOnly ReleaseDate { get; set; } + [Required] + public int ManagerId { get; set; } + public virtual Manager? Manager { get; private set; } + [ForeignKey("AccountId")] + public virtual List AccountWithdrawals { get; set; } = new(); + [ForeignKey("SenderAccountId")] + public virtual List SenderTransfers { get; set; } = new(); + [ForeignKey("RecipientAccountId")] + public virtual List RecipientTransfers { get; set; } = new(); + + public static Account? Create(AccountBindingModel model) + { + if (model == null) return null; + return new Account + { + Id = model.Id, + Number = model.Number, + ReleaseDate = model.ReleaseDate, + ManagerId = model.ManagerId, + Money = model.Money, + }; + } + + public void Update(AccountBindingModel model) + { + Id = model.Id; + Number = model.Number; + ReleaseDate = model.ReleaseDate; + ManagerId = model.ManagerId; + Money = model.Money; + } + + public AccountViewModel GetViewModel => new() + { + Id = Id, + Number = Number, + ReleaseDate = ReleaseDate, + ManagerId = ManagerId, + Money = Money, + ManagerName = Manager?.Fio ?? string.Empty, + }; + } +} diff --git a/Bank/BankDatabaseImplement/Models/AccountWithdrawal.cs b/Bank/BankDatabaseImplement/Models/AccountWithdrawal.cs new file mode 100644 index 0000000..448aaee --- /dev/null +++ b/Bank/BankDatabaseImplement/Models/AccountWithdrawal.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BankDatabaseImplement.Models +{ + public class AccountWithdrawal + { + public int Id { get; set; } + [Required] + public int AccountId { get; set; } + [Required] + public int WithdrawalId { get; set; } + public virtual Account Account { get; set; } = new(); + public virtual Withdrawal Withdrawal { get; set; } = new(); + } +} diff --git a/Bank/BankDatabaseImplement/Models/Manager.cs b/Bank/BankDatabaseImplement/Models/Manager.cs new file mode 100644 index 0000000..9eef20f --- /dev/null +++ b/Bank/BankDatabaseImplement/Models/Manager.cs @@ -0,0 +1,63 @@ +using BankContracts.BindingModels; +using BankContracts.ViewModels; +using BankDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BankDatabaseImplement.Models +{ + public class Manager : IManagerModel + { + public int Id { get; set; } + [Required] + public string Fio { get; set; } = string.Empty; + [Required] + public string Email { get; set; } = string.Empty; + [Required] + public string Password { get; set; } = string.Empty; + [ForeignKey("ManagerId")] + public virtual List Accounts { get; set; } = new(); + public static Manager? Create(ManagerBindingModel model) + { + if (model == null) + return null; + return new Manager + { + Id = model.Id, + Fio = model.Fio, + Email = model.Email, + Password = model.Password, + }; + } + public static Manager Create(ManagerViewModel model) + { + return new Manager() + { + Id = model.Id, + Fio = model.Fio, + Email = model.Email, + Password = model.Password, + }; + } + public void Update(ManagerBindingModel model) + { + if (model == null) + return; + Fio = model.Fio; + Email = model.Email; + Password = model.Password; + } + public ManagerViewModel GetViewModel => new() + { + Id = Id, + Fio = Fio, + Email = Email, + Password = Password, + }; + } +} diff --git a/Bank/BankDatabaseImplement/Models/Transfer.cs b/Bank/BankDatabaseImplement/Models/Transfer.cs new file mode 100644 index 0000000..33c71be --- /dev/null +++ b/Bank/BankDatabaseImplement/Models/Transfer.cs @@ -0,0 +1,65 @@ +using BankContracts.BindingModels; +using BankContracts.ViewModels; +using BankDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BankDatabaseImplement.Models +{ + public class Transfer : ITransferModel + { + public int Id { get; set; } + [Required] + public int Sum { get; set; } + [Required] + public DateTime TransferTime { get; set; } + [Required] + public int? OperationId { get; set; } = null; + public virtual Operation? Operation { get; set; } = null; + [Required] + public int SenderAccountId { get; set; } + public virtual Account? SenderAccount { get; set; } = null; + [Required] + public int RecipientAccountId { get; set; } + public virtual Account? RecipientAccount { get; set; } = null; + + public static Transfer? Create(TransferBindingModel model) + { + if (model == null) return null; + return new Transfer + { + Id = model.Id, + Sum = model.Sum, + TransferTime = model.TransferTime, + SenderAccountId = model.SenderAccountId, + RecipientAccountId = model.RecipientAccountId, + }; + } + + public void Update(TransferBindingModel model) + { + if (model == null) return; + Sum = model.Sum; + TransferTime = model.TransferTime; + OperationId = model.OperationId; + SenderAccountId = model.SenderAccountId; + RecipientAccountId = model.RecipientAccountId; + } + + public TransferViewModel GetViewModel => new() + { + Id = Id, + Sum = Sum, + TransferTime = TransferTime, + SenderAccountId = SenderAccountId, + RecipientAccountId = RecipientAccountId, + OperationId = OperationId, + SenderAccountNumber = SenderAccount?.Number ?? string.Empty, + RecipientAccountNumber = RecipientAccount?.Number ?? string.Empty, + }; + } +} diff --git a/Bank/BankDatabaseImplement/Models/Withdrawal.cs b/Bank/BankDatabaseImplement/Models/Withdrawal.cs new file mode 100644 index 0000000..7641285 --- /dev/null +++ b/Bank/BankDatabaseImplement/Models/Withdrawal.cs @@ -0,0 +1,86 @@ +using BankContracts.BindingModels; +using BankContracts.ViewModels; +using BankDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BankDatabaseImplement.Models +{ + public class Withdrawal : IWithdrawalModel + { + public int Id { get; set; } + [Required] + public DateTime WithdrawalTime { get; set; } + [Required] + public int? RequestId { get; set; } = null; + public virtual Request? Request { get; set; } = null; + [ForeignKey("WithdrawalId")] + public virtual List Accounts { get; set; } = new(); + private Dictionary? _withdrawalAccounts { get; set; } = null; + [NotMapped] + public Dictionary WithdrawalAccounts + { + get + { + if (_withdrawalAccounts == null) + { + _withdrawalAccounts = Accounts.ToDictionary(x => x.AccountId, x => x.Account as IAccountModel); + } + return _withdrawalAccounts; + } + } + + public static Withdrawal Create(BankDatabase context, WithdrawalBindingModel model) + { + return new Withdrawal + { + Id = model.Id, + WithdrawalTime = model.WithdrawalTime, + Accounts = model.WithdrawalAccounts.Select(x => new AccountWithdrawal + { + Account = context.Accounts.First(y => y.Id == x.Key) + }).ToList() + }; + } + + public void Update(WithdrawalBindingModel model) + { + WithdrawalTime = model.WithdrawalTime; + RequestId = model.RequestId; + } + + public WithdrawalViewModel GetViewModel => new() + { + Id = Id, + WithdrawalTime = WithdrawalTime, + WithdrawalAccounts = WithdrawalAccounts, + RequestId = RequestId + }; + + public void UpdateAccounts(BankDatabase context, WithdrawalBindingModel model) + { + var WithdrawalAccounts = context.AccountWithdrawals.Where(rec => rec.WithdrawalId == model.Id).ToList(); + if (WithdrawalAccounts != null && WithdrawalAccounts.Count > 0) + { + context.AccountWithdrawals.RemoveRange(WithdrawalAccounts.Where(rec => !model.WithdrawalAccounts.ContainsKey(rec.AccountId))); + context.SaveChanges(); + } + var Withdrawal = context.Withdrawals.First(x => x.Id == Id); + foreach (var account in model.WithdrawalAccounts) + { + context.AccountWithdrawals.Add(new AccountWithdrawal + { + Withdrawal = Withdrawal, + Account = context.Accounts.First(x => x.Id == account.Key), + }); + context.SaveChanges(); + } + _withdrawalAccounts = null; + } + } +}