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
1 change: 1 addition & 0 deletions app/controllers/customers_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class CustomersController < ApplicationController
# this action returns information for all of the customers in the api request
def index
customers = Customer.all
# render json: customers, status: :ok
Expand Down
3 changes: 3 additions & 0 deletions app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class MoviesController < ApplicationController

# this action will return a list of all the movies in the api response
def index
movies = Movie.all
render(
Expand All @@ -8,6 +9,7 @@ def index
)
end

# this action will return information for a sinlge instance of Movie in the api request
def show
movie = Movie.find_by(id: params[:id])

Expand All @@ -25,6 +27,7 @@ def show

end

# this action allows a new instance of Movie to be created by the user of the api
def create
movie = Movie.new(movie_params)

Expand Down
6 changes: 6 additions & 0 deletions app/controllers/rentals_controller.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
class RentalsController < ApplicationController

# This method is used to 'check-out' a movie.
# A new instance of Rental is created using a customer_id and movie_id passed in by the user
# This instance of Rental's checked_out status defaults to true
def create
rental = Rental.new(rental_data)
rental.due_date = Date.today + 3
if rental.save
render(
json: {id: rental.id, customer_id: rental.customer.id, movie_id: rental.movie.id, due_date: rental.due_date}
)
# if the user did not pass in valid information (i.e. there was no customer_id or movie_id) then the new instance of Rental will not save
else
render(
json: {error: rental.errors.messages}, status: :bad_request
)
end # if/else
end # create

# the update method is used to 'check-in' a movie. It changes the checked_out attribute for the rental to false
def update
rental = Rental.find_by(id: params[:id])
if rental
Expand All @@ -27,6 +32,7 @@ def update
json: {error: rental.errors.messages}, status: :bad_request
)
end # if/else
# if the rental_id passed in does not exist then not_found is returned
else
render(
json: {"no rental found": true}, status: :not_found
Expand Down
2 changes: 2 additions & 0 deletions app/models/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class Customer < ApplicationRecord

validates :name, presence: true

# this method calculates the number of movies checked out for the index action for customer
# when we render json in the index action it knows to run this method for each instance of Customer (treating the each instance as self) without having to pass any parameters to this method.
def movies_checked_out_count
self.rentals.where(checked_out: true).count
end # movies_checked_out
Expand Down
2 changes: 2 additions & 0 deletions app/models/movie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ class Movie < ApplicationRecord

validates :title, :inventory, presence: true

# this method shows the availible inventory for a specific movies_path
# when this method is called in the show action for movie (:available_inventory) it knows that self is the instance of movie that 'movie' points to without having to pass any parameters to this method
def available_inventory
(self.inventory) - (self.rentals.where(checked_out: true).count)
end
Expand Down