public async Task <IActionResult> WithdrawMoneyByCard([FromBody] string data) { var model = JsonConvert.DeserializeObject <WithdrawMoneyModel>(data); if (!TryValidateModel(model)) { return(BadRequest()); } var account = await _accountService.GetAccountByCardNumberAsync(model.CardNumber); if (account == null || account.Cards.Find(x => x.Number == model.CardNumber).PinCode != _cryptoHelper.Hash(model.PinCode)) { return(BadRequest()); } var serviceModel = new WithdrawMoneyServiceModel { Account = account, Amount = model.Amount, }; var isSuccessful = await _transactionService.WithdrawMoneyByCardAsync(serviceModel); if (!isSuccessful) { return(NoContent()); } return(Ok()); }
public async Task <bool> WithdrawMoneyByCardAsync(WithdrawMoneyServiceModel model) { using (var transaction = await _transactionRepository.BeginTransaction()) { try { var withdrawTransfer = new Transaction { Id = new Guid(), FromAccountId = null, ToAccountId = model.Account.Id, ExchangeRate = 1, Amount = model.Amount, CardId = null, Date = DateTime.Now, Description = "Withdraw Money", StatusId = 1, FromCurrency = model.Account.Currency.Code, ToCurrency = model.Account.Currency.Code, Number = _transactionHelper.Number(), Destination = model.Account.Number }; if ((model.Account.Balance - model.Amount) < 0) { withdrawTransfer.StatusId = 2; await _transactionRepository.Add(withdrawTransfer); } else { model.Account.Balance -= model.Amount; await _transactionRepository.Add(withdrawTransfer); await _accountRepository.Update(); } transaction.Commit(); } catch (Exception) { transaction.Rollback(); await _transactionRepository.Add(new Transaction { Id = new Guid(), FromAccountId = null, ToAccountId = model.Account.Id, ExchangeRate = 1, Amount = model.Amount, CardId = null, Date = DateTime.Now, Description = "Withdraw Money", StatusId = 2, FromCurrency = model.Account.Currency.Code, ToCurrency = model.Account.Currency.Code, Number = _transactionHelper.Number(), Destination = model.Account.Number }); return(false); } } return(true); }