From 1a143944fe0a2bf94800aaf5ca3426b9c2565e6a Mon Sep 17 00:00:00 2001 From: Chantelle Belic Date: Mon, 17 Dec 2018 15:16:49 -0800 Subject: [PATCH 01/13] env --- .gitignore | 2 ++ lib/movie_wrapper.rb | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 4a494a75..614c3613 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,5 @@ # Ignore master key for decrypting credentials and more. /config/master.key + +.env diff --git a/lib/movie_wrapper.rb b/lib/movie_wrapper.rb index c51d05ee..e557a85f 100644 --- a/lib/movie_wrapper.rb +++ b/lib/movie_wrapper.rb @@ -1,4 +1,4 @@ - +require 'pry' class MovieWrapper BASE_URL = "https://api.themoviedb.org/3/" KEY = ENV["MOVIEDB_KEY"] @@ -8,6 +8,7 @@ class MovieWrapper DEFAULT_IMG_URL = "http://lorempixel.com/185/278/" def self.search(query) + url = BASE_URL + "search/movie?api_key=" + KEY + "&query=" + query puts url # puts url @@ -15,6 +16,7 @@ def self.search(query) if response["total_results"] == 0 return [] else + puts response movies = response["results"].map do |result| self.construct_movie(result) From 9b37ef1f98b9a944bb5bd63f24ee3e05da6fbce8 Mon Sep 17 00:00:00 2001 From: Chantelle Belic Date: Tue, 18 Dec 2018 14:54:15 -0800 Subject: [PATCH 02/13] working create movie from external api, add to db --- app/controllers/movies_controller.rb | 18 +++++++++++++++++- config/routes.rb | 1 + lib/movie_wrapper.rb | 24 +++++++++++++++++++----- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 362e2791..75fe9565 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -17,8 +17,24 @@ def show json: @movie.as_json( only: [:title, :overview, :release_date, :inventory], methods: [:available_inventory] - ) ) + ) + end + + def create + movie = MovieWrapper.get_movie(params[:id]) + + if movie + movie.save + render( + status: :ok, json: movie.as_json( only: [:title] ) + ) + else + #errors + render( + status: :bad_request, json: {errors: {movie: "bad data, somehow"}} + ) + end end private diff --git a/config/routes.rb b/config/routes.rb index f4c99688..edc96f4b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,6 +4,7 @@ resources :customers, only: [:index] resources :movies, only: [:index, :show], param: :title + resources :movies, only: [:create], param: :id post "/rentals/:title/check-out", to: "rentals#check_out", as: "check_out" post "/rentals/:title/return", to: "rentals#check_in", as: "check_in" diff --git a/lib/movie_wrapper.rb b/lib/movie_wrapper.rb index e557a85f..482db9c9 100644 --- a/lib/movie_wrapper.rb +++ b/lib/movie_wrapper.rb @@ -10,6 +10,7 @@ class MovieWrapper def self.search(query) url = BASE_URL + "search/movie?api_key=" + KEY + "&query=" + query + puts url # puts url response = HTTParty.get(url) @@ -25,6 +26,19 @@ def self.search(query) end end + def self.get_movie(id) + + url = BASE_URL + "movie/" + id + "?api_key=" + KEY + + response = HTTParty.get(url) + + if response["id"] + return self.construct_movie(response) + else + return nil + end + end + private def self.construct_movie(api_result) @@ -34,10 +48,10 @@ def self.construct_movie(api_result) 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), external_id: api_result["id"]) - end + end - def self.construct_image_url(img_name) - return BASE_IMG_URL + DEFAULT_IMG_SIZE + img_name - end + def self.construct_image_url(img_name) + return BASE_IMG_URL + DEFAULT_IMG_SIZE + img_name + end -end + end From f372d1d39cc3ea9e6f0a1adf41237ce254a4abde Mon Sep 17 00:00:00 2001 From: Anibel America Date: Tue, 18 Dec 2018 15:15:59 -0800 Subject: [PATCH 03/13] replaces url in move_wrapper --- lib/movie_wrapper.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/movie_wrapper.rb b/lib/movie_wrapper.rb index 482db9c9..57ab40b7 100644 --- a/lib/movie_wrapper.rb +++ b/lib/movie_wrapper.rb @@ -28,8 +28,7 @@ def self.search(query) def self.get_movie(id) - url = BASE_URL + "movie/" + id + "?api_key=" + KEY - + url = "#{BASE_URL}movie/#{id}?api_key=#{KEY}" response = HTTParty.get(url) if response["id"] From 61354be7de007aacafd3b7402af8f93fb4514418 Mon Sep 17 00:00:00 2001 From: Chantelle Belic Date: Wed, 19 Dec 2018 21:29:41 -0800 Subject: [PATCH 04/13] added post customer path --- app/controllers/customers_controller.rb | 16 ++++++++++++++++ config/routes.rb | 1 + 2 files changed, 17 insertions(+) diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index be25f1be..b4924d6e 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -18,6 +18,22 @@ def index ) end + def create + + customer = Customer.new(name: params[:name]) + + if customer.save + render( + status: :ok, json: customer.as_json( only: [:name] ) + ) + else + #errors + render( + status: :bad_request, json: {errors: {customer: "bad data, somehow"}} + ) + end + end + private def parse_query_args errors = {} diff --git a/config/routes.rb b/config/routes.rb index edc96f4b..e316bf81 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,6 +2,7 @@ # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html resources :customers, only: [:index] + resources :customers, only: [:create], param: :name resources :movies, only: [:index, :show], param: :title resources :movies, only: [:create], param: :id From 0e3334bf1054cb2a4fb13ca129ba0925f857b5fe Mon Sep 17 00:00:00 2001 From: Chantelle Belic Date: Wed, 19 Dec 2018 21:43:25 -0800 Subject: [PATCH 05/13] added strong params to customer nw --- app/controllers/customers_controller.rb | 9 +++++++-- config/routes.rb | 4 ++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index b4924d6e..3f65fae0 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -20,7 +20,7 @@ def index def create - customer = Customer.new(name: params[:name]) + customer = Customer.new(customer_params) if customer.save render( @@ -34,7 +34,12 @@ def create end end -private + private + + def customer_params + params.permit(:name, :registered_at, :address, :city, :state, :postal_code, :phone, :account_credit) + end + def parse_query_args errors = {} @sort = params[:sort] diff --git a/config/routes.rb b/config/routes.rb index e316bf81..2962870e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,8 +1,8 @@ Rails.application.routes.draw do # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html - resources :customers, only: [:index] - resources :customers, only: [:create], param: :name + resources :customers, only: [:index, :create] + # resources :customers, only: [:create] resources :movies, only: [:index, :show], param: :title resources :movies, only: [:create], param: :id From 5d0b9daeb860b42f886bbbf4493b0bfa9304a116 Mon Sep 17 00:00:00 2001 From: Anibel America Date: Thu, 20 Dec 2018 10:26:10 -0800 Subject: [PATCH 06/13] changes json response when attempting to add an already entered movie into the rental library --- app/controllers/movies_controller.rb | 27 ++++++++++++++++----------- app/models/movie.rb | 1 + 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 75fe9565..35c722aa 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -22,18 +22,23 @@ def show end def create - movie = MovieWrapper.get_movie(params[:id]) - - if movie - movie.save - render( - status: :ok, json: movie.as_json( only: [:title] ) - ) + movie_exists = Movie.find_by(external_id: params[:id]) + if movie_exists + render status: :bad_request, json: {errors: {movie: "Movie already exists in library"}} else - #errors - render( - status: :bad_request, json: {errors: {movie: "bad data, somehow"}} - ) + movie = MovieWrapper.get_movie(params[:id]) + + if movie + movie.save + render( + status: :ok, json: movie.as_json( only: [:title] ) + ) + else + #errors + render( + status: :bad_request, json: {errors: {movie: "bad data, somehow"}} + ) + end end end diff --git a/app/models/movie.rb b/app/models/movie.rb index 0016080b..f9f47207 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -1,6 +1,7 @@ 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 From 6102755171db912d142c121a2c7b68935d954a9b Mon Sep 17 00:00:00 2001 From: Anibel America Date: Thu, 20 Dec 2018 10:47:43 -0800 Subject: [PATCH 07/13] updates error message for movies controller --- .elasticbeanstalk/config.yml | 2 ++ app/controllers/movies_controller.rb | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 .elasticbeanstalk/config.yml diff --git a/.elasticbeanstalk/config.yml b/.elasticbeanstalk/config.yml new file mode 100644 index 00000000..4102d904 --- /dev/null +++ b/.elasticbeanstalk/config.yml @@ -0,0 +1,2 @@ +global: + profile: eb-cli diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 35c722aa..bde2d437 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -36,7 +36,7 @@ def create else #errors render( - status: :bad_request, json: {errors: {movie: "bad data, somehow"}} + status: :bad_request, json: {errors: {id: 'must be present'}} ) end end From d54e6eac9bf85186514af42033278b7aba563854 Mon Sep 17 00:00:00 2001 From: Chantelle Belic Date: Thu, 20 Dec 2018 10:49:02 -0800 Subject: [PATCH 08/13] customers errors --- app/controllers/customers_controller.rb | 2 +- app/models/customer.rb | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index 3f65fae0..913d6d39 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -29,7 +29,7 @@ def create else #errors render( - status: :bad_request, json: {errors: {customer: "bad data, somehow"}} + status: :bad_request, json: {errors: {customer: customer.errors.messages }} ) end end diff --git a/app/models/customer.rb b/app/models/customer.rb index 6fc89447..6f284716 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -1,6 +1,7 @@ class Customer < ApplicationRecord has_many :rentals has_many :movies, through: :rentals + validates :name, presence: true def movies_checked_out_count self.rentals.where(returned: false).length From 0d18b9f9f8c9c13a2ba06c96415627440bf8117f Mon Sep 17 00:00:00 2001 From: Anibel America Date: Thu, 20 Dec 2018 11:06:03 -0800 Subject: [PATCH 09/13] removes pry --- Gemfile | 1 - Gemfile.lock | 9 +-------- lib/movie_wrapper.rb | 1 - 3 files changed, 1 insertion(+), 10 deletions(-) diff --git a/Gemfile b/Gemfile index 2d53ba9c..fa19b64a 100644 --- a/Gemfile +++ b/Gemfile @@ -52,7 +52,6 @@ end gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] group :development, :test do - gem 'pry-rails' gem 'dotenv-rails' end diff --git a/Gemfile.lock b/Gemfile.lock index a0a07df1..543ce8d7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -56,7 +56,6 @@ GEM byebug (10.0.2) case_transform (0.2) activesupport - coderay (1.1.2) concurrent-ruby (1.1.4) crass (1.0.4) dotenv (2.5.0) @@ -110,11 +109,6 @@ GEM nokogiri (1.8.5) mini_portile2 (~> 2.3.0) pg (1.1.3) - pry (0.12.2) - coderay (~> 1.1.0) - method_source (~> 0.9.0) - pry-rails (0.3.8) - pry (>= 0.10.4) puma (3.12.0) rack (2.0.6) rack-cors (1.0.2) @@ -186,7 +180,6 @@ DEPENDENCIES minitest-rails minitest-reporters pg (>= 0.18, < 2.0) - pry-rails puma (~> 3.11) rack-cors rails (~> 5.2.0) @@ -199,4 +192,4 @@ RUBY VERSION ruby 2.5.1p57 BUNDLED WITH - 1.16.6 + 1.17.2 diff --git a/lib/movie_wrapper.rb b/lib/movie_wrapper.rb index 57ab40b7..3ca024e3 100644 --- a/lib/movie_wrapper.rb +++ b/lib/movie_wrapper.rb @@ -1,4 +1,3 @@ -require 'pry' class MovieWrapper BASE_URL = "https://api.themoviedb.org/3/" KEY = ENV["MOVIEDB_KEY"] From a62bfc9796c8aaa2f642cc125081d7523e39bca6 Mon Sep 17 00:00:00 2001 From: Chantelle Belic Date: Fri, 21 Dec 2018 11:05:25 -0800 Subject: [PATCH 10/13] redeploy db to heroku --- app/controllers/customers_controller.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index 913d6d39..0a6366fb 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -18,13 +18,14 @@ def index ) end + def create customer = Customer.new(customer_params) if customer.save render( - status: :ok, json: customer.as_json( only: [:name] ) + status: :ok, json: customer.as_json( only: [:name, :id] ) ) else #errors From 3f85e1e62cd3b9ce954ba9f9d25746d24c6652fe Mon Sep 17 00:00:00 2001 From: Anibel America Date: Fri, 21 Dec 2018 11:16:27 -0800 Subject: [PATCH 11/13] comments out pagination line --- app/controllers/customers_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index 0a6366fb..7c6d043e 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -10,7 +10,7 @@ def index data = Customer.all end - data = data.paginate(page: params[:p], per_page: params[:n]) + # data = data.paginate(page: params[:p], per_page: params[:n]) render json: data.as_json( only: [:id, :name, :registered_at, :address, :city, :state, :postal_code, :phone, :account_credit], From 9dda89f7426904838882b8ebd46600e43c15a829 Mon Sep 17 00:00:00 2001 From: Anibel America Date: Fri, 21 Dec 2018 11:19:24 -0800 Subject: [PATCH 12/13] uncomments pagination line --- app/controllers/customers_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index 7c6d043e..0a6366fb 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -10,7 +10,7 @@ def index data = Customer.all end - # data = data.paginate(page: params[:p], per_page: params[:n]) + data = data.paginate(page: params[:p], per_page: params[:n]) render json: data.as_json( only: [:id, :name, :registered_at, :address, :city, :state, :postal_code, :phone, :account_credit], From fc8bcde77c60b6555623eaa47e3807246a3ec525 Mon Sep 17 00:00:00 2001 From: Anibel America Date: Fri, 21 Dec 2018 11:25:23 -0800 Subject: [PATCH 13/13] reverts back to previous version --- app/controllers/customers_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index 0a6366fb..7c6d043e 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -10,7 +10,7 @@ def index data = Customer.all end - data = data.paginate(page: params[:p], per_page: params[:n]) + # data = data.paginate(page: params[:p], per_page: params[:n]) render json: data.as_json( only: [:id, :name, :registered_at, :address, :city, :state, :postal_code, :phone, :account_credit],