Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion app/controllers/rentals_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def overdue
def checkout
rental = Rental.new(rental_params)

# if rental.movie.available_inventory
if rental.save
render(
json: {
Expand All @@ -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
1 change: 1 addition & 0 deletions app/models/movie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ def checked_out(status)
end



end
7 changes: 6 additions & 1 deletion app/models/rental.rb
Original file line number Diff line number Diff line change
@@ -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
36 changes: 34 additions & 2 deletions test/controllers/rentals_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe RentalsController do


describe "create" do
describe "checkout" do
let(:rental_data) {
{
movie_id: Movie.first.id,
Expand All @@ -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

Expand All @@ -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
3 changes: 3 additions & 0 deletions test/fixtures/movies.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ 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: "Jaws"
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

lambs:
title: "The Silence of the Lambs"
Expand Down