From 4c4f7ae13c16c74a47ad3b7a4eda8ab0b7350476 Mon Sep 17 00:00:00 2001 From: LetiTran Date: Mon, 18 Jun 2018 15:43:21 -0700 Subject: [PATCH 1/7] Setting up DB --- 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 90de80007d8c1ba99d7bed715e26cebb8f6d7c43 Mon Sep 17 00:00:00 2001 From: LetiTran Date: Wed, 20 Jun 2018 14:26:05 -0700 Subject: [PATCH 2/7] Adds CORS Fix for 'unauthorized'problem given on Chrome for POST requests since it has the same origin. Also modified routes for easier access with our react app. --- Gemfile | 2 ++ Gemfile.lock | 2 ++ config/application.rb | 10 ++++++---- config/routes.rb | 2 +- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/Gemfile b/Gemfile index 009415af..7ddb7130 100644 --- a/Gemfile +++ b/Gemfile @@ -71,3 +71,5 @@ gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] gem 'httparty' gem "active_model_serializers" + + gem 'rack-cors', require: 'rack/cors' diff --git a/Gemfile.lock b/Gemfile.lock index 22779079..fcb76c2a 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/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..d6898a42 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,7 +5,7 @@ resources :movies, only: [:index, :show], param: :title - post "/rentals/:title/check-out", to: "rentals#check_out", as: "check_out" + post "/rentals/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 94aba282c92aa44c546d8d3798b887d9d530d138 Mon Sep 17 00:00:00 2001 From: Kate Pond Date: Wed, 20 Jun 2018 15:24:51 -0700 Subject: [PATCH 3/7] adds functionality to add movie to library --- app/controllers/movies_controller.rb | 15 +++++++++++++++ config/routes.rb | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 362e2791..839a7ec1 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -11,6 +11,17 @@ def index render status: :ok, json: data end + def create + @movie = Movie.new(movie_params) + + if @movie.save + render status: :ok, json: @movie + else + render status: 500, json: { errors: { title: ["Did not save movie to library"] } } + end + + end + def show render( status: :ok, @@ -23,6 +34,10 @@ def show private + def movie_params + return params.permit(:title, :overview, :release_date, :image_url, :external_id) + end + def require_movie @movie = Movie.find_by(title: params[:title]) unless @movie diff --git a/config/routes.rb b/config/routes.rb index d6898a42..55a751a3 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/check-out", to: "rentals#check_out", as: "check_out" post "/rentals/:title/return", to: "rentals#check_in", as: "check_in" From 4b8d14dca7ae077cc2fc36b73ebda073231fb37c Mon Sep 17 00:00:00 2001 From: LetiTran Date: Thu, 21 Jun 2018 17:09:06 -0700 Subject: [PATCH 4/7] Adds conditional for creating a new Movie instance in our library - if it already exists it will only increment inventory intead of creating a new one. --- app/controllers/movies_controller.rb | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 839a7ec1..e020c008 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -12,14 +12,24 @@ def index end def create - @movie = Movie.new(movie_params) + # Search for movie in our library: + @movie = Movie.find_by(title: params[:title]) + + if !@movie + # If movie doesnt already exists in our library: + puts "MOOOOVIE: #{@movie}" + @movie = Movie.new(movie_params) + @movie.inventory = 1 + else + # If movie already exists: + @movie.inventory += 1 + end if @movie.save render status: :ok, json: @movie else render status: 500, json: { errors: { title: ["Did not save movie to library"] } } end - end def show @@ -28,14 +38,14 @@ def show json: @movie.as_json( only: [:title, :overview, :release_date, :inventory], methods: [:available_inventory] - ) ) + ) end private 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) end def require_movie From 07cbf4d6c8c827ae9c9f64f5bb834ca878df9394 Mon Sep 17 00:00:00 2001 From: LetiTran Date: Thu, 21 Jun 2018 18:24:46 -0700 Subject: [PATCH 5/7] Deleted puts that was being used for debugging earlier. --- app/controllers/movies_controller.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index e020c008..e71a5485 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -17,7 +17,6 @@ def create if !@movie # If movie doesnt already exists in our library: - puts "MOOOOVIE: #{@movie}" @movie = Movie.new(movie_params) @movie.inventory = 1 else From db677356db4847503e9282c01d99699524437e77 Mon Sep 17 00:00:00 2001 From: Kate Pond Date: Thu, 21 Jun 2018 19:02:02 -0700 Subject: [PATCH 6/7] includes inventory to api --- app/serializers/movie_serializer.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/serializers/movie_serializer.rb b/app/serializers/movie_serializer.rb index 8a1cdc35..c1871302 100644 --- a/app/serializers/movie_serializer.rb +++ b/app/serializers/movie_serializer.rb @@ -1,5 +1,5 @@ class MovieSerializer < ActiveModel::Serializer attribute :id, if: -> { object.id != nil } - attributes :title, :overview, :release_date, :image_url, :external_id + attributes :title, :overview, :release_date, :image_url, :external_id, :inventory end From 873f7a4b3bde235cb7e4525b665a64bd2edd339f Mon Sep 17 00:00:00 2001 From: LetiTran Date: Thu, 21 Jun 2018 22:55:02 -0700 Subject: [PATCH 7/7] Fix checked-out movie count to display correclty in front-end --- app/models/customer.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/models/customer.rb b/app/models/customer.rb index 6fc89447..d7f4deca 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -3,6 +3,7 @@ class Customer < ApplicationRecord has_many :movies, through: :rentals def movies_checked_out_count - self.rentals.where(returned: false).length + # self.rentals.where(returned: false).length + self.rentals.count end end