예제 #1
0
        public async Task <IActionResult> DebitAsync(string userId, string accountType, int amount)
        {
            try
            {
                //Input validations

                if (string.IsNullOrEmpty(userId) ||
                    string.IsNullOrEmpty(accountType))
                {
                    //400
                    return(BadRequest());
                }

                if (!_validations.ValidateAccountType(accountType))
                {
                    //400
                    return(BadRequest());
                }

                if (!_validations.ValidateAmount(amount))
                {
                    //400
                    return(BadRequest());
                }


                var response = await _bankService.DebitAsync(userId, accountType, amount);

                if (response.Success)
                {
                    //200
                    return(Ok(response.Balance));
                }
                else if (response.InvalidAccount)
                {
                    //404
                    return(NotFound());
                }
                else
                {
                    //insufficient Funds
                    return(StatusCode(403, "Insufficient funds."));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                //500
                return(StatusCode(Microsoft.AspNetCore.Http.StatusCodes.Status500InternalServerError, "Server Error."));
            }
        }