From 28c726991ff0a9982ea26e0e60edf6863f1fe943 Mon Sep 17 00:00:00 2001 From: peachmakkoli Date: Wed, 24 Jun 2020 14:19:09 -0700 Subject: [PATCH 1/4] adds errors to movies#index action if the query params do not match any movie titles --- app/controllers/movies_controller.rb | 4 ++++ test/controllers/movies_controller_test.rb | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 362e2791..5c1535c5 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -4,6 +4,10 @@ class MoviesController < ApplicationController def index if params[:query] data = MovieWrapper.search(params[:query]) + if data.empty? + render status: :not_found, json: { errors: { query: ["No movie matching the query \"#{params[:query]}\""] } } + return + end else data = Movie.all end diff --git a/test/controllers/movies_controller_test.rb b/test/controllers/movies_controller_test.rb index 9172cf6e..de30ace8 100644 --- a/test/controllers/movies_controller_test.rb +++ b/test/controllers/movies_controller_test.rb @@ -40,6 +40,15 @@ class MoviesControllerTest < ActionDispatch::IntegrationTest expected_names[movie["title"]] = true end end + + it "returns a 404 error if there are no movies matching the query params" do + get movies_url, params: {query: "a title that doesn't exist"} + assert_response :not_found + + data = JSON.parse @response.body + data.must_include "errors" + data["errors"].must_include "query" + end end describe "show" do From 3852cb3eb2fb716ca8d762c6284c2713f2598567 Mon Sep 17 00:00:00 2001 From: peachmakkoli Date: Wed, 24 Jun 2020 14:31:49 -0700 Subject: [PATCH 2/4] adds an error message to movies#index if query params are empty --- app/controllers/movies_controller.rb | 11 ++++++++--- test/controllers/movies_controller_test.rb | 9 +++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 5c1535c5..481029b0 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -3,10 +3,15 @@ class MoviesController < ApplicationController def index if params[:query] - data = MovieWrapper.search(params[:query]) - if data.empty? - render status: :not_found, json: { errors: { query: ["No movie matching the query \"#{params[:query]}\""] } } + if params[:query].empty? + render status: :not_found, json: { errors: { query: ["Please enter a search query"] } } return + else + data = MovieWrapper.search(params[:query]) + if data.empty? + render status: :not_found, json: { errors: { query: ["No movie matching the query \"#{params[:query]}\""] } } + return + end end else data = Movie.all diff --git a/test/controllers/movies_controller_test.rb b/test/controllers/movies_controller_test.rb index de30ace8..a5048d56 100644 --- a/test/controllers/movies_controller_test.rb +++ b/test/controllers/movies_controller_test.rb @@ -49,6 +49,15 @@ class MoviesControllerTest < ActionDispatch::IntegrationTest data.must_include "errors" data["errors"].must_include "query" end + + it "returns a 404 error if the query params are empty" do + get movies_url, params: {query: ""} + assert_response :not_found + + data = JSON.parse @response.body + data.must_include "errors" + data["errors"].must_include "query" + end end describe "show" do From e949944033aad2c7ae6f9947ba63821d9fb43939 Mon Sep 17 00:00:00 2001 From: Yaz Date: Fri, 26 Jun 2020 11:22:14 -0700 Subject: [PATCH 3/4] added post action for adding movie to our library, with controller action and route --- 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 481029b0..95591d63 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -30,6 +30,17 @@ def show ) end + def create + movie = Movie.new(movie_params) + movie.inventory = 5 + if Movie.find_by(external_id: movie.external_id) + render status: :bad_request, json: { errors: { external_id: ["#{movie.title} is already in library"] } } + else + movie.save + render status: :ok, json: movie + end + end + private def require_movie @@ -38,4 +49,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, :external_id, :image_url) + end end diff --git a/config/routes.rb b/config/routes.rb index f4c99688..76715f9a 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 aaefe75ef499a02a64c0d7c5f07b45228a491d92 Mon Sep 17 00:00:00 2001 From: Yaz Date: Fri, 26 Jun 2020 15:07:23 -0700 Subject: [PATCH 4/4] added model methods for popular and recent movies, and expanded conditionals for index action --- app/controllers/movies_controller.rb | 6 ++++++ app/models/movie.rb | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 95591d63..d5520c46 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -13,6 +13,12 @@ def index return end end + elsif params[:showcase] + if params[:showcase] == "recently_added" + data = Movie.recently_added + elsif params[:showcase] == "popular" + data = Movie.popular + end else data = Movie.all end diff --git a/app/models/movie.rb b/app/models/movie.rb index fda94941..6c9db1eb 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -6,6 +6,15 @@ def available_inventory self.inventory - Rental.where(movie: self, returned: false).length end + def self.recently_added + month = DateTime.now >> -1 + self.where("created_at >= :last_month", last_month: month) + end + + def self.popular + self.joins(:rentals) + end + def image_url raw_value = read_attribute :image_url if !raw_value