From b40b5dbf4410dd246202ce2e44aaf8834289d973 Mon Sep 17 00:00:00 2001 From: Dan Roberts Date: Wed, 14 Jun 2017 12:53:52 -0700 Subject: [PATCH 1/8] 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 41d99b5ba2573192c629b50e762ef47d333a87d6 Mon Sep 17 00:00:00 2001 From: natalia-ku Date: Wed, 14 Jun 2017 14:25:11 -0700 Subject: [PATCH 2/8] made post work --- app/controllers/movies_controller.rb | 11 ++++++++++- app/controllers/rentals_controller.rb | 5 +++++ config/routes.rb | 3 ++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 362e2791..ad598ff2 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -7,10 +7,16 @@ def index else data = Movie.all end - render status: :ok, json: data end + def create + print "In create method:" + movie = Movie.create(movie_params) + + end + + def show render( status: :ok, @@ -22,6 +28,9 @@ def show end private + def movie_params + params.require(:movie).permit(:title, :overview, :release_date, :image_url) + end def require_movie @movie = Movie.find_by(title: params[:title]) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 92744380..8dbd1d12 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -2,6 +2,11 @@ class RentalsController < ApplicationController before_action :require_movie, only: [:check_out, :check_in] before_action :require_customer, only: [:check_out, :check_in] + # def create + # rental = Rental.create(params) + # puts rental + # end + # def check_out rental = Rental.new(movie: @movie, customer: @customer, due_date: params[:due_date], returned: false) diff --git a/config/routes.rb b/config/routes.rb index 54bf033e..c809c87b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,7 +4,8 @@ resources :customers, only: [:index] resources :movies, only: [:index, :show], param: :title - +post "/movies", to: "movies#create" + # post "/rentals", to: "rentals#create" 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" From 4a3426508db12ae1b3382b3e351453f2a473ebdd Mon Sep 17 00:00:00 2001 From: Ting Wong Date: Wed, 14 Jun 2017 15:24:26 -0700 Subject: [PATCH 3/8] funny images not working --- app/controllers/movies_controller.rb | 20 ++++++++++++-------- app/models/movie.rb | 4 ++++ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index ad598ff2..c147430b 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -12,19 +12,23 @@ def index def create print "In create method:" - movie = Movie.create(movie_params) - + movie = Movie.new(movie_params) + existing_movie = Movie.find_by(title: params[:title]) + print params[:image_url] + if existing_movie.nil? + movie.save + end end 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 private diff --git a/app/models/movie.rb b/app/models/movie.rb index 0327a4d6..235fdf4e 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -9,11 +9,15 @@ def available_inventory end def image_url + puts "IN IMAGE URL METHOD" + orig_value = read_attribute :image_url + puts orig_value if !orig_value MovieWrapper::DEFAULT_IMG_URL else MovieWrapper.construct_image_url(orig_value) + # return orig_value end end end From 5b86dd076d09bd6a256d93d521e6fc3ec685b18b Mon Sep 17 00:00:00 2001 From: Ting Wong Date: Wed, 14 Jun 2017 15:52:19 -0700 Subject: [PATCH 4/8] fixed image problems in movies controller --- app/controllers/movies_controller.rb | 20 +++++++++++++++----- app/models/movie.rb | 3 --- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index c147430b..150f8181 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -12,12 +12,22 @@ def index def create print "In create method:" - movie = Movie.new(movie_params) + + movie_data = { + title: movie_params[:title], + overview: movie_params[:overview], + release_date: movie_params[:release_date], + image_url: movie_params[:image_url][31..-1] + } + + movie = Movie.new(movie_data) existing_movie = Movie.find_by(title: params[:title]) - print params[:image_url] - if existing_movie.nil? - movie.save - end + movie.save + print movie.image_url + # if existing_movie.nil? + # movie.save + # print movie.image_url + # end end diff --git a/app/models/movie.rb b/app/models/movie.rb index 235fdf4e..109107aa 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -9,10 +9,7 @@ def available_inventory end def image_url - puts "IN IMAGE URL METHOD" - orig_value = read_attribute :image_url - puts orig_value if !orig_value MovieWrapper::DEFAULT_IMG_URL else From ff5c943b3b33616f8a561dfb8cdd0cefb2fdd6e7 Mon Sep 17 00:00:00 2001 From: Ting Wong Date: Wed, 14 Jun 2017 15:56:37 -0700 Subject: [PATCH 5/8] FINISHED CORE REQUIREMENTS --- app/controllers/movies_controller.rb | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 150f8181..9004d83e 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -22,12 +22,9 @@ def create movie = Movie.new(movie_data) existing_movie = Movie.find_by(title: params[:title]) - movie.save - print movie.image_url - # if existing_movie.nil? - # movie.save - # print movie.image_url - # end + if existing_movie.nil? + movie.save + end end From c9072eaeb024cbbbe0fd6f33dd4610e223d6142f Mon Sep 17 00:00:00 2001 From: natalia-ku Date: Thu, 15 Jun 2017 09:04:16 -0700 Subject: [PATCH 6/8] removes double --- app/controllers/movies_controller.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 9004d83e..9df2c676 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -21,7 +21,8 @@ def create } movie = Movie.new(movie_data) - existing_movie = Movie.find_by(title: params[:title]) + existing_movie = Movie.find_by(title: params[:title], release_date: params[:release_date]) + puts existing_movie if existing_movie.nil? movie.save end From 2a9dd270017d3d5bbee00f02b86e39c4467ca0e5 Mon Sep 17 00:00:00 2001 From: Ting Wong Date: Mon, 19 Jun 2017 11:10:08 -0700 Subject: [PATCH 7/8] edited routes for api --- app/controllers/rentals_controller.rb | 10 +++++----- config/routes.rb | 5 +++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 8dbd1d12..0231149d 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -2,11 +2,11 @@ class RentalsController < ApplicationController before_action :require_movie, only: [:check_out, :check_in] before_action :require_customer, only: [:check_out, :check_in] - # def create - # rental = Rental.create(params) - # puts rental - # end - # + def index + rentals = Rental.all + render status: :ok, json: rentals + end + def check_out rental = Rental.new(movie: @movie, customer: @customer, due_date: params[:due_date], returned: false) diff --git a/config/routes.rb b/config/routes.rb index c809c87b..d11f78d7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,8 +4,9 @@ resources :customers, only: [:index] resources :movies, only: [:index, :show], param: :title -post "/movies", to: "movies#create" - # post "/rentals", to: "rentals#create" + post "/movies", to: "movies#create" + get "/rentals", to: "rentals#index" + # post "/rentals", to: "rentals#checkout" 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" From 15e60fdb4e378bf57fef55499c51de18c2af2e23 Mon Sep 17 00:00:00 2001 From: Ting Wong Date: Tue, 20 Jun 2017 16:10:16 -0700 Subject: [PATCH 8/8] changed API to handle returns by id number --- app/controllers/rentals_controller.rb | 10 +++++++++- config/routes.rb | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 0231149d..1c0c2c72 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -1,5 +1,6 @@ class RentalsController < ApplicationController - before_action :require_movie, only: [:check_out, :check_in] + before_action :require_movie, only: [:check_out] + before_action :require_movie_checkin, only: [:check_in ] before_action :require_customer, only: [:check_out, :check_in] def index @@ -57,6 +58,13 @@ def require_movie end end + def require_movie_checkin + @movie = Movie.find_by id: params[:movie_id] + unless @movie + render status: :not_found, json: { errors: { title: ["No movie with id #{params[:movie_id]}"] } } + end + end + def require_customer @customer = Customer.find_by id: params[:customer_id] unless @customer diff --git a/config/routes.rb b/config/routes.rb index d11f78d7..ba266ced 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,7 +8,7 @@ get "/rentals", to: "rentals#index" # post "/rentals", to: "rentals#checkout" post "/rentals/:title/check-out", to: "rentals#check_out", as: "check_out" - post "/rentals/:title/return", to: "rentals#check_in", as: "check_in" + post "/rentals/:movie_id/return", to: "rentals#check_in", as: "check_in" get "/rentals/overdue", to: "rentals#overdue", as: "overdue"