From f386dc3ebeef976ae3987a5234d59b6637bbe71f Mon Sep 17 00:00:00 2001 From: KOSASIH Date: Fri, 20 Sep 2024 17:14:25 +0700 Subject: [PATCH] Update transactions_controller.rb --- .../api/v1/transactions_controller.rb | 69 +++++++++---------- 1 file changed, 34 insertions(+), 35 deletions(-) diff --git a/global_business/business/controllers/api/v1/transactions_controller.rb b/global_business/business/controllers/api/v1/transactions_controller.rb index d891804b8..1c6e0323d 100644 --- a/global_business/business/controllers/api/v1/transactions_controller.rb +++ b/global_business/business/controllers/api/v1/transactions_controller.rb @@ -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