diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index c3919a8f6..3ef2de06d 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -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 diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index a0d26bb50..142638721 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -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( @@ -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]) @@ -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) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index c94b15a5e..e160d97c4 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -1,5 +1,8 @@ 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 @@ -7,6 +10,7 @@ def create 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 @@ -14,6 +18,7 @@ def create 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 @@ -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 diff --git a/app/models/customer.rb b/app/models/customer.rb index 8f4558692..c666fdb0b 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -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 diff --git a/app/models/movie.rb b/app/models/movie.rb index 6f23eb1b6..b30daecc5 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -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