Skip to content

Commit

Permalink
Update transactions_controller.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Sep 20, 2024
1 parent 42e981f commit f386dc3
Showing 1 changed file with 34 additions and 35 deletions.
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

0 comments on commit f386dc3

Please sign in to comment.