diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 7b7fa25ba..c94b15a5e 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -12,7 +12,27 @@ def create json: {error: rental.errors.messages}, status: :bad_request ) end # if/else - end + end # create + + def update + rental = Rental.find_by(id: params[:id]) + if rental + rental.checked_out = false + if rental.save + render( + json: {"checked_in" => true}, status: :ok + ) + else + render( + json: {error: rental.errors.messages}, status: :bad_request + ) + end # if/else + else + render( + json: {"no rental found": true}, status: :not_found + ) + end # if rental + end # update private def rental_data diff --git a/config/routes.rb b/config/routes.rb index c8e3581b1..be900cde6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,6 +4,7 @@ resources :movies, only: [:index, :show, :create] resources :customers, only: [:index] - resources :rentals, only: [:create] + resources :rentals, only: [:create, :update] + # patch '/rentals/:id', to: "rentals#checkin", as: 'rental_checkin' end diff --git a/test/controllers/rentals_controller_test.rb b/test/controllers/rentals_controller_test.rb index 0e0ba0394..1caa9a9c9 100644 --- a/test/controllers/rentals_controller_test.rb +++ b/test/controllers/rentals_controller_test.rb @@ -71,4 +71,51 @@ body.keys.sort.must_equal keys end end # checkout -end + + describe "checkin" do + let(:r) {Rental.new(customer_id: c.id,movie_id: m.id)} + + it "will check in a movie if the movie exists" do + # arrange + start_count = Rental.count + availible = m.available_inventory + num_movies = c.movies_checked_out_count + + r = post rentals_path, params: {rental: rental_data} + + Rental.count.must_equal start_count + 1 + m.available_inventory.must_equal availible - 1 + c.movies_checked_out_count.must_equal num_movies + 1 + + r_id = Rental.last.id + + # Act + patch rental_path(r_id) + + # Assert + Rental.count.must_equal start_count + 1 + m.available_inventory.must_equal availible + c.movies_checked_out_count.must_equal num_movies + end # checkin a movie + + it "will return not_found if rental does not exist" do + # arrange + start_count = Rental.count + + r = post rentals_path, params: {rental: rental_data} + + Rental.count.must_equal start_count + 1 + + r_id = Rental.last.id + 1 + + # Act + patch rental_path(r_id) + + # Assert + must_respond_with :not_found + + body = JSON.parse(response.body) + body.must_equal "no rental found" => true + end # not_found + end # checkin +end # renals