From ae64ee9564e32c1ee35d276360eefea764a39053 Mon Sep 17 00:00:00 2001 From: Nora Peters Date: Mon, 18 Jun 2018 16:59:37 -0700 Subject: [PATCH 1/8] add env --- env | 1 + 1 file changed, 1 insertion(+) create mode 100644 env diff --git a/env b/env new file mode 100644 index 00000000..a311db0d --- /dev/null +++ b/env @@ -0,0 +1 @@ +MOVIEDB_KEY: 5d0f342048d3fbd9e1f43ab551fb6368 From 6facfcb694354a34028886ef9056b8467d63a28b Mon Sep 17 00:00:00 2001 From: Nora Peters Date: Mon, 18 Jun 2018 17:37:39 -0700 Subject: [PATCH 2/8] fixed env file issue and reseeded --- env | 1 - 1 file changed, 1 deletion(-) diff --git a/env b/env index a311db0d..e69de29b 100644 --- a/env +++ b/env @@ -1 +0,0 @@ -MOVIEDB_KEY: 5d0f342048d3fbd9e1f43ab551fb6368 From fdc0e949bff81046a7a21e081e95751580a76326 Mon Sep 17 00:00:00 2001 From: Nora Peters Date: Mon, 18 Jun 2018 17:38:17 -0700 Subject: [PATCH 3/8] removes old env file --- env | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 env diff --git a/env b/env deleted file mode 100644 index e69de29b..00000000 From 408652d3460f5c208d0dfe0b0a2e281dd35d5487 Mon Sep 17 00:00:00 2001 From: Nora Peters Date: Tue, 19 Jun 2018 13:57:40 -0700 Subject: [PATCH 4/8] adds movies#create that calls wrapper construct_movie method. fixes cors error with rack-cors gem and middleware in config --- Gemfile | 1 + Gemfile.lock | 2 ++ app/controllers/movies_controller.rb | 14 ++++++++++++++ config/application.rb | 10 ++++++---- config/routes.rb | 3 ++- 5 files changed, 25 insertions(+), 5 deletions(-) diff --git a/Gemfile b/Gemfile index 009415af..7f16215c 100644 --- a/Gemfile +++ b/Gemfile @@ -5,6 +5,7 @@ git_source(:github) do |repo_name| "https://github.com/#{repo_name}.git" end +gem 'rack-cors', require: 'rack/cors' gem 'awesome_print' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 5.0.1' diff --git a/Gemfile.lock b/Gemfile.lock index 9f7601a8..0f6ee0ec 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -120,6 +120,7 @@ GEM pry (>= 0.9.10) puma (3.6.2) rack (2.0.1) + rack-cors (1.0.2) rack-test (0.6.3) rack (>= 1.0) rails (5.0.1) @@ -210,6 +211,7 @@ DEPENDENCIES minitest-spec-rails pry-rails puma (~> 3.0) + rack-cors rails (~> 5.0.1) sass-rails (~> 5.0) spring diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 362e2791..a6eba90f 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -21,6 +21,16 @@ def show ) end + def create + movie = MovieWrapper.construct_movie(movie_params) + if movie.save + render json: movie.as_json(only: [:id]), status: :ok + else + render json: { ok: false, errors: movie.errors }, + status: :bad_request + end + end + private def require_movie @@ -29,4 +39,8 @@ def require_movie render status: :not_found, json: { errors: { title: ["No movie with title #{params["title"]}"] } } end end + + def movie_params + return params.permit(:title, :overview, :release_date, :image_url, :external_id) + end end diff --git a/config/application.rb b/config/application.rb index cc803322..0e371183 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' => '*', - '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] + end + end end end diff --git a/config/routes.rb b/config/routes.rb index 54bf033e..c0b35bbb 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,10 +3,11 @@ 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" From eb181572063fa943db94772514a958e3eaebf6a1 Mon Sep 17 00:00:00 2001 From: Monalisa Chatterjee Date: Wed, 20 Jun 2018 13:07:29 -0700 Subject: [PATCH 5/8] Fixed bug related to image_url, poster_path --- Gemfile.lock | 2 +- lib/movie_wrapper.rb | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 0f6ee0ec..fcb76c2a 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-cors (1.0.2) rack-test (0.6.3) diff --git a/lib/movie_wrapper.rb b/lib/movie_wrapper.rb index 7bd05c0e..62ef0206 100644 --- a/lib/movie_wrapper.rb +++ b/lib/movie_wrapper.rb @@ -14,6 +14,7 @@ def self.search(query) return [] else movies = response["results"].map do |result| + result["image_url"] = result["poster_path"] self.construct_movie(result) end return movies @@ -27,7 +28,7 @@ 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["image_url"], external_id: api_result["id"]) end From 7fd081cfe5b46f50bd5ebb33ee3ec6504bb7baf3 Mon Sep 17 00:00:00 2001 From: Monalisa Chatterjee Date: Wed, 20 Jun 2018 13:10:25 -0700 Subject: [PATCH 6/8] Add validation for title --- app/models/movie.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/models/movie.rb b/app/models/movie.rb index 0016080b..85113eb9 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -1,9 +1,11 @@ class Movie < ApplicationRecord has_many :rentals has_many :customers, through: :rentals + validates :title, uniqueness: true + # validates :overview, uniqueness: { scope: :title } def available_inventory - self.inventory - Rental.where(movie: self, returned: false).length + self.inventory - Rental.where(movie: self, returned: [false, nil]).length end def image_url From 1e22cb4583a45705fdf4268df424c176ac85502d Mon Sep 17 00:00:00 2001 From: Monalisa Chatterjee Date: Wed, 20 Jun 2018 14:06:37 -0700 Subject: [PATCH 7/8] refactored movies_checked_out_count --- app/models/customer.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/customer.rb b/app/models/customer.rb index 6fc89447..6d9cc567 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -3,6 +3,6 @@ class Customer < ApplicationRecord has_many :movies, through: :rentals def movies_checked_out_count - self.rentals.where(returned: false).length + self.rentals.where(returned: [false, nil]).length end end From 426d05d89afc9ebacfa563110594da2c768b76b2 Mon Sep 17 00:00:00 2001 From: Monalisa Chatterjee Date: Fri, 22 Jun 2018 10:47:16 -0700 Subject: [PATCH 8/8] Fixed inventory error --- app/models/movie.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/models/movie.rb b/app/models/movie.rb index 85113eb9..3984c1d3 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -5,6 +5,7 @@ class Movie < ApplicationRecord # validates :overview, uniqueness: { scope: :title } def available_inventory + return 0 if self.inventory.nil? self.inventory - Rental.where(movie: self, returned: [false, nil]).length end