This commit is contained in:
Artyom_Yashin 2024-05-26 19:34:05 +04:00
commit 3906d70eca
3 changed files with 105 additions and 79 deletions

View File

@ -19,7 +19,6 @@ namespace BankContracts.StoragesContracts
AccountViewModel? GetElement(AccountSearchModel model);
AccountViewModel? Insert(AccountBindingModel model);
AccountViewModel? Update(AccountBindingModel model);
bool MakeTransfer(int recipientId, int senderId, int sum);
AccountViewModel? Delete(AccountBindingModel model);
}
}

View File

@ -95,47 +95,6 @@ namespace BankDatabaseImplement.Implements
return account.GetViewModel;
}
public bool MakeTransfer(int recipientId, int senderId, int sum)
{
using var context = new BankDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
Account? sender = context.Accounts.FirstOrDefault(x => x.Id == senderId);
if (sender == null)
throw new InvalidOperationException("Sender account was not found");
if (sender.Money < sum)
throw new InvalidOperationException("Sender account did not have enough money");
Account? recipient = context.Accounts.FirstOrDefault(x => x.Id == recipientId);
if (recipient == null)
throw new InvalidOperationException("Recipient account was not found");
sender.Update(new AccountBindingModel
{
Id = sender.Id,
Number = sender.Number,
ReleaseDate = sender.ReleaseDate,
ManagerId = sender.ManagerId,
Money = sender.Money - sum,
});
recipient.Update(new AccountBindingModel
{
Id = recipient.Id,
Number = recipient.Number,
ReleaseDate = recipient.ReleaseDate,
ManagerId = recipient.ManagerId,
Money = recipient.Money + sum,
});
context.SaveChanges();
transaction.Commit();
}
catch
{
transaction.Rollback();
throw;
}
return true;
}
public AccountViewModel? Delete(AccountBindingModel model)
{
using var context = new BankDatabase();

View File

@ -56,51 +56,119 @@ namespace BankDatabaseImplement.Implements
.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? Insert(TransferBindingModel model)
{
var newTransfer = Transfer.Create(model);
if (newTransfer == null)
return null;
using var context = new BankDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
MakeTransfer(context, model);
context.Transfers.Add(newTransfer);
}
catch
{
transaction.Rollback();
throw;
}
context.SaveChanges();
transaction.Commit();
return newTransfer.GetViewModel;
}
public TransferViewModel? Update(TransferBindingModel model)
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)
{
var oldTransfer = context.Transfers.FirstOrDefault(x => x.Id == model.Id);
if (oldTransfer == null)
return null;
}
operation.Update(model);
using var transaction = context.Database.BeginTransaction();
try
{
RejectTransfer(context, new TransferBindingModel
{
RecipientAccountId = oldTransfer.RecipientAccountId,
SenderAccountId = oldTransfer.SenderAccountId,
Sum = oldTransfer.Sum,
});
MakeTransfer(context, model);
oldTransfer.Update(model);
}
catch
{
transaction.Rollback();
throw;
}
context.SaveChanges();
return operation.GetViewModel;
transaction.Commit();
return oldTransfer.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;
}
}
var oldTransfer = context.Transfers.FirstOrDefault(rec => rec.Id == model.Id);
if (oldTransfer == null)
return null;
using var transaction = context.Database.BeginTransaction();
try
{
RejectTransfer(context, new TransferBindingModel
{
RecipientAccountId = oldTransfer.RecipientAccountId,
SenderAccountId = oldTransfer.SenderAccountId,
Sum = oldTransfer.Sum,
});
context.Transfers.Remove(oldTransfer);
}
catch
{
transaction.Rollback();
throw;
}
context.SaveChanges();
transaction.Commit();
return oldTransfer.GetViewModel;
}
private void MakeTransfer(BankDatabase context, TransferBindingModel model)
{
TransferMoney(context, model.RecipientAccountId, model.SenderAccountId, model.Sum);
}
private void RejectTransfer(BankDatabase context, TransferBindingModel model)
{
TransferMoney(context, model.SenderAccountId, model.RecipientAccountId, model.Sum);
}
private void TransferMoney(BankDatabase context, int recipientId, int senderId, int sum)
{
Account? sender = context.Accounts.FirstOrDefault(x => x.Id == senderId);
if (sender == null)
throw new InvalidOperationException("Sender account was not found");
if (sender.Money < sum)
throw new InvalidOperationException("Sender account did not have enough money");
Account? recipient = context.Accounts.FirstOrDefault(x => x.Id == recipientId);
if (recipient == null)
throw new InvalidOperationException("Recipient account was not found");
sender.Update(new AccountBindingModel
{
Id = sender.Id,
Number = sender.Number,
ReleaseDate = sender.ReleaseDate,
ManagerId = sender.ManagerId,
Money = sender.Money - sum,
});
recipient.Update(new AccountBindingModel
{
Id = recipient.Id,
Number = recipient.Number,
ReleaseDate = recipient.ReleaseDate,
ManagerId = recipient.ManagerId,
Money = recipient.Money + sum,
});
}
}
}