add models in Database project
This commit is contained in:
parent
d9cd0fb9d2
commit
2016514ca0
@ -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<int, IAccountModel> WithdrawalAccounts = new Dictionary<int, IAccountModel>();
|
||||
}
|
||||
}
|
||||
|
@ -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; }
|
||||
}
|
||||
}
|
||||
|
@ -16,5 +16,5 @@ namespace BankContracts.ViewModels
|
||||
public DateTime WithdrawalTime { get; set; } = DateTime.Now;
|
||||
[DisplayName("Номер заявки")]
|
||||
public int? RequestId { get; set; }
|
||||
}
|
||||
public Dictionary<int, IAccountModel> WithdrawalAccounts = new Dictionary<int, IAccountModel>();
|
||||
}
|
||||
|
@ -12,6 +12,5 @@ namespace BankDataModels.Models
|
||||
DateTime RequestTime { get; set; }
|
||||
RequestStatus Status { get; set; }
|
||||
Dictionary<int, ICardModel> CardRequests { get; }
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -20,5 +20,10 @@ namespace BankDatabaseImplement
|
||||
public virtual DbSet<Operation> Operations { get; set; }
|
||||
public virtual DbSet<Request> Requests { get; set; }
|
||||
public virtual DbSet<CardRequest> CardRequests { get; set; }
|
||||
}
|
||||
public virtual DbSet<Manager> Managers { get; set; }
|
||||
public virtual DbSet<Account> Accounts { get; set; }
|
||||
public virtual DbSet<Transfer> Transfers { get; set; }
|
||||
public virtual DbSet<Withdrawal> Withdrawals { get; set; }
|
||||
public virtual DbSet<AccountWithdrawal> AccountWithdrawals { get; set; }
|
||||
}
|
||||
}
|
67
Bank/BankDatabaseImplement/Models/Account.cs
Normal file
67
Bank/BankDatabaseImplement/Models/Account.cs
Normal file
@ -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<AccountWithdrawal> AccountWithdrawals { get; set; } = new();
|
||||
[ForeignKey("SenderAccountId")]
|
||||
public virtual List<Transfer> SenderTransfers { get; set; } = new();
|
||||
[ForeignKey("RecipientAccountId")]
|
||||
public virtual List<Transfer> 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,
|
||||
};
|
||||
}
|
||||
}
|
20
Bank/BankDatabaseImplement/Models/AccountWithdrawal.cs
Normal file
20
Bank/BankDatabaseImplement/Models/AccountWithdrawal.cs
Normal file
@ -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();
|
||||
}
|
||||
}
|
63
Bank/BankDatabaseImplement/Models/Manager.cs
Normal file
63
Bank/BankDatabaseImplement/Models/Manager.cs
Normal file
@ -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<Account> 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,
|
||||
};
|
||||
}
|
||||
}
|
65
Bank/BankDatabaseImplement/Models/Transfer.cs
Normal file
65
Bank/BankDatabaseImplement/Models/Transfer.cs
Normal file
@ -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,
|
||||
};
|
||||
}
|
||||
}
|
86
Bank/BankDatabaseImplement/Models/Withdrawal.cs
Normal file
86
Bank/BankDatabaseImplement/Models/Withdrawal.cs
Normal file
@ -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<AccountWithdrawal> Accounts { get; set; } = new();
|
||||
private Dictionary<int, IAccountModel>? _withdrawalAccounts { get; set; } = null;
|
||||
[NotMapped]
|
||||
public Dictionary<int, IAccountModel> 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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user