Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,22 @@ class MoviesController < ApplicationController

def index
if params[:query]
data = MovieWrapper.search(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
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
Expand All @@ -21,6 +36,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
Expand All @@ -29,4 +55,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
9 changes: 9 additions & 0 deletions app/models/movie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
18 changes: 18 additions & 0 deletions test/controllers/movies_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@ 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

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
Expand Down