From bd6188a54ddeaf1f2b0e005c00e5fe402e2c2813 Mon Sep 17 00:00:00 2001 From: Aurora Lemieux Date: Tue, 13 Jun 2017 14:57:16 -0700 Subject: [PATCH 01/12] testing to make sure partner is synched --- lib/movie_wrapper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/movie_wrapper.rb b/lib/movie_wrapper.rb index 7bd05c0e..dd13a871 100644 --- a/lib/movie_wrapper.rb +++ b/lib/movie_wrapper.rb @@ -1,7 +1,7 @@ class MovieWrapper BASE_URL = "https://api.themoviedb.org/3/" KEY = ENV["MOVIEDB_KEY"] - +# this is a comment BASE_IMG_URL = "https://image.tmdb.org/t/p/" DEFAULT_IMG_SIZE = "w185" DEFAULT_IMG_URL = "http://lorempixel.com/185/278/" From 4f00a77e479d64c7f41189c32cead121784c5d20 Mon Sep 17 00:00:00 2001 From: Aurora Lemieux Date: Tue, 13 Jun 2017 15:46:00 -0700 Subject: [PATCH 02/12] added create action for movies and corresponding route --- app/controllers/movies_controller.rb | 11 +++++++++++ config/routes.rb | 1 + lib/movie_wrapper.rb | 1 + 3 files changed, 13 insertions(+) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 362e2791..5c09839a 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -21,6 +21,17 @@ def show ) end + def create + movie = Movie.new(title: params[:title], overview: params[:overview], release_date: params[:release_date], inventory: params[:inventory], image_url: params[:image_url]) + + if movie.save + render json: {id: movie.id}, status: :ok + else + render json: { errors: movie.errors.messages }, status: :bad_request + end + + end + private def require_movie diff --git a/config/routes.rb b/config/routes.rb index 54bf033e..b81406ce 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,6 +4,7 @@ resources :customers, only: [:index] resources :movies, only: [:index, :show], param: :title + post "/movies", to: "movies#create" 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 dd13a871..4dc7cbb4 100644 --- a/lib/movie_wrapper.rb +++ b/lib/movie_wrapper.rb @@ -7,6 +7,7 @@ class MovieWrapper DEFAULT_IMG_URL = "http://lorempixel.com/185/278/" def self.search(query) + puts KEY url = BASE_URL + "search/movie?api_key=" + KEY + "&query=" + query # puts url response = HTTParty.get(url) From 629d6e72db39dc5a1c8d650527116916d1cd3f79 Mon Sep 17 00:00:00 2001 From: Bo Trethewey Date: Tue, 13 Jun 2017 16:14:44 -0700 Subject: [PATCH 03/12] added test for create method for movie controller --- config/routes.rb | 2 +- test/controllers/movies_controller_test.rb | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/config/routes.rb b/config/routes.rb index b81406ce..8bb45c26 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,7 +4,7 @@ resources :customers, only: [:index] resources :movies, only: [:index, :show], param: :title - post "/movies", to: "movies#create" + post "/movies", to: "movies#create", as: "new_movie" 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/test/controllers/movies_controller_test.rb b/test/controllers/movies_controller_test.rb index 9172cf6e..4e24e1c3 100644 --- a/test/controllers/movies_controller_test.rb +++ b/test/controllers/movies_controller_test.rb @@ -1,6 +1,22 @@ require 'test_helper' class MoviesControllerTest < ActionDispatch::IntegrationTest + describe "create" do + it "creates a new movie" do + post new_movie_path, params: { movie: { title: "WowMovie!", overview: "MyText", release_date: "2017-01-11", inventory: 4 }} + must_respond_with :success + Movie.last.must_equal movies(:one) + end + + it "returns the id of the movie if successful" do + post new_movie_path(movies(:one)) + body = JSON.parse(response.body) + body.must_be_kind_of Hash + body["id"].must_equal Movie.last.id + end + + # checks that if the movie already exisit in our library, then it return error message + end describe "index" do it "returns a JSON array" do get movies_url From 83fdcfbf3cb1f0419dfc929aa98e82612ca571db Mon Sep 17 00:00:00 2001 From: Bo Trethewey Date: Tue, 13 Jun 2017 16:20:13 -0700 Subject: [PATCH 04/12] controller tests are passing --- test/controllers/movies_controller_test.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/controllers/movies_controller_test.rb b/test/controllers/movies_controller_test.rb index 4e24e1c3..67b03a4d 100644 --- a/test/controllers/movies_controller_test.rb +++ b/test/controllers/movies_controller_test.rb @@ -3,9 +3,9 @@ class MoviesControllerTest < ActionDispatch::IntegrationTest describe "create" do it "creates a new movie" do - post new_movie_path, params: { movie: { title: "WowMovie!", overview: "MyText", release_date: "2017-01-11", inventory: 4 }} - must_respond_with :success - Movie.last.must_equal movies(:one) + post new_movie_path params: { title: "WowMovie!", overview: "MyText", release_date: "2017-01-11", inventory: 4 } + assert_response :success + Movie.last.title.must_equal movies(:one).title end it "returns the id of the movie if successful" do From c6ef434857b736c868107bef90c94b0cc444b8ee Mon Sep 17 00:00:00 2001 From: Aurora Lemieux Date: Fri, 16 Jun 2017 11:15:45 -0700 Subject: [PATCH 05/12] fixed image url issues for create --- app/models/movie.rb | 4 +++- lib/movie_wrapper.rb | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/app/models/movie.rb b/app/models/movie.rb index 0327a4d6..2d5bd4d3 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -12,8 +12,10 @@ def image_url orig_value = read_attribute :image_url if !orig_value MovieWrapper::DEFAULT_IMG_URL - else + elsif external_id MovieWrapper.construct_image_url(orig_value) + else + orig_value end end end diff --git a/lib/movie_wrapper.rb b/lib/movie_wrapper.rb index 4dc7cbb4..109b2411 100644 --- a/lib/movie_wrapper.rb +++ b/lib/movie_wrapper.rb @@ -28,7 +28,8 @@ 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["poster_path"], + #image_url: api_result["poster_path"] ? self.construct_image_url(api_result["poster_path"]) : nil, external_id: api_result["id"]) end From 1fd1603d0a31f19445f5e90fea666026ac49a600 Mon Sep 17 00:00:00 2001 From: Aurora Lemieux Date: Fri, 16 Jun 2017 17:11:32 -0700 Subject: [PATCH 06/12] trying to figure out inventory for new movies --- app/controllers/movies_controller.rb | 2 ++ lib/movie_wrapper.rb | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 5c09839a..a31cfd0c 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -24,6 +24,8 @@ def show def create movie = Movie.new(title: params[:title], overview: params[:overview], release_date: params[:release_date], inventory: params[:inventory], image_url: params[:image_url]) + # movie.inventory ||= rand(8) + if movie.save render json: {id: movie.id}, status: :ok else diff --git a/lib/movie_wrapper.rb b/lib/movie_wrapper.rb index 109b2411..ffaecb26 100644 --- a/lib/movie_wrapper.rb +++ b/lib/movie_wrapper.rb @@ -30,7 +30,8 @@ def self.construct_movie(api_result) release_date: api_result["release_date"], image_url: api_result["poster_path"], #image_url: api_result["poster_path"] ? self.construct_image_url(api_result["poster_path"]) : nil, - external_id: api_result["id"]) + external_id: api_result["external_id"], + inventory: 1) end def self.construct_image_url(img_name) From 0d29ea15c6accc69e96b7cd04d4beb6bb18c0be4 Mon Sep 17 00:00:00 2001 From: Aurora Lemieux Date: Mon, 19 Jun 2017 10:49:05 -0700 Subject: [PATCH 07/12] fixed bug in movie rb logic for image showing --- lib/movie_wrapper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/movie_wrapper.rb b/lib/movie_wrapper.rb index ffaecb26..5e2761eb 100644 --- a/lib/movie_wrapper.rb +++ b/lib/movie_wrapper.rb @@ -30,7 +30,7 @@ def self.construct_movie(api_result) release_date: api_result["release_date"], image_url: api_result["poster_path"], #image_url: api_result["poster_path"] ? self.construct_image_url(api_result["poster_path"]) : nil, - external_id: api_result["external_id"], + external_id: api_result["id"], inventory: 1) end From a0aded1603041d14c79a155d7f48749837a41adb Mon Sep 17 00:00:00 2001 From: Bo Trethewey Date: Mon, 19 Jun 2017 11:01:58 -0700 Subject: [PATCH 08/12] added delete action and route for movie --- app/controllers/movies_controller.rb | 12 ++++++++++++ config/routes.rb | 1 + 2 files changed, 13 insertions(+) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index a31cfd0c..f5f95bab 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -31,7 +31,19 @@ def create else render json: { errors: movie.errors.messages }, status: :bad_request end + end + def destroy + movie = Movie.find_by(id: params[:id]) + if movie + if movie.destroy + render json: {id: movie.id}, status: :ok + else + render json: { errors: movie.errors.messages }, status: :bad_request + end + else + render json: { errors: "This Movie does not exist in the database" }, status: :not_found + end end private diff --git a/config/routes.rb b/config/routes.rb index 8bb45c26..64e05097 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,6 +5,7 @@ resources :movies, only: [:index, :show], param: :title post "/movies", to: "movies#create", as: "new_movie" + delete "/movies/:id", to:"movies#destroy", as: "delete_movie" post "/rentals/:title/check-out", to: "rentals#check_out", as: "check_out" post "/rentals/:title/return", to: "rentals#check_in", as: "check_in" From b30dd7b6a87a4e20d6b8d9fc665a3c5ffc4340d8 Mon Sep 17 00:00:00 2001 From: Bo Trethewey Date: Mon, 19 Jun 2017 11:19:50 -0700 Subject: [PATCH 09/12] added tests for the movie controller for destroy action --- test/controllers/movies_controller_test.rb | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/test/controllers/movies_controller_test.rb b/test/controllers/movies_controller_test.rb index 67b03a4d..9d4fdb23 100644 --- a/test/controllers/movies_controller_test.rb +++ b/test/controllers/movies_controller_test.rb @@ -17,6 +17,34 @@ class MoviesControllerTest < ActionDispatch::IntegrationTest # checks that if the movie already exisit in our library, then it return error message end + + describe "destroy" do + it "deletes a movie" do + Movie.destroy_all + post new_movie_path params: { title: "WowMovie!", overview: "MyText", release_date: "2017-01-11", inventory: 4 } + movie_id = Movie.first.id + delete delete_movie_path(movie_id) + assert_response :success + body = JSON.parse(response.body) + body.must_be_kind_of Hash + body["id"].must_equal movie_id + Movie.count.must_equal 0 + end + + it "Doens't delete movie if the id passed is invalid" do + Movie.destroy_all + post new_movie_path params: { title: "WowMovie!", overview: "MyText", release_date: "2017-01-11", inventory: 4 } + delete delete_movie_path(23424556) + assert_response :not_found + Movie.count.must_equal 1 + body = JSON.parse(response.body) + body.must_be_kind_of Hash + body["errors"].must_equal "This Movie does not exist in the database" + end + end + + + describe "index" do it "returns a JSON array" do get movies_url From 006dcb3ed8eff96beaed20fde7a25ede9cad4f6c Mon Sep 17 00:00:00 2001 From: Aurora Lemieux Date: Wed, 21 Jun 2017 15:40:13 -0700 Subject: [PATCH 10/12] added dependent destroy to movie model in case of movie deletion and rentals --- 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 2d5bd4d3..c90d02ba 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -1,7 +1,7 @@ class Movie < ApplicationRecord attr_accessor :external_id - has_many :rentals + has_many :rentals, dependent: :destroy has_many :customers, through: :rentals def available_inventory From 5a61a857eca2c0a66e5f3c751b58d00fa77c9a94 Mon Sep 17 00:00:00 2001 From: Bo Trethewey Date: Fri, 23 Jun 2017 10:42:21 -0700 Subject: [PATCH 11/12] added action methods for index and customer_rental to the Rental API --- app/controllers/rentals_controller.rb | 28 +++++++++++++++++++-------- config/routes.rb | 3 ++- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 92744380..f1c7dd3e 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -1,6 +1,16 @@ class RentalsController < ApplicationController before_action :require_movie, only: [:check_out, :check_in] - before_action :require_customer, only: [:check_out, :check_in] + before_action :require_customer, only: [:check_out, :check_in, :customer_rental] + + def index + data = Rental.all + render status: :ok, json: data + end + + def customer_rental + rentals = Rental.where(customer: @customer) + render status: :ok, json: rentals + end def check_out rental = Rental.new(movie: @movie, customer: @customer, due_date: params[:due_date], returned: false) @@ -32,18 +42,20 @@ def check_in def overdue rentals = Rental.overdue.map do |rental| { - title: rental.movie.title, - customer_id: rental.customer_id, - name: rental.customer.name, - postal_code: rental.customer.postal_code, - checkout_date: rental.checkout_date, - due_date: rental.due_date + title: rental.movie.title, + customer_id: rental.customer_id, + name: rental.customer.name, + postal_code: rental.customer.postal_code, + checkout_date: rental.checkout_date, + due_date: rental.due_date } end render status: :ok, json: rentals end -private + + + private # TODO: make error payloads arrays def require_movie @movie = Movie.find_by title: params[:title] diff --git a/config/routes.rb b/config/routes.rb index 64e05097..cd688ec0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -10,6 +10,7 @@ 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" - + get "/rentals", to: "rentals#index", as: "rentals" + get "/rentals/by-customer", to: "rentals#customer_rental", as: "rentals_by_customer" end From 1a2dda6d237db8e09f3b6b96d8d0d2f62942a467 Mon Sep 17 00:00:00 2001 From: Bo Trethewey Date: Fri, 23 Jun 2017 14:53:55 -0700 Subject: [PATCH 12/12] updated rental controller --- app/controllers/rentals_controller.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index f1c7dd3e..6d3919ea 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -9,6 +9,15 @@ def index def customer_rental rentals = Rental.where(customer: @customer) + rentals = rentals.map do |rental| + { + title: rental.movie.title, + customer_id: rental.customer_id, + checkout_date: rental.checkout_date, + due_date: rental.due_date, + returned: rental.returned + } + end render status: :ok, json: rentals end