bank account refactor

This commit is contained in:
hydrant
2020-03-15 21:33:04 +01:00
parent 75e26b8e8a
commit 92c054c90c
30 changed files with 2088 additions and 300 deletions

View File

@@ -9,6 +9,7 @@ using ReallifeGamemode.Server.Util;
using ReallifeGamemode.Database;
using ReallifeGamemode.Database.Models;
using ReallifeGamemode.Database.Entities.Logs;
using ReallifeGamemode.Database.Entities;
/**
* @overview Life of German Reallife - Managers BankManager (BankManager.cs)
@@ -54,43 +55,63 @@ namespace ReallifeGamemode.Server.Managers
}
}
public static TransactionResult TransferMoney(IBankAccountOwner sender, IBankAccountOwner receiver, int amount, string origin)
public static TransactionResult TransferMoney<TSender, TReceiver>(
BankAccountHolder<TSender> sender,
BankAccountHolder<TReceiver> receiver,
int amount,
string origin,
DatabaseContext dbContext) where TSender : class, IBankAccount, new() where TReceiver : class, IBankAccount, new()
{
using (var transferMoney = new DatabaseContext())
if (amount < 0) return TransactionResult.NEGATIVE_MONEY_SENT;
IBankAccount senderAccount;
IBankAccount receiverAccount;
if (sender is BankAccountHolder<BusinessBankAccount> businessSender)
{
if (amount < 0) return TransactionResult.NEGATIVE_MONEY_SENT;
IBankAccount senderAccount = sender.GetBankAccount(transferMoney);
IBankAccount receiverAccount = receiver.GetBankAccount(transferMoney);
if (senderAccount == null) return TransactionResult.SENDER_NO_BANKACCOUNT;
if (receiverAccount == null) return TransactionResult.RECEIVER_NO_BANKACCOUNT;
if (senderAccount.Balance < amount) return TransactionResult.SENDER_NOT_ENOUGH_MONEY;
var transactionLog = new BankAccountTransactionHistory
{
Sender = sender.Name,
SenderBalance = senderAccount.Balance,
Receiver = receiver.Name,
ReceiverBalance = receiverAccount.Balance,
NewReceiverBalance = receiverAccount.Balance + amount,
NewSenderBalance = senderAccount.Balance - amount,
MoneySent = amount,
Fee = 0,
Origin = origin
};
// add log
transferMoney.BankAccountTransactionLogs.Add(transactionLog);
senderAccount.Balance -= amount;
receiverAccount.Balance += amount;
transferMoney.SaveChanges();
return TransactionResult.SUCCESS;
senderAccount = dbContext.BusinessBankAccounts.Where(b => b.Id == businessSender.BankAccountId).First();
}
else
{
senderAccount = sender.BankAccount;
}
if (receiver is BankAccountHolder<BusinessBankAccount> businessReceiver)
{
receiverAccount = dbContext.BusinessBankAccounts.Where(b => b.Id == businessReceiver.BankAccountId).First();
}
else
{
receiverAccount = receiver.BankAccount;
}
if (senderAccount == null) return TransactionResult.SENDER_NO_BANKACCOUNT;
if (receiverAccount == null) return TransactionResult.RECEIVER_NO_BANKACCOUNT;
if (senderAccount.Balance < amount) return TransactionResult.SENDER_NOT_ENOUGH_MONEY;
var transactionLog = new BankAccountTransactionHistory
{
Sender = sender.BankAccountName,
SenderBalance = senderAccount.Balance,
Receiver = receiver.BankAccountName,
ReceiverBalance = receiverAccount.Balance,
NewReceiverBalance = receiverAccount.Balance + amount,
NewSenderBalance = senderAccount.Balance - amount,
MoneySent = amount,
Fee = 0,
Origin = origin
};
// add log
dbContext.BankAccountTransactionLogs.Add(transactionLog);
senderAccount.Balance -= amount;
receiverAccount.Balance += amount;
dbContext.SaveChanges();
return TransactionResult.SUCCESS;
}
}
}