From b40b5dbf4410dd246202ce2e44aaf8834289d973 Mon Sep 17 00:00:00 2001 From: Dan Roberts Date: Wed, 14 Jun 2017 12:53:52 -0700 Subject: [PATCH 1/7] Fix the CORS issue using the rack-cors gem --- Gemfile | 2 ++ Gemfile.lock | 2 ++ config/application.rb | 10 ++++++---- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Gemfile b/Gemfile index d5c7affd..d837ba0e 100644 --- a/Gemfile +++ b/Gemfile @@ -37,6 +37,8 @@ gem 'jbuilder', '~> 2.5' gem 'will_paginate' +gem 'rack-cors', :require => 'rack/cors' + group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platform: :mri diff --git a/Gemfile.lock b/Gemfile.lock index b39604fa..f80bcea9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -119,6 +119,7 @@ GEM pry (>= 0.9.10) puma (3.6.2) rack (2.0.1) + rack-cors (0.4.1) rack-test (0.6.3) rack (>= 1.0) rails (5.0.1) @@ -208,6 +209,7 @@ DEPENDENCIES minitest-spec-rails pry-rails puma (~> 3.0) + rack-cors rails (~> 5.0.1) sass-rails (~> 5.0) spring diff --git a/config/application.rb b/config/application.rb index 2ac21a62..9f8a6f91 100644 --- a/config/application.rb +++ b/config/application.rb @@ -15,9 +15,11 @@ class Application < Rails::Application #this loads everything in the lib folder automatically config.eager_load_paths << Rails.root.join('lib') - config.action_dispatch.default_headers = { - 'Access-Control-Allow-Origin' => 'http://localhost:8081', - 'Access-Control-Request-Method' => %w{GET POST OPTIONS}.join(",") - } + config.middleware.insert_before 0, Rack::Cors do + allow do + origins '*' + resource '*', :headers => :any, :methods => [:get, :post, :put, :delete, :options] + end + end end end From 0a0c657ca337b826bdb3b5016585b8a662e8775b Mon Sep 17 00:00:00 2001 From: EricaJCase Date: Thu, 15 Jun 2017 09:49:59 -0700 Subject: [PATCH 2/7] added post route and create method" --- app/controllers/movies_controller.rb | 5 +++++ config/routes.rb | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 362e2791..520ee730 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -21,6 +21,11 @@ def show ) end + def create + movie = Movie.new(params) + movie.save + end + private def require_movie diff --git a/config/routes.rb b/config/routes.rb index 54bf033e..8eff1ff6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,7 +3,7 @@ resources :customers, only: [:index] - resources :movies, only: [:index, :show], param: :title + resources :movies, only: [:index, :show, :create], param: :title post "/rentals/:title/check-out", to: "rentals#check_out", as: "check_out" post "/rentals/:title/return", to: "rentals#check_in", as: "check_in" From 902b1138742539fcc33317b84a6692005c475e21 Mon Sep 17 00:00:00 2001 From: EricaJCase Date: Thu, 15 Jun 2017 10:07:31 -0700 Subject: [PATCH 3/7] post works for API --- app/controllers/movies_controller.rb | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 520ee730..ccf22062 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -13,17 +13,19 @@ def index def show render( - status: :ok, - json: @movie.as_json( - only: [:title, :overview, :release_date, :inventory], - methods: [:available_inventory] - ) - ) + status: :ok, + json: @movie.as_json( + only: [:title, :overview, :release_date, :inventory], + methods: [:available_inventory] + ) + ) end def create - movie = Movie.new(params) + movie = Movie.new(movie_params) movie.save + + render status: :ok, json: { title: movie.title } end private @@ -34,4 +36,8 @@ def require_movie render status: :not_found, json: { errors: { title: ["No movie with title #{params["title"]}"] } } end end + + def movie_params + params.require(:movie).permit(:id, :title, :overview, :release_date, :image_url, :external_id) + end end From 3ffa0d671c15e1819c08ddab8c49101ec6cff6c3 Mon Sep 17 00:00:00 2001 From: Janice Lichtman Date: Mon, 19 Jun 2017 17:02:50 -0700 Subject: [PATCH 4/7] fixed image problems --- app/controllers/movies_controller.rb | 5 ++++- app/models/movie.rb | 3 +++ lib/movie_wrapper.rb | 3 ++- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index ccf22062..0bce3652 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -22,7 +22,10 @@ def show end def create - movie = Movie.new(movie_params) + + modified_params = movie_params + modified_params[:image_url] = modified_params[:image_url].gsub("https://image.tmdb.org/t/p/w185","") + movie = Movie.new(modified_params) movie.save render status: :ok, json: { title: movie.title } diff --git a/app/models/movie.rb b/app/models/movie.rb index 0327a4d6..9302b1de 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -4,6 +4,9 @@ class Movie < ApplicationRecord has_many :rentals has_many :customers, through: :rentals + # validates :title, :uniqueness + #validates :title, :uniqueness => {:scope => :description} + def available_inventory self.inventory - Rental.where(movie: self, returned: false).length end diff --git a/lib/movie_wrapper.rb b/lib/movie_wrapper.rb index 7bd05c0e..6c0a20f1 100644 --- a/lib/movie_wrapper.rb +++ b/lib/movie_wrapper.rb @@ -23,11 +23,12 @@ def self.search(query) private def self.construct_movie(api_result) + Movie.new( title: api_result["title"], 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), + image_url: api_result["poster_path"] , #(api_result["poster_path"] ? self.construct_image_url(api_result["poster_path"]) : nil), external_id: api_result["id"]) end From 72f76a1394d8e35f37004cee66be3a611b660b5c Mon Sep 17 00:00:00 2001 From: EricaJCase Date: Tue, 20 Jun 2017 13:58:59 -0700 Subject: [PATCH 5/7] validates unique title/uniqueness combo" --- app/models/movie.rb | 2 ++ lib/movie_wrapper.rb | 5 ++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/models/movie.rb b/app/models/movie.rb index 0327a4d6..5b1c8eef 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -4,6 +4,8 @@ class Movie < ApplicationRecord has_many :rentals has_many :customers, through: :rentals + validates_uniqueness_of :title, :scope => [:release_date] + def available_inventory self.inventory - Rental.where(movie: self, returned: false).length end diff --git a/lib/movie_wrapper.rb b/lib/movie_wrapper.rb index 7bd05c0e..51dd4615 100644 --- a/lib/movie_wrapper.rb +++ b/lib/movie_wrapper.rb @@ -27,12 +27,11 @@ def self.construct_movie(api_result) title: api_result["title"], 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"]) + image_url: api_result["img_name"]) end def self.construct_image_url(img_name) - return BASE_IMG_URL + DEFAULT_IMG_SIZE + img_name + return img_name end end From d77c64ee805f041c7dd722801515f06940648aab Mon Sep 17 00:00:00 2001 From: Janice Lichtman Date: Wed, 21 Jun 2017 16:23:56 -0700 Subject: [PATCH 6/7] seems not to match Erica's mchine --- app/controllers/movies_controller.rb | 7 +++++-- lib/movie_wrapper.rb | 4 ++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 0bce3652..fde49a6b 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -26,9 +26,12 @@ def create modified_params = movie_params modified_params[:image_url] = modified_params[:image_url].gsub("https://image.tmdb.org/t/p/w185","") movie = Movie.new(modified_params) - movie.save - render status: :ok, json: { title: movie.title } + if movie.save + render status: :ok, json: { title: movie.title } + else + render status: :bad_request, json: { errors: movie.errors.messages } + end end private diff --git a/lib/movie_wrapper.rb b/lib/movie_wrapper.rb index 829a57f1..1b3892e7 100644 --- a/lib/movie_wrapper.rb +++ b/lib/movie_wrapper.rb @@ -28,12 +28,12 @@ def self.construct_movie(api_result) title: api_result["title"], 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), + image_url: api_result["poster_path"] , #(api_result["poster_path"] ? self.construct_image_url(api_result["poster_path"]) : nil), external_id: api_result["id"]) end def self.construct_image_url(img_name) - return img_name + return BASE_IMG_URL + DEFAULT_IMG_SIZE + img_name end end From ad74f31263c6a11f91eecaf50709b150eb0a08b4 Mon Sep 17 00:00:00 2001 From: EricaJCase Date: Wed, 21 Jun 2017 16:26:34 -0700 Subject: [PATCH 7/7] saving for the big merge --- lib/movie_wrapper.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/movie_wrapper.rb b/lib/movie_wrapper.rb index 829a57f1..6c0a20f1 100644 --- a/lib/movie_wrapper.rb +++ b/lib/movie_wrapper.rb @@ -28,12 +28,12 @@ def self.construct_movie(api_result) title: api_result["title"], 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), + image_url: api_result["poster_path"] , #(api_result["poster_path"] ? self.construct_image_url(api_result["poster_path"]) : nil), external_id: api_result["id"]) end def self.construct_image_url(img_name) - return img_name + return BASE_IMG_URL + DEFAULT_IMG_SIZE + img_name end end