Exemple #1
0
        public async Task <IActionResult> Debit(TransactionEntity obj)
        {
            if (obj.Account <= 0)
            {
                return(BadRequest(new ErrorViewModel {
                    Error = "Account number must be greater than 0"
                }));
            }

            if (await _transactionsRepository.FindByAccountAsync(obj.Account) == null)
            {
                return(NotFound(new ErrorViewModel {
                    Error = "Account not found"
                }));
            }

            if (!obj.IsDebit)
            {
                return(BadRequest(new ErrorViewModel {
                    Error = "Operation must be Debit"
                }));
            }

            if (obj.Value <= 0)
            {
                return(BadRequest(new ErrorViewModel {
                    Error = "Cannot debit account with value less than 0 or 0"
                }));
            }

            if (await _transactionsRepository.BalanceAsync(obj.Account) < obj.Value)
            {
                return(BadRequest(new ErrorViewModel {
                    Error = "Balance must be greater than amount to be debited"
                }));
            }

            await _transactionsRepository.InsertAsync(obj);

            return(Ok(new SuccessViewModel
            {
                Message = "Successfully debited credits",
                Value = obj.Value,
                Balance = await _transactionsRepository.BalanceAsync(obj.Account)
            }));
        }
        public async Task <IActionResult> Debit(TransactionEntity obj)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }

            try
            {
                if (obj.Account <= 0)
                {
                    throw new TransactionException("Account number must be greater than 0");
                }

                if (await _transactionsRepository.FindByAccountAsync(obj.Account) == null)
                {
                    throw new NotFoundException("Account not found");
                }

                if (!obj.IsDebit)
                {
                    throw new TransactionException("Operation must be Debit");
                }

                if (obj.Value <= 0)
                {
                    throw new TransactionException("Cannot debit account with value less than 0 or 0");
                }

                if (await _transactionsRepository.BalanceAsync(obj.Account) < obj.Value)
                {
                    throw new TransactionException("Balance must be greater than amount to be debited");
                }

                await _transactionsRepository.InsertAsync(obj);

                return(RedirectToAction(nameof(Success), new { Message = "Successfully debited credits", obj.Value, Balance = await _transactionsRepository.BalanceAsync(obj.Account) }));
            }
            catch (ApplicationException e)
            {
                return(RedirectToAction(nameof(Error), new { e.Message }));
            }
        }