From 845a7db63426385212263407a9197d583347c235 Mon Sep 17 00:00:00 2001 From: Cheeron Mars Date: Mon, 18 Jun 2018 15:37:43 -0700 Subject: [PATCH 1/9] changes to gemfile lock --- Gemfile.lock | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 9f7601a8..294153e1 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-test (0.6.3) rack (>= 1.0) @@ -222,4 +222,4 @@ DEPENDENCIES will_paginate BUNDLED WITH - 1.16.1 + 1.16.2 From 63b5b0ffdfd4921c551feea2b55bb5b25dbecb96 Mon Sep 17 00:00:00 2001 From: Cheeron Mars Date: Mon, 18 Jun 2018 15:38:29 -0700 Subject: [PATCH 2/9] added DS_Store to gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index ce865237..5bc6e37c 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,4 @@ # Ignore Byebug command history file. .byebug_history .env +.DS_Store From b6d0126293eee49f54c0e9fb46c4f37fa9b76bca Mon Sep 17 00:00:00 2001 From: Kaitlin Forsman Date: Mon, 18 Jun 2018 15:38:55 -0700 Subject: [PATCH 3/9] updates from setup --- Gemfile.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile.lock b/Gemfile.lock index 9f7601a8..22779079 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-test (0.6.3) rack (>= 1.0) From e33ed59aa69f8ecdcc65b4d0ce7c01f21336c939 Mon Sep 17 00:00:00 2001 From: Kaitlin Forsman Date: Tue, 19 Jun 2018 15:04:10 -0700 Subject: [PATCH 4/9] changed due date to be set in the check-out action ofthe rental controller and installed rack-cors to deal with access-control-allow-origin issue --- Gemfile | 2 ++ Gemfile.lock | 2 ++ app/controllers/rentals_controller.rb | 2 +- config/application.rb | 7 +++++++ 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 009415af..e0ec0062 100644 --- a/Gemfile +++ b/Gemfile @@ -5,6 +5,8 @@ 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 294153e1..75617a53 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -120,6 +120,7 @@ GEM pry (>= 0.9.10) puma (3.11.4) 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/rentals_controller.rb b/app/controllers/rentals_controller.rb index 67e77073..734bda9c 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -4,7 +4,7 @@ class RentalsController < ApplicationController # TODO: make sure that wave 2 works all the way def check_out - rental = Rental.new(movie: @movie, customer: @customer, due_date: params[:due_date]) + rental = Rental.new(movie: @movie, customer: @customer, due_date: Date.today + 7) if rental.save render status: :ok, json: {} diff --git a/config/application.rb b/config/application.rb index cc803322..f0638b57 100644 --- a/config/application.rb +++ b/config/application.rb @@ -19,5 +19,12 @@ class Application < Rails::Application '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 From aacf87a14379ef0b196d73cd587c1c0ea568e289 Mon Sep 17 00:00:00 2001 From: Cheeron Mars Date: Thu, 21 Jun 2018 13:30:27 -0700 Subject: [PATCH 5/9] add route and method to add movie --- app/controllers/movies_controller.rb | 18 +++++++++++++++++- config/routes.rb | 2 +- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 362e2791..3a6459f7 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -17,10 +17,22 @@ def show json: @movie.as_json( only: [:title, :overview, :release_date, :inventory], methods: [:available_inventory] - ) ) + ) end + + def create + movie = Movie.new(movie_params) + if movie.save + render json: movie.as_json, status: :ok + else + render json: { ok: false, errors: movie.errors }, + status: :bad_request + end + end + + private def require_movie @@ -29,4 +41,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/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 6af42a13b52755dd6b00c0699129cfd07ea4c22c Mon Sep 17 00:00:00 2001 From: Kaitlin Forsman Date: Thu, 21 Jun 2018 17:22:29 -0700 Subject: [PATCH 6/9] fixed problem with images for added movies from frontend app compared to seeded images --- 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 3a6459f7..9f98f6bf 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -43,6 +43,6 @@ def require_movie end def movie_params - return params.permit(:title, :overview, :release_date, :image_url, :external_id) + return params.permit(:title, :overview, :release_date, :image_url, :external_id, :inventory) end end diff --git a/app/models/movie.rb b/app/models/movie.rb index 0016080b..ba13535d 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?(MovieWrapper::BASE_IMG_URL) MovieWrapper.construct_image_url(orig_value) else orig_value From c72851c95ae15495ad3dd6f060b38abb68b2bb04 Mon Sep 17 00:00:00 2001 From: Kaitlin Forsman Date: Thu, 21 Jun 2018 21:11:42 -0700 Subject: [PATCH 7/9] added logic to account for the available inventory for the json response to request for all the database records of Movie, and setting the rental checkout method to set a new rental's returned attribute to false --- app/controllers/rentals_controller.rb | 3 +-- app/serializers/movie_serializer.rb | 1 + 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 734bda9c..2aa82f90 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -4,8 +4,7 @@ class RentalsController < ApplicationController # TODO: make sure that wave 2 works all the way def check_out - rental = Rental.new(movie: @movie, customer: @customer, due_date: Date.today + 7) - + rental = Rental.new(movie: @movie, customer: @customer, due_date: Date.today + 7, returned: false) if rental.save render status: :ok, json: {} else diff --git a/app/serializers/movie_serializer.rb b/app/serializers/movie_serializer.rb index 8a1cdc35..194cc45f 100644 --- a/app/serializers/movie_serializer.rb +++ b/app/serializers/movie_serializer.rb @@ -1,5 +1,6 @@ class MovieSerializer < ActiveModel::Serializer attribute :id, if: -> { object.id != nil } + attribute :available_inventory, if: -> { object.id != nil } attributes :title, :overview, :release_date, :image_url, :external_id end From 4bbd4e96097e673bf4f5ddf09f676e4b562a2746 Mon Sep 17 00:00:00 2001 From: Cheeron Mars Date: Thu, 21 Jun 2018 21:22:16 -0700 Subject: [PATCH 8/9] prevent duplicate external_id's from being added to rental library --- app/controllers/movies_controller.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 3a6459f7..b72c4835 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -23,7 +23,14 @@ def show def create - movie = Movie.new(movie_params) + movie = Movie.find_by(external_id: params[:external_id]) + if !movie + movie = Movie.new(movie_params) + else + render json: { ok: false, errors: movie.errors }, + status: :bad_request + end + if movie.save render json: movie.as_json, status: :ok else From 9c1646f0d31a82c27dfe6be54a95d69aeb0f37c7 Mon Sep 17 00:00:00 2001 From: Kaitlin Forsman Date: Fri, 22 Jun 2018 11:01:22 -0700 Subject: [PATCH 9/9] fixed minor bug for photo url --- app/models/movie.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/movie.rb b/app/models/movie.rb index ba13535d..a8e77710 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 && !orig_value.include?(MovieWrapper::BASE_IMG_URL) + elsif external_id && !orig_value.include?("http") MovieWrapper.construct_image_url(orig_value) else orig_value