From 22a070df4b7c33a894b602427a1d34eb46c199fb Mon Sep 17 00:00:00 2001 From: Sarah Read-Brown Date: Mon, 18 Dec 2017 11:55:09 -0800 Subject: [PATCH 1/7] Did basic set up for local machine --- Gemfile.lock | 219 ++++++++++++++++++++++++++------------------------- 1 file changed, 112 insertions(+), 107 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index b39604fa..af4a7c2c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,104 +1,107 @@ GEM remote: https://rubygems.org/ specs: - actioncable (5.0.1) - actionpack (= 5.0.1) - nio4r (~> 1.2) + actioncable (5.0.6) + actionpack (= 5.0.6) + nio4r (>= 1.2, < 3.0) websocket-driver (~> 0.6.1) - actionmailer (5.0.1) - actionpack (= 5.0.1) - actionview (= 5.0.1) - activejob (= 5.0.1) + actionmailer (5.0.6) + actionpack (= 5.0.6) + actionview (= 5.0.6) + activejob (= 5.0.6) mail (~> 2.5, >= 2.5.4) rails-dom-testing (~> 2.0) - actionpack (5.0.1) - actionview (= 5.0.1) - activesupport (= 5.0.1) + actionpack (5.0.6) + actionview (= 5.0.6) + activesupport (= 5.0.6) rack (~> 2.0) rack-test (~> 0.6.3) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.0.2) - actionview (5.0.1) - activesupport (= 5.0.1) + actionview (5.0.6) + activesupport (= 5.0.6) builder (~> 3.1) erubis (~> 2.7.0) rails-dom-testing (~> 2.0) - rails-html-sanitizer (~> 1.0, >= 1.0.2) - active_model_serializers (0.10.6) + rails-html-sanitizer (~> 1.0, >= 1.0.3) + active_model_serializers (0.10.7) actionpack (>= 4.1, < 6) activemodel (>= 4.1, < 6) case_transform (>= 0.2) - jsonapi-renderer (>= 0.1.1.beta1, < 0.2) - activejob (5.0.1) - activesupport (= 5.0.1) + jsonapi-renderer (>= 0.1.1.beta1, < 0.3) + activejob (5.0.6) + activesupport (= 5.0.6) globalid (>= 0.3.6) - activemodel (5.0.1) - activesupport (= 5.0.1) - activerecord (5.0.1) - activemodel (= 5.0.1) - activesupport (= 5.0.1) + activemodel (5.0.6) + activesupport (= 5.0.6) + activerecord (5.0.6) + activemodel (= 5.0.6) + activesupport (= 5.0.6) arel (~> 7.0) - activesupport (5.0.1) + activesupport (5.0.6) concurrent-ruby (~> 1.0, >= 1.0.2) i18n (~> 0.7) minitest (~> 5.1) tzinfo (~> 1.1) ansi (1.5.0) arel (7.1.4) - better_errors (2.1.1) + better_errors (2.4.0) coderay (>= 1.0.0) - erubis (>= 2.6.6) + erubi (>= 1.0.0) rack (>= 0.9.0) - binding_of_caller (0.7.2) + bindex (0.5.0) + binding_of_caller (0.7.3) debug_inspector (>= 0.0.1) - builder (3.2.2) - byebug (9.0.6) + builder (3.2.3) + byebug (9.1.0) case_transform (0.2) activesupport - coderay (1.1.1) - coffee-rails (4.2.1) + coderay (1.1.2) + coffee-rails (4.2.2) coffee-script (>= 2.2.0) - railties (>= 4.0.0, < 5.2.x) + railties (>= 4.0.0) coffee-script (2.4.1) coffee-script-source execjs coffee-script-source (1.12.2) - concurrent-ruby (1.0.4) - debug_inspector (0.0.2) - dotenv (2.2.0) - dotenv-rails (2.2.0) - dotenv (= 2.2.0) - railties (>= 3.2, < 5.1) + concurrent-ruby (1.0.5) + crass (1.0.3) + debug_inspector (0.0.3) + dotenv (2.2.1) + dotenv-rails (2.2.1) + dotenv (= 2.2.1) + railties (>= 3.2, < 5.2) + erubi (1.7.0) erubis (2.7.0) execjs (2.7.0) - ffi (1.9.14) - globalid (0.3.7) - activesupport (>= 4.1.0) - httparty (0.15.5) + ffi (1.9.18) + globalid (0.4.1) + activesupport (>= 4.2.0) + httparty (0.15.6) multi_xml (>= 0.5.2) - i18n (0.7.0) - jbuilder (2.6.1) - activesupport (>= 3.0.0, < 5.1) - multi_json (~> 1.2) - jquery-rails (4.2.2) + i18n (0.9.1) + concurrent-ruby (~> 1.0) + jbuilder (2.7.0) + activesupport (>= 4.2.0) + multi_json (>= 1.2) + jquery-rails (4.3.1) rails-dom-testing (>= 1, < 3) railties (>= 4.2.0) thor (>= 0.14, < 2.0) - jsonapi-renderer (0.1.2) + jsonapi-renderer (0.2.0) listen (3.0.8) rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) - loofah (2.0.3) + loofah (2.1.1) + crass (~> 1.0.2) nokogiri (>= 1.5.9) - mail (2.6.4) - mime-types (>= 1.16, < 4) - method_source (0.8.2) - mime-types (3.1) - mime-types-data (~> 3.2015) - mime-types-data (3.2016.0521) - mini_portile2 (2.1.0) - minitest (5.10.1) - minitest-reporters (1.1.12) + mail (2.7.0) + mini_mime (>= 0.1.1) + method_source (0.9.0) + mini_mime (1.0.0) + mini_portile2 (2.3.0) + minitest (5.10.3) + minitest-reporters (1.1.19) ansi builder minitest (>= 5.0) @@ -106,58 +109,60 @@ GEM minitest-spec-rails (5.4.0) minitest (~> 5.0) rails (>= 4.1) - multi_json (1.12.1) + multi_json (1.12.2) multi_xml (0.6.0) - nio4r (1.2.1) - nokogiri (1.7.0.1) - mini_portile2 (~> 2.1.0) - pry (0.10.4) + nio4r (2.1.0) + nokogiri (1.8.1) + mini_portile2 (~> 2.3.0) + pry (0.11.3) coderay (~> 1.1.0) - method_source (~> 0.8.1) - slop (~> 3.4) - pry-rails (0.3.4) - pry (>= 0.9.10) - puma (3.6.2) - rack (2.0.1) + method_source (~> 0.9.0) + pry-rails (0.3.6) + pry (>= 0.10.4) + puma (3.11.0) + rack (2.0.3) rack-test (0.6.3) rack (>= 1.0) - rails (5.0.1) - actioncable (= 5.0.1) - actionmailer (= 5.0.1) - actionpack (= 5.0.1) - actionview (= 5.0.1) - activejob (= 5.0.1) - activemodel (= 5.0.1) - activerecord (= 5.0.1) - activesupport (= 5.0.1) - bundler (>= 1.3.0, < 2.0) - railties (= 5.0.1) + rails (5.0.6) + actioncable (= 5.0.6) + actionmailer (= 5.0.6) + actionpack (= 5.0.6) + actionview (= 5.0.6) + activejob (= 5.0.6) + activemodel (= 5.0.6) + activerecord (= 5.0.6) + activesupport (= 5.0.6) + bundler (>= 1.3.0) + railties (= 5.0.6) sprockets-rails (>= 2.0.0) - rails-dom-testing (2.0.2) - activesupport (>= 4.2.0, < 6.0) - nokogiri (~> 1.6) + rails-dom-testing (2.0.3) + activesupport (>= 4.2.0) + nokogiri (>= 1.6) rails-html-sanitizer (1.0.3) loofah (~> 2.0) - railties (5.0.1) - actionpack (= 5.0.1) - activesupport (= 5.0.1) + railties (5.0.6) + actionpack (= 5.0.6) + activesupport (= 5.0.6) method_source rake (>= 0.8.7) thor (>= 0.18.1, < 2.0) - rake (12.0.0) - rb-fsevent (0.9.8) - rb-inotify (0.9.7) - ffi (>= 0.5.0) - ruby-progressbar (1.8.1) - sass (3.4.23) - sass-rails (5.0.6) + rake (12.3.0) + rb-fsevent (0.10.2) + rb-inotify (0.9.10) + ffi (>= 0.5.0, < 2) + ruby-progressbar (1.9.0) + sass (3.5.4) + sass-listen (~> 4.0.0) + sass-listen (4.0.0) + rb-fsevent (~> 0.9, >= 0.9.4) + rb-inotify (~> 0.9, >= 0.9.7) + sass-rails (5.0.7) railties (>= 4.0.0, < 6) sass (~> 3.1) sprockets (>= 2.8, < 4.0) sprockets-rails (>= 2.0, < 4.0) tilt (>= 1.1, < 3) - slop (3.6.0) - spring (2.0.0) + spring (2.0.2) activesupport (>= 4.2) spring-watcher-listen (2.0.1) listen (>= 2.7, < 4.0) @@ -165,30 +170,30 @@ GEM sprockets (3.7.1) concurrent-ruby (~> 1.0) rack (> 1, < 3) - sprockets-rails (3.2.0) + sprockets-rails (3.2.1) actionpack (>= 4.0) activesupport (>= 4.0) sprockets (>= 3.0.0) sqlite3 (1.3.13) - thor (0.19.4) - thread_safe (0.3.5) - tilt (2.0.5) + thor (0.20.0) + thread_safe (0.3.6) + tilt (2.0.8) turbolinks (5.0.1) turbolinks-source (~> 5) - turbolinks-source (5.0.0) - tzinfo (1.2.2) + turbolinks-source (5.0.3) + tzinfo (1.2.4) thread_safe (~> 0.1) - uglifier (3.0.4) + uglifier (4.0.2) execjs (>= 0.3.0, < 3) - web-console (3.4.0) + web-console (3.5.1) actionview (>= 5.0) activemodel (>= 5.0) - debug_inspector + bindex (>= 0.4.0) railties (>= 5.0) - websocket-driver (0.6.4) + websocket-driver (0.6.5) websocket-extensions (>= 0.1.0) - websocket-extensions (0.1.2) - will_paginate (3.1.5) + websocket-extensions (0.1.3) + will_paginate (3.1.6) PLATFORMS ruby @@ -220,4 +225,4 @@ DEPENDENCIES will_paginate BUNDLED WITH - 1.14.6 + 1.15.4 From cbe84fd07dca7f7ab149b8309c97d9f99ca31833 Mon Sep 17 00:00:00 2001 From: Sarah Read-Brown Date: Mon, 18 Dec 2017 14:02:40 -0800 Subject: [PATCH 2/7] changes config file to 8080 --- config/application.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/application.rb b/config/application.rb index 2ac21a62..c3e76843 100644 --- a/config/application.rb +++ b/config/application.rb @@ -16,7 +16,7 @@ class Application < Rails::Application config.eager_load_paths << Rails.root.join('lib') config.action_dispatch.default_headers = { - 'Access-Control-Allow-Origin' => 'http://localhost:8081', + 'Access-Control-Allow-Origin' => 'http://localhost:8080', 'Access-Control-Request-Method' => %w{GET POST OPTIONS}.join(",") } end From 4883b513e397cd4cb0065149fad9aae3fda4be66 Mon Sep 17 00:00:00 2001 From: Sarah Read-Brown Date: Tue, 19 Dec 2017 12:35:40 -0800 Subject: [PATCH 3/7] added notes for how to proceed with adding a movie to db after lunch --- app/controllers/movies_controller.rb | 24 +++++++++++++++++++++++- config/routes.rb | 3 ++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 362e2791..f6d88b6d 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -11,12 +11,34 @@ def index render status: :ok, json: data end + def create + # check movies to see if it exists already? + @movie = Movie.find_by(title: params[:title]) + + # if the movie exists send back + if @movie + data = false; + render( + status: :bad_request, + json: data, + ) + else + # make a call to the api for movie with external_id? + # can we just pass in params? + # create a hash with title, overview, release_date, poster_path, external_id + MovieWrapper.construct_movie(params) + + 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] + # methods: [:available_inventory] ) ) end diff --git a/config/routes.rb b/config/routes.rb index 54bf033e..3f57ac95 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,11 +3,12 @@ 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" get "/rentals/overdue", to: "rentals#overdue", as: "overdue" + end From 9a5b511ba7b9c7cb68be78daa553b2cdb32bd4e8 Mon Sep 17 00:00:00 2001 From: Andrea Chen Date: Tue, 19 Dec 2017 14:54:35 -0800 Subject: [PATCH 4/7] Add gem rack-cors to gemfile to get Rails to accept OPTIONS request --- Gemfile | 2 ++ Gemfile.lock | 4 +++- config/application.rb | 7 +++++++ 3 files changed, 12 insertions(+), 1 deletion(-) 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 af4a7c2c..c560ee11 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -121,6 +121,7 @@ GEM pry (>= 0.10.4) puma (3.11.0) rack (2.0.3) + rack-cors (1.0.2) rack-test (0.6.3) rack (>= 1.0) rails (5.0.6) @@ -213,6 +214,7 @@ DEPENDENCIES minitest-spec-rails pry-rails puma (~> 3.0) + rack-cors rails (~> 5.0.1) sass-rails (~> 5.0) spring @@ -225,4 +227,4 @@ DEPENDENCIES will_paginate BUNDLED WITH - 1.15.4 + 1.16.0.pre.3 diff --git a/config/application.rb b/config/application.rb index c3e76843..6dee1a05 100644 --- a/config/application.rb +++ b/config/application.rb @@ -19,5 +19,12 @@ class Application < Rails::Application 'Access-Control-Allow-Origin' => 'http://localhost:8080', '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, :options, :patch] + end + end end end From 68f57e7b513fb7e83343dea18ddee80ef2cb82df Mon Sep 17 00:00:00 2001 From: Andrea Chen Date: Tue, 19 Dec 2017 15:09:02 -0800 Subject: [PATCH 5/7] Save new movie --- app/controllers/movies_controller.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index f6d88b6d..a98b2f66 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -13,10 +13,10 @@ def index def create # check movies to see if it exists already? - @movie = Movie.find_by(title: params[:title]) + movie = Movie.find_by(title: params[:title]) # if the movie exists send back - if @movie + if movie data = false; render( status: :bad_request, @@ -26,7 +26,8 @@ def create # make a call to the api for movie with external_id? # can we just pass in params? # create a hash with title, overview, release_date, poster_path, external_id - MovieWrapper.construct_movie(params) + new_movie = MovieWrapper.construct_movie(params) + new_movie.save end From c41486d7d664042e16f8cce03cdde1edb6f8370f Mon Sep 17 00:00:00 2001 From: Sarah Read-Brown Date: Tue, 19 Dec 2017 15:46:08 -0800 Subject: [PATCH 6/7] update create method and add error handling for movie duplicates --- app/controllers/movies_controller.rb | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index a98b2f66..54c2fc0f 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -17,21 +17,12 @@ def create # if the movie exists send back if movie - data = false; - render( - status: :bad_request, - json: data, - ) + render status: :not_found, json: { errors: { title: ["The movie #{params["title"]} has already been added to our Library."] } } else - # make a call to the api for movie with external_id? - # can we just pass in params? - # create a hash with title, overview, release_date, poster_path, external_id new_movie = MovieWrapper.construct_movie(params) new_movie.save - + render status: :ok, json: data end - - render status: :ok, json: data end def show From 05042b9161838cec1e58ae50d797478689678f85 Mon Sep 17 00:00:00 2001 From: Sarah Read-Brown Date: Tue, 19 Dec 2017 16:34:51 -0800 Subject: [PATCH 7/7] fixes bud with succes response render --- app/controllers/movies_controller.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 54c2fc0f..2bc2d86b 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -21,8 +21,10 @@ def create else new_movie = MovieWrapper.construct_movie(params) new_movie.save - render status: :ok, json: data + render status: :ok, json: new_movie end + + end def show