From 80bcbd2850b5fdbba5ce866551933765ee511005 Mon Sep 17 00:00:00 2001 From: Kelly Downes Date: Wed, 26 Jun 2019 13:47:49 -0700 Subject: [PATCH 1/5] Add add_movie method and route --- app/controllers/movies_controller.rb | 18 ++++++++++++++---- config/routes.rb | 3 ++- lib/movie_wrapper.rb | 7 ++++--- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 362e2791..43435f12 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -16,9 +16,19 @@ def show status: :ok, json: @movie.as_json( only: [:title, :overview, :release_date, :inventory], - methods: [:available_inventory] - ) - ) + methods: [:available_inventory], + ), + ) + end + + def add_movie + movie = Movie.new(title: params[:title], overview: params[:overview], release_date: params[:release_date], image_url: params[:image_url], external_id: params[:external_id]) + + if movie.save + render status: :ok, json: {} + else + render status: :bad_request, json: {errors: movie.errors.messages} + end end private @@ -26,7 +36,7 @@ def show def require_movie @movie = Movie.find_by(title: params[:title]) unless @movie - render status: :not_found, json: { errors: { title: ["No movie with title #{params["title"]}"] } } + render status: :not_found, json: {errors: {title: ["No movie with title #{params["title"]}"]}} end end end diff --git a/config/routes.rb b/config/routes.rb index f4c99688..8ea080ed 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -9,6 +9,7 @@ post "/rentals/:title/return", to: "rentals#check_in", as: "check_in" get "/rentals/overdue", to: "rentals#overdue", as: "overdue" - root 'movies#index' + post "/movies/add-movie", to: "movies#add_movie", as: "add_movie" + root "movies#index" end diff --git a/lib/movie_wrapper.rb b/lib/movie_wrapper.rb index 0152c40e..a4a86d04 100644 --- a/lib/movie_wrapper.rb +++ b/lib/movie_wrapper.rb @@ -8,8 +8,9 @@ class MovieWrapper DEFAULT_IMG_URL = "http://lorempixel.com/185/278/" def self.search(query) + sleep(1) url = BASE_URL + "search/movie?api_key=" + KEY + "&query=" + query - response = HTTParty.get(url) + response = HTTParty.get(url) if response["total_results"] == 0 return [] else @@ -28,11 +29,11 @@ def self.construct_movie(api_result) overview: api_result["overview"], release_date: api_result["release_date"], image_url: api_result["poster_path"], #(api_result["poster_path"] ? self.construct_image_url(api_result["poster_path"]) : nil), - external_id: api_result["id"]) + external_id: api_result["id"], + ) end def self.construct_image_url(img_name) return BASE_IMG_URL + DEFAULT_IMG_SIZE + img_name end - end From 3d1f3bf45e64dbebbb3e3385925fd92e30704b4e Mon Sep 17 00:00:00 2001 From: Kelly Downes Date: Wed, 26 Jun 2019 14:39:13 -0700 Subject: [PATCH 2/5] Add condition to handle duplicate movie adding --- app/controllers/movies_controller.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 43435f12..bb42d969 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -24,7 +24,11 @@ def show def add_movie movie = Movie.new(title: params[:title], overview: params[:overview], release_date: params[:release_date], image_url: params[:image_url], external_id: params[:external_id]) - if movie.save + unless Movie.find_by(external_id: params[:external_id]) + successful = movie.save + end + + if successful render status: :ok, json: {} else render status: :bad_request, json: {errors: movie.errors.messages} From 93dd50157152d08a6bef206fc5b350a9d16726db Mon Sep 17 00:00:00 2001 From: Jansen Date: Wed, 26 Jun 2019 14:53:38 -0700 Subject: [PATCH 3/5] Add logic handling cases where the movie is already inside the library --- app/controllers/movies_controller.rb | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index bb42d969..68d8aaff 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -24,14 +24,18 @@ def show def add_movie movie = Movie.new(title: params[:title], overview: params[:overview], release_date: params[:release_date], image_url: params[:image_url], external_id: params[:external_id]) - unless Movie.find_by(external_id: params[:external_id]) - successful = movie.save - end + already_added = Movie.find_by(external_id: params[:external_id]) + + # unless Movie.find_by(external_id: params[:external_id]) + # successful = movie.save + # end - if successful + if already_added + render status: :bad_request, json: { errors: "This movie is already in the library" } + elsif movie.save render status: :ok, json: {} else - render status: :bad_request, json: {errors: movie.errors.messages} + render status: :bad_request, json: { errors: movie.errors.messages } end end @@ -40,7 +44,7 @@ def add_movie def require_movie @movie = Movie.find_by(title: params[:title]) unless @movie - render status: :not_found, json: {errors: {title: ["No movie with title #{params["title"]}"]}} + render status: :not_found, json: { errors: { title: ["No movie with title #{params["title"]}"] } } end end end From 2e5d892470f7068b291e37379303a1ea26c60beb Mon Sep 17 00:00:00 2001 From: Jansen Date: Thu, 27 Jun 2019 10:24:08 -0700 Subject: [PATCH 4/5] Fix image error --- app/controllers/movies_controller.rb | 2 +- app/models/movie.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 68d8aaff..66bec227 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -31,7 +31,7 @@ def add_movie # end if already_added - render status: :bad_request, json: { errors: "This movie is already in the library" } + render status: :conflict, json: { errors: "This movie is already in the library" } elsif movie.save render status: :ok, json: {} else diff --git a/app/models/movie.rb b/app/models/movie.rb index 0016080b..b1bc6560 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -10,7 +10,7 @@ def image_url orig_value = read_attribute :image_url if !orig_value MovieWrapper::DEFAULT_IMG_URL - elsif external_id + elsif external_id && !orig_value.include?("https://image.tmdb.org/t/p/") MovieWrapper.construct_image_url(orig_value) else orig_value From 18c5fdd7f678b02ba72480f015de18afcf96dd50 Mon Sep 17 00:00:00 2001 From: Kelly Downes Date: Thu, 27 Jun 2019 12:07:52 -0700 Subject: [PATCH 5/5] Remove comments --- app/controllers/movies_controller.rb | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 66bec227..4bda55d4 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -26,16 +26,12 @@ def add_movie already_added = Movie.find_by(external_id: params[:external_id]) - # unless Movie.find_by(external_id: params[:external_id]) - # successful = movie.save - # end - if already_added - render status: :conflict, json: { errors: "This movie is already in the library" } + render status: :conflict, json: {errors: "This movie is already in the library"} elsif movie.save render status: :ok, json: {} else - render status: :bad_request, json: { errors: movie.errors.messages } + render status: :bad_request, json: {errors: movie.errors.messages} end end @@ -44,7 +40,7 @@ def add_movie def require_movie @movie = Movie.find_by(title: params[:title]) unless @movie - render status: :not_found, json: { errors: { title: ["No movie with title #{params["title"]}"] } } + render status: :not_found, json: {errors: {title: ["No movie with title #{params["title"]}"]}} end end end