app/Http/Controllers/Api/TransactionController.php
/**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
Store a newly created resource in storage.
Store a newly created resource in storage .
public function store(Request $request)
    {   
        $account = Account::where('account_number', '=' , $request->input('account_number'))->first();
        $transactionType = TransactionType::find($request->input('transaction_type_id'));
        if($transactionType){
            if($transactionType->name == 'Deposit'){
                $accountBalance = $account->account_balance + $request->input('transaction_amount');
            }
            elseif($transactionType->name = 'Withdrawal'){
                $accountBalance = $account->account_balance - $request->input('transaction_amount');
            }
        }
        else{
            $response = [
                'success' => false,
                'message' => 'Transaction Type Incorrect'
            ];
            Log::error('Incorrect transaction type provided: '. $request->input('transaction_type_id'));
            return response()->json($response);
        }
        $transaction = Transaction::create([
            'account_number' => $request->input('account_number'),
            'transaction_amount' => $request->input('transaction_amount'),
            'transaction_type_id' => $request->input('transaction_type_id'),
            'account_balance' => $accountBalance,
            'created_at' => date('Y-m-d H:i:s'),
            'created_by' => 1,
            'updated_by' => 1,
        ]);
        if($transaction){
            $accountRecord = Account::find($account->id);
            $accountRecord->account_balance = $accountBalance;
            $saved = $accountRecord->save();
            if($saved === false){
                Log::error('Unable to update account balance');
            }
            $response = [
                    'success' => true,
                    'message' => 'Transaction Successful'
                ];
        }else{
            $response = [
                'success' => false,
                'message' => 'Transaction fail'
            ];
        }
        return response()->json($response);
    }
public function store ( Request $ request ) { $ account = Account :: where ( 'account_number' , '=' , $ request -> input ( 'account_number' ) ) -> first ( ) ; $ transactionType = TransactionType :: find ( $ request -> input ( 'transaction_type_id' ) ) ; if ( $ transactionType ) { if ( $ transactionType -> name == 'Deposit' ) { $ accountBalance = $ account -> account_balance + $ request -> input ( 'transaction_amount' ) ; } elseif ( $ transactionType -> name = 'Withdrawal' ) { $ accountBalance = $ account -> account_balance - $ request -> input ( 'transaction_amount' ) ; } } else { $ response = [ 'success' => false , 'message' => 'Transaction Type Incorrect' ] ; Log :: error ( 'Incorrect transaction type provided: ' . $ request -> input ( 'transaction_type_id' ) ) ; return response ( ) -> json ( $ response ) ; } $ transaction = Transaction :: create ( [ 'account_number' => $ request -> input ( 'account_number' ) , 'transaction_amount' => $ request -> input ( 'transaction_amount' ) , 'transaction_type_id' => $ request -> input ( 'transaction_type_id' ) , 'account_balance' => $ accountBalance , 'created_at' => date ( 'Y-m-d H:i:s' ) , 'created_by' => 1 , 'updated_by' => 1 , ] ) ; if ( $ transaction ) { $ accountRecord = Account :: find ( $ account -> id ) ; $ accountRecord -> account_balance = $ accountBalance ; $ saved = $ accountRecord -> save ( ) ; if ( $ saved === false ) { Log :: error ( 'Unable to update account balance' ) ; } $ response = [ 'success' => true , 'message' => 'Transaction Successful' ] ; } else { $ response = [ 'success' => false , 'message' => 'Transaction fail' ] ; } return response ( ) -> json ( $ response ) ; }
Store a newly created resource in storage.
Store a newly created resource in storage .