Beispiel #1
0
        public async Task <IActionResult> Withdraw(int id, decimal ammountToWithdraw)
        {
            try
            {
                _Logger.LogInformation($"Getting Term CD {id} for withdrawal.");
                // Get reference account.
                Account input = await _Context.GetAccountDetailsByAccountID(id);

                if (input == null)
                {
                    _Logger.LogWarning($"Term CD {id} not found.");
                    return(NotFound(null));
                }

                // Check if one year has passed.
                if (input.CreateDate.Subtract(DateTime.Now).TotalDays < -365)
                {
                    if (ammountToWithdraw < 0 || ammountToWithdraw > input.Balance)
                    {
                        _Logger.LogWarning("Withdraw amount specified invalid TermCDController Withdraw Action!");
                        return(BadRequest());
                    }

                    _Logger.LogInformation($"Withdrawing from Term CD {id}.");

                    if (!await _Context.Withdraw(id, ammountToWithdraw))
                    {
                        _Logger.LogWarning($"Cannot withdraw account is closed");
                        return(NotFound(id));
                    }

                    await _Context.SaveChanges();

                    return(NoContent());
                }
                else
                {
                    _Logger.LogWarning($"Term CD {id} NOT matured.");
                }

                return(BadRequest());
            }
            catch (Exception e)
            {
                _Logger.LogError(e, "Unexpected Error in TermCDController Withdraw Action!");
                return(StatusCode(500, e));
            }
        }
        public async Task <ActionResult <Account> > GetAccountDetailsByAccountID(int id)
        {
            try
            {
                Account result = null;
                _logger?.LogInformation(string.Format("Start GetAccountDetailsByAccountID: {0}", id.ToString()));
                result = await _repo?.GetAccountDetailsByAccountID(id) ?? null;

                // Check if return object was null.
                if (result == null)
                {
                    // Return NotFound 404 response if no account detail was found for ID.
                    _logger?.LogWarning(string.Format("Account #{0} details not found!", id.ToString()));
                    return(NotFound(null));
                }

                // Return account object found.
                _logger?.LogInformation(string.Format("GetAccountDetailsByAccountID: {0} Succeeded.", id.ToString()));

                return(Ok(result));
            }
            catch (Exception WTF)
            {
                // Return Internal Server Error 500 on general exception.
                _logger?.LogError(WTF, "Unexpected Error in GetAccountDetailsByAccountID!");
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }
        public async Task <ActionResult> Deposit(int id, decimal amount)
        {
            _logger?.LogInformation(string.Format("Attempting to deposit into account with id: {0}", id.ToString()));

            try
            {
                Account acctFound = null;
                if (amount < 0) //make sure deposit amount is positive
                {
                    _logger?.LogWarning(string.Format("PUT request failed, Amount passed is less than 0.  Account with ID: {0}", id.ToString()));
                    return(StatusCode(400));
                }

                acctFound = await _repoAccount.GetAccountDetailsByAccountID(id); //retrieve account in order to check if it exists, and manage balance

                if (acctFound == null)                                           //make sure account exist in database
                {
                    _logger?.LogWarning(string.Format("PUT request failed, No Account found with ID: {0} or Account is already closed", id.ToString()));
                    return(NotFound(id));
                }

                //deposit into account with id if it returns false then account is closed
                if (!await _repoAccount.Deposit(acctFound.Id, amount))
                {
                    _logger?.LogWarning(string.Format("PUT, deposit failed, Account is already closed", id.ToString()));
                    return(NotFound(id));
                }

                _logger?.LogInformation("PUT Success deposited into account with ID: {0} Amount: {2}", id.ToString(), amount.ToString());
                await _repoAccount.SaveChanges();

                return(NoContent());
            }
            catch (Exception e)
            {
                _logger?.LogError(e, "Unexpected Error in deposit of account with ID: {0}", id.ToString());
                return(StatusCode(500));
            }
        }
        public async Task <ActionResult> ProcessLoanPayment(int id, decimal amount)
        {
            try
            {
                if (amount <= 0) //make sure payment amount is positive
                {
                    _logger?.LogWarning(string.Format("LoanAccountController PUT request failed, Amount passed is less than or equal to 0.  Account with ID: {0}", id));
                    return(StatusCode(400));
                }

                var acct = await _repo.GetAccountDetailsByAccountID(id);

                if (acct == null)
                {
                    _logger?.LogWarning(string.Format("LoanAccountController PUT request failed, Account not found.  Account with ID: {0}", id));
                    return(NotFound(id));
                }
                else
                {
                    if (!await _repo.PayLoan(id, amount))
                    {
                        _logger?.LogWarning(string.Format("LoanAccountController PUT request failed, Account not is already closed.  Account with ID: {0}", id));
                        return(NotFound(id));
                    }

                    await _repo.SaveChanges();

                    return(NoContent());
                }
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Unexpected Error in LoanAccountController!");
                return(StatusCode(500));
            }
        }