Compare commits

..

No commits in common. "42c893e1051cdc9dc115af0edb7485745124d51d" and "ca859d9bc5364505164ef1abee262926209f4688" have entirely different histories.

3 changed files with 79 additions and 105 deletions

View File

@ -19,6 +19,7 @@ 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,6 +95,47 @@ 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,119 +56,51 @@ namespace BankDatabaseImplement.Implements
.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? 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? Update(TransferBindingModel model)
public TransferViewModel? Update(TransferBindingModel model)
{
using var context = new BankDatabase();
var oldTransfer = context.Transfers.FirstOrDefault(x => x.Id == model.Id);
if (oldTransfer == null)
return null;
using var transaction = context.Database.BeginTransaction();
try
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)
{
RejectTransfer(context, new TransferBindingModel
{
RecipientAccountId = oldTransfer.RecipientAccountId,
SenderAccountId = oldTransfer.SenderAccountId,
Sum = oldTransfer.Sum,
});
MakeTransfer(context, model);
oldTransfer.Update(model);
}
catch
{
transaction.Rollback();
throw;
}
return null;
}
operation.Update(model);
context.SaveChanges();
transaction.Commit();
return oldTransfer.GetViewModel;
return operation.GetViewModel;
}
public TransferViewModel? Delete(TransferBindingModel model)
{
using var context = new BankDatabase();
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,
});
}
}
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;
}
}
}