From 09e00f58fd6c35f355f6893a3ae56e8a512475b1 Mon Sep 17 00:00:00 2001 From: Brenda Rios Date: Mon, 18 Jun 2018 12:37:18 -0700 Subject: [PATCH 1/8] Doing installation of project. --- 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 2eaebbbc0dc067aa1cde6895af5a72916324a528 Mon Sep 17 00:00:00 2001 From: Brenda Rios Date: Tue, 19 Jun 2018 14:30:26 -0700 Subject: [PATCH 2/8] Added create method for movies and add it in the routes. --- 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..952b926a 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -11,6 +11,16 @@ def index render status: :ok, json: data end + + def create + movie = Movie.new(movie_params) + if movie.save + render json: {id: movie.id} + else + render json: {errors: movie.errors.messages},status: :bad_request + end + end + def show render( status: :ok, @@ -23,6 +33,11 @@ def show private + + def movie_params + params.permit(:title, :overview, :inventory, :release_date) + end + def require_movie @movie = Movie.find_by(title: params[:title]) unless @movie diff --git a/config/routes.rb b/config/routes.rb index 54bf033e..1b227184 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 af31ed7f04a4d8c5c72dbd04a2cb10ef55dc5173 Mon Sep 17 00:00:00 2001 From: Brenda Rios Date: Tue, 19 Jun 2018 14:30:39 -0700 Subject: [PATCH 3/8] Added gem rack-cors. --- Gemfile | 2 ++ Gemfile.lock | 2 ++ config/application.rb | 10 ++++++---- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Gemfile b/Gemfile index 009415af..dc74851b 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 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 From 770820e79901a11ccb7b4001ae51fdfc032d170e Mon Sep 17 00:00:00 2001 From: Brenda Rios Date: Tue, 19 Jun 2018 15:49:54 -0700 Subject: [PATCH 4/8] added create method for movies and gem rack-cors. --- Gemfile | 1 - app/controllers/movies_controller.rb | 4 ++-- config/application.rb | 1 + 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Gemfile b/Gemfile index dc74851b..dc061ed6 100644 --- a/Gemfile +++ b/Gemfile @@ -38,7 +38,6 @@ 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/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 952b926a..10e8110f 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -35,9 +35,9 @@ def show def movie_params - params.permit(:title, :overview, :inventory, :release_date) + params.permit(:title, :overview, :inventory, :release_date, :external_id, :image_url) end - + def require_movie @movie = Movie.find_by(title: params[:title]) unless @movie diff --git a/config/application.rb b/config/application.rb index 0e371183..0827fa4c 100644 --- a/config/application.rb +++ b/config/application.rb @@ -21,5 +21,6 @@ class Application < Rails::Application resource '*', headers: :any, methods: [:get, :post, :options] end end + end end From 73b625e82388509cf637be9aaac848a3a72e3637 Mon Sep 17 00:00:00 2001 From: Winifred Irarrazaval Date: Tue, 19 Jun 2018 15:50:39 -0700 Subject: [PATCH 5/8] changes --- 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 c7cef134315b6144512045b5f91fbd5f5a9ddaf0 Mon Sep 17 00:00:00 2001 From: Winifred Irarrazaval Date: Tue, 19 Jun 2018 16:55:47 -0700 Subject: [PATCH 6/8] able to make a rental --- app/controllers/rentals_controller.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 67e77073..da515857 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -9,6 +9,7 @@ def check_out if rental.save render status: :ok, json: {} else + puts rental.errors.messages render status: :bad_request, json: { errors: rental.errors.messages } end end From cf6d8325b80b7350970ab2fdaa5a86b6215fe39d Mon Sep 17 00:00:00 2001 From: Winifred Irarrazaval Date: Wed, 20 Jun 2018 14:58:58 -0700 Subject: [PATCH 7/8] added validations for external id --- app/controllers/movies_controller.rb | 2 ++ app/models/movie.rb | 2 ++ 2 files changed, 4 insertions(+) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 10e8110f..2765b07d 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -1,3 +1,5 @@ +require "pry" + class MoviesController < ApplicationController before_action :require_movie, only: [:show] diff --git a/app/models/movie.rb b/app/models/movie.rb index 0016080b..25ba580a 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -2,6 +2,8 @@ class Movie < ApplicationRecord has_many :rentals has_many :customers, through: :rentals + + validates :external_id, uniqueness: true def available_inventory self.inventory - Rental.where(movie: self, returned: false).length end From b7882d03f4a74fe60927c6e8b53c445b6f2cc638 Mon Sep 17 00:00:00 2001 From: Winifred Irarrazaval Date: Fri, 22 Jun 2018 11:12:24 -0700 Subject: [PATCH 8/8] fixed image url in movie model --- 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 25ba580a..e14e2d56 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -12,7 +12,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