diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index be25f1be..3c6d9e13 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -18,6 +18,20 @@ def index ) end + def show + customer = Customer.find_by(id: params[:id]) + if customer + rental_data = customer.rentals.where(returned: false) + + render status: :ok, json: rental_data.as_json( + only: [:returned, :due_date], + methods: [:movie] + ) + else + render status: :not_found, json: { errors: { id: ["No customer with id #{params["id"]}"] } } + end + end + private def parse_query_args errors = {} diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 362e2791..313e7d56 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -1,6 +1,6 @@ class MoviesController < ApplicationController before_action :require_movie, only: [:show] - + def index if params[:query] data = MovieWrapper.search(params[:query]) @@ -8,21 +8,47 @@ def index data = Movie.all end + if params[:p] || params[:n] + data = data.paginate(page: params[:p], per_page: params[:n]) + end + render status: :ok, json: data end - + def show render( status: :ok, json: @movie.as_json( only: [:title, :overview, :release_date, :inventory], methods: [:available_inventory] + ) + ) + end + + + def create + new_movie = Movie.new(movie_params) + + if new_movie.save + render( + status: :ok, + json: new_movie.as_json( + only: [:title, :overview, :release_date, :external_id, :image_url] ) ) + else + render json: { + ok: false, + errors: new_movie.errors.messages + }, status: :bad_request + end end - + private - + def movie_params + return params.permit(:title, :overview, :release_date, :external_id, :image_url) + end + def require_movie @movie = Movie.find_by(title: params[:title]) unless @movie @@ -30,3 +56,4 @@ def require_movie end end end + \ No newline at end of file diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 67e77073..687d0ce0 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -38,7 +38,8 @@ def overdue name: rental.customer.name, postal_code: rental.customer.postal_code, checkout_date: rental.checkout_date, - due_date: rental.due_date + due_date: rental.due_date, + movie: rental.movie } end render status: :ok, json: rentals diff --git a/app/models/movie.rb b/app/models/movie.rb index fda94941..a24f52e7 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -1,4 +1,5 @@ class Movie < ApplicationRecord + validates :external_id, presence: true,uniqueness: { message: "This movie already exists in your library!" } has_many :rentals has_many :customers, through: :rentals diff --git a/config/routes.rb b/config/routes.rb index f4c99688..b7cae9da 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,9 +1,10 @@ Rails.application.routes.draw do # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html - resources :customers, only: [:index] + resources :customers, only: [:index, :show] resources :movies, only: [:index, :show], param: :title + post "/movies", to: "movies#create" post "/rentals/:title/check-out", to: "rentals#check_out", as: "check_out" post "/rentals/:title/return", to: "rentals#check_in", as: "check_in"