Example #1
0
        public async Task <FundAccountResponse> FundCustomerAsync(FundAccountViewModel model)
        {
            try
            {
                if (model == null)
                {
                    throw new NullReferenceException("Fund Account Model is Null");
                }

                //Amount and Acct Numb
                //check if account number already exists
                var result = _context.CustomerAccounts.FirstOrDefault(z => z.AccountNumber == model.AccountNumber);

                if (result != null) //if account exist
                {
                    //increment Balance on CustAcct..get cust by acct num
                    var balance = _context.CustomerAccounts.FirstOrDefault(x => x.CustomerAccountId == result.CustomerAccountId).Balance;
                    balance += model.Amount;

                    result.Balance = balance;

                    _context.Entry(result).State = EntityState.Modified;
                    await _context.SaveChangesAsync();

                    // _context.Update(cust.Balance);
                    //await _context.SaveChangesAsync();

                    //Enter value to Transaction table
                    var txn = new Transaction();
                    txn.Amount            = model.Amount;
                    txn.Type              = model.TransactionType;
                    txn.CustomerAccountId = result.CustomerAccountId.ToString();
                    txn.AccountNumber     = result.AccountNumber;
                    txn.CreatedOn         = DateTime.Now;

                    await _context.AddAsync(txn);

                    await _context.SaveChangesAsync();
                }
                else
                {
                    throw new NullReferenceException("Error Occurred.");
                }

                return(new FundAccountResponse
                {
                    Message = "Account funded successfully",
                    IsSuccess = true
                });
            }
            catch (Exception ex)
            {
                throw new NullReferenceException(ex.Message);
            }
        }
        public async Task <IActionResult> FundAccountAsync([FromBody] FundAccountViewModel model)
        {
            if (ModelState.IsValid)
            {
                var result = await _customerService.FundCustomerAsync(model);

                if (result.IsSuccess)
                {
                    return(Ok(result)); //Status Code : 200
                }
                return(BadRequest(result));
            }

            return(BadRequest("Some properties are not valid")); //return code:400
        }