From cd76ea047f71bcf5df15d8bd2fd21f899fceb1f4 Mon Sep 17 00:00:00 2001 From: KOSASIH Date: Fri, 20 Sep 2024 16:49:47 +0700 Subject: [PATCH] Create transactions_controller.rb --- .../api/v1/transactions_controller.rb | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 global_business/business/controllers/api/v1/transactions_controller.rb diff --git a/global_business/business/controllers/api/v1/transactions_controller.rb b/global_business/business/controllers/api/v1/transactions_controller.rb new file mode 100644 index 000000000..d891804b8 --- /dev/null +++ b/global_business/business/controllers/api/v1/transactions_controller.rb @@ -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