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
22 changes: 21 additions & 1 deletion app/controllers/rentals_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
49 changes: 48 additions & 1 deletion test/controllers/rentals_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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