From 09883a571399f889ce43e55b09e8a482fbd6282f Mon Sep 17 00:00:00 2001 From: Melony Franchini Date: Sun, 29 Oct 2023 17:20:11 -0600 Subject: [PATCH] refactor: UserLoansController#create and #update --- .../api/v1/users/loans_controller.rb | 23 ++----------------- 1 file changed, 2 insertions(+), 21 deletions(-) diff --git a/app/controllers/api/v1/users/loans_controller.rb b/app/controllers/api/v1/users/loans_controller.rb index e318e8a..1c351b1 100644 --- a/app/controllers/api/v1/users/loans_controller.rb +++ b/app/controllers/api/v1/users/loans_controller.rb @@ -11,27 +11,8 @@ def create end def update - # loan = Loan.find(params[:loan_id]) - - if params[:action_type] == 'accept' - @loan.update(status: 1) - @loan.puzzle.update(status: 2) - render json: LoanSerializer.new(@loan), status: 200 - elsif params[:action_type] == 'withdraw' - @loan.update(status: 2) - @loan.puzzle.update(status: 0) - render json: LoanSerializer.new(@loan), status: 200 - elsif params[:action_type] == 'deny' - @loan.update(status: 2) - @loan.puzzle.update(status: 2) - render json: LoanSerializer.new(@loan), status: 200 - elsif params[:action_type] == 'close' - @loan.update(status: 3) - @loan.puzzle.update(status: 0) - render json: LoanSerializer.new(@loan), status: 200 - else - render json: { error: "Unable to update loan status" }, status: 422 - end + updated_loan = @loan.update_status_and_puzzle_status(params[:action_type]) + render json: LoanSerializer.new(updated_loan), status: 200 end private