Esempio n. 1
0
        public async Task <IActionResult> DepositMoney([FromBody] string data)
        {
            var model = JsonConvert.DeserializeObject <DepositMoneyModel>(data);

            if (!TryValidateModel(model))
            {
                return(BadRequest());
            }

            var account =
                await _accountService.GetAccountByCardNumberAsync(model.CardNumber);

            if (account == null)
            {
                return(BadRequest());
            }

            var serviceModel = new DepositMoneyServiceModel
            {
                Account = account,
                Amount  = model.Amount,
            };

            var isSuccessful = await _transactionService.DepositMoneyByCardAsync(serviceModel);

            if (!isSuccessful)
            {
                return(NoContent());
            }

            return(Ok());
        }
        public async Task <bool> DepositMoneyByCardAsync(DepositMoneyServiceModel model)
        {
            using (var transaction = await _transactionRepository.BeginTransaction())
            {
                try
                {
                    var transfer = new Transaction
                    {
                        Id            = new Guid(),
                        FromAccountId = null,
                        ToAccountId   = model.Account.Id,
                        ExchangeRate  = 1,
                        Amount        = model.Amount,
                        CardId        = null,
                        Date          = DateTime.Now,
                        Description   = "Deposit Money",
                        StatusId      = 1,
                        FromCurrency  = model.Account.Currency.Code,
                        ToCurrency    = model.Account.Currency.Code,
                        Number        = _transactionHelper.Number(),
                        Destination   = model.Account.Number
                    };

                    model.Account.Balance += model.Amount;
                    await _transactionRepository.Add(transfer);

                    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   = "Deposit Money",
                        StatusId      = 2,
                        FromCurrency  = model.Account.Currency.Code,
                        ToCurrency    = model.Account.Currency.Code,
                        Number        = _transactionHelper.Number(),
                        Destination   = model.Account.Number
                    });

                    return(false);
                }
            }

            return(true);
        }