diff --git a/Gemfile.lock b/Gemfile.lock index 9f7601a8..22779079 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -118,7 +118,7 @@ GEM slop (~> 3.4) pry-rails (0.3.4) pry (>= 0.9.10) - puma (3.6.2) + puma (3.11.4) rack (2.0.1) rack-test (0.6.3) rack (>= 1.0) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 362e2791..1b3c2dc1 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -17,8 +17,36 @@ def show json: @movie.as_json( only: [:title, :overview, :release_date, :inventory], methods: [:available_inventory] - ) ) + ) + end + + def add_movie + #pass in new_movie from params...somehow..its an object + @new_movie = Movie.new() + @new_movie.title = params[:title] + @new_movie.overview = params[:overview] + @new_movie.release_date = params[:release_date] + @new_movie.image_url = params[:image_url] + @new_movie.external_id = params[:external_id] + + if Movie.find_by(external_id: params[:external_id]) + render( + status: :bad_request, json: { errors: "already in movie library" } + ) + else + if @new_movie.save + render( + status: :ok, + json: @new_movie.as_json( + # also can return external_id and then have the front end do an api call again + only: [:title, :overview] + ) + ) + else + render status: :bad_request, json: { errors: rental.errors.messages } + end + end end private diff --git a/config/routes.rb b/config/routes.rb index 54bf033e..dab8b77d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,6 +5,8 @@ resources :movies, only: [:index, :show], param: :title + post "/add_movie", to: "movies#add_movie", as: "add_movie" + post "/rentals/:title/check-out", to: "rentals#check_out", as: "check_out" post "/rentals/:title/return", to: "rentals#check_in", as: "check_in" get "/rentals/overdue", to: "rentals#overdue", as: "overdue"