From 5198246a349e1857bb9b0393b2ec59502f390306 Mon Sep 17 00:00:00 2001 From: Rebecca Bergena Date: Tue, 7 Nov 2017 16:14:13 -0800 Subject: [PATCH] rentals controller --- app/controllers/rentals_controller.rb | 33 ++++++++++++++++++- app/models/movie.rb | 1 + app/models/rental.rb | 7 +++- test/controllers/rentals_controller_test.rb | 36 +++++++++++++++++++-- test/fixtures/movies.yml | 4 ++- 5 files changed, 76 insertions(+), 5 deletions(-) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index bd3d02f03..075b45b3a 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -6,6 +6,7 @@ def overdue def checkout rental = Rental.new(rental_params) + # if rental.movie.available_inventory if rental.save render( json: { @@ -15,19 +16,49 @@ def checkout }, status: :ok ) + rental.customer.movies_checked_out_count +=1 + rental.movie.available_inventory -= 1 + # binding.pry + else render( json: { errors: rental.errors.messages }, status: :bad_request ) end + # else + + #some error movie not available + # end + + # binding.pry + # movie has many rentals in movie movie.rentals + # rental.customer.movies_checked_out_count +=1 + # rental.customer.movies_checked_out_count + # rental.movie.inspect + # rental.movie.available_inventory -= 1 + # puts rental.customer.movies_checked_out_count end def checkin + rental = Rental.where(rental_params).first + if rental + rental.checkin_date = DateTime.now + rental.due_date = nil + rental.save + + rental.customer.movies_checked_out_count -=1 + rental.movie.available_inventory += 1 + else + render( + json: { errors: rental.errors.messages }, + status: :bad_request + ) + end end def rental_params - params.permit(:customer_id, :movie_id, :due_date) + params.permit(:customer_id, :movie_id, :due_date, :checkin_date) end end diff --git a/app/models/movie.rb b/app/models/movie.rb index 3e9a3e272..decd12ffa 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -14,4 +14,5 @@ def set_avail_inv_attribute end + end diff --git a/app/models/rental.rb b/app/models/rental.rb index b8b876915..94532fd81 100644 --- a/app/models/rental.rb +++ b/app/models/rental.rb @@ -1,6 +1,11 @@ class Rental < ApplicationRecord belongs_to :movie belongs_to :customer - + + after_create :set_checkout_date + + def set_checkout_date + self.update_columns(checkout_date: self.created_at) + end end diff --git a/test/controllers/rentals_controller_test.rb b/test/controllers/rentals_controller_test.rb index 1c5988fd2..4e967df97 100644 --- a/test/controllers/rentals_controller_test.rb +++ b/test/controllers/rentals_controller_test.rb @@ -3,7 +3,7 @@ describe RentalsController do - describe "create" do + describe "checkout" do let(:rental_data) { { movie_id: Movie.first.id, @@ -13,12 +13,16 @@ } it "creates a rental" do - Rental.new(rental_data).must_be :valid? + rental = Rental.new(rental_data) + rental.must_be :valid? proc { post checkout_path, params: rental_data }.must_change 'Rental.count', 1 + Rental.last.customer.id.must_equal rental.customer.id + # checkout_date.must_equal Date.today + must_respond_with :success end @@ -37,4 +41,32 @@ body.must_equal "errors" => {"movie" => ["must exist"]} end end + + describe "checkin" do + + it "updates a rental with checkin_date" do + rental_params = { + customer_id: Customer.first.id, + movie_id: Movie.first.id, + checkin_date: nil + } + + rental = Rental.where(rental_params).first + + # puts rental.inspect + + post checkin_path, params: rental_params + + rental.reload.checkin_date.must_equal Date.today + + + # proc { + # }.must_change 'Rental.count', 1 + + must_respond_with :success + end + + + end + end diff --git a/test/fixtures/movies.yml b/test/fixtures/movies.yml index 5e737617f..611bfcea7 100644 --- a/test/fixtures/movies.yml +++ b/test/fixtures/movies.yml @@ -9,9 +9,11 @@ psycho: overview: "When larcenous real estate clerk Marion Crane goes on the lam with a wad of cash and hopes of starting a new life, she ends up at the notorious Bates Motel, where manager Norman Bates cares for his housebound mother. The place seems quirky, but fine… until Marion decides to take a shower." release_date: "1960-06-16" inventory: 8 - + available_inventory: 8 + jaws: title: "The Exorcist" overview: "12-year-old Regan MacNeil begins to adapt an explicit new personality as strange events befall the local area of Georgetown. Her mother becomes torn between science and superstition in a desperate bid to save her daughter, and ultimately turns to her last hope: Father Damien Karras, a troubled priest who is struggling with his own faith." release_date: "1973-12-26" inventory: 7 + available_inventory: 7