Skip to content

Commit

Permalink
Create transactions_controller.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Sep 20, 2024
1 parent 404c053 commit cd76ea0
Showing 1 changed file with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
module Api
module V1
class TransactionsController < ApplicationController
before_action :authenticate_user!
before_action :set_transaction, only: [:show, :update, :destroy]

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 show
render json: @transaction
end

def update
if @transaction.update(transaction_params)
render json: @transaction
else
render json: { errors: @transaction.errors }, status: :unprocessable_entity
end
end

def destroy
@transaction.destroy
render json: { message: "Transaction deleted successfully" }, status: :ok
end

private

def transaction_params
params.require(:transaction).permit(:amount, :transaction_type, :account_id)
end

def set_transaction
@transaction = Transaction.find(params[:id])
end
end
end
end

0 comments on commit cd76ea0

Please sign in to comment.