-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
34 additions
and
35 deletions.
There are no files selected for viewing
69 changes: 34 additions & 35 deletions
69
global_business/business/controllers/api/v1/transactions_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,43 @@ | ||
module Api | ||
module V1 | ||
class TransactionsController < ApplicationController | ||
before_action :authenticate_user! | ||
before_action :set_transaction, only: [:show, :update, :destroy] | ||
class Api::V1::TransactionsController < ApplicationController | ||
before_action :authenticate_user! | ||
|
||
def create | ||
@transaction = Transaction.new(transaction_params) | ||
if @transaction.save | ||
render json: @transaction, status: :created | ||
else | ||
render json: { errors: @transaction.errors }, status: :unprocessable_entity | ||
end | ||
end | ||
def index | ||
transactions = current_user.accounts.find(params[:account_id]).transactions | ||
render json: transactions, status: :ok | ||
end | ||
|
||
def show | ||
render json: @transaction | ||
end | ||
def show | ||
transaction = current_user.accounts.find(params[:account_id]).transactions.find(params[:id]) | ||
render json: transaction, status: :ok | ||
end | ||
|
||
def update | ||
if @transaction.update(transaction_params) | ||
render json: @transaction | ||
else | ||
render json: { errors: @transaction.errors }, status: :unprocessable_entity | ||
end | ||
end | ||
def create | ||
transaction = current_user.accounts.find(params[:account_id]).transactions.new(transaction_params) | ||
if transaction.save | ||
render json: transaction, status: :created | ||
else | ||
render json: { errors: transaction.errors }, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
def destroy | ||
@transaction.destroy | ||
render json: { message: "Transaction deleted successfully" }, status: :ok | ||
end | ||
def update | ||
transaction = current_user.accounts.find(params[:account_id]).transactions.find(params[:id]) | ||
if transaction.update(transaction_params) | ||
render json: transaction, status: :ok | ||
else | ||
render json: { errors: transaction.errors }, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
private | ||
def destroy | ||
transaction = current_user.accounts.find(params[:account_id]).transactions.find(params[:id]) | ||
transaction.destroy | ||
render json: {}, status: :no_content | ||
end | ||
|
||
def transaction_params | ||
params.require(:transaction).permit(:amount, :transaction_type, :account_id) | ||
end | ||
private | ||
|
||
def set_transaction | ||
@transaction = Transaction.find(params[:id]) | ||
end | ||
end | ||
def transaction_params | ||
params.require(:transaction).permit(:amount, :type) | ||
end | ||
end |