Sha256: 463e49d537136901524280203078665e1070779ec78e5b1553862f47e58dbd2b

Contents?: true

Size: 1.63 KB

Versions: 1

Compression:

Stored size: 1.63 KB

Contents

class AccountsController < ApplicationController
  def index
    @accounts = ACCOUNTS
  end

  def deposit_form
    @account = find_account_by_name params[:id]
    @deposit = Account::DepositCommand.new
  end
  
  def deposit
    @account = find_account_by_name params[:id]
    @deposit = @account.deposit params[:deposit]
    
    if @deposit.success?
      redirect_to root_path, :notice => "Deposited #{@deposit.amount} to #{@account.name}'s account."
    else
      render "deposit_form"
    end
  end
  
  def withdraw_form
    @account = find_account_by_name params[:id]
    @withdraw = Account::WithdrawCommand.new
  end  
  
  def withdraw
    @account = find_account_by_name params[:id]
    @withdraw = @account.withdraw params[:withdraw]
    
    if @withdraw.success?
      redirect_to root_path, :notice => "Withdrew #{@withdraw.amount} from #{@account.name}'s account."
    else
      render "withdraw_form"
    end  
  end

  def transfer_form
    @accounts = ACCOUNTS
    @transfer = Account::TransferCommand.new
  end
  
  def transfer
    @accounts = ACCOUNTS
    @transfer = Account::TransferCommand.new
    @transfer.to = find_account_by_name params[:transfer][:to]
    @transfer.from = find_account_by_name params[:transfer][:from]
    @transfer.amount = params[:transfer][:amount]
    Account.transfer @transfer
    
    if @transfer.success?
      redirect_to root_path, :notice => "Transferred #{@transfer.amount} from #{@transfer.from.name}'s account to #{@transfer.to.name}'s account."
    else
      render "transfer_form"
    end
  end
  
  private
    def find_account_by_name(name)
      ACCOUNTS.find { |a| a.name == name }
    end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
command_model-1.0.0 examples/bank/app/controllers/accounts_controller.rb