Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sockets - Kelly & Jansen #17

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
22 changes: 18 additions & 4 deletions app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,31 @@ def show
status: :ok,
json: @movie.as_json(
only: [:title, :overview, :release_date, :inventory],
methods: [:available_inventory]
)
)
methods: [:available_inventory],
),
)
end

def add_movie
movie = Movie.new(title: params[:title], overview: params[:overview], release_date: params[:release_date], image_url: params[:image_url], external_id: params[:external_id])

already_added = Movie.find_by(external_id: params[:external_id])

if already_added
render status: :conflict, json: {errors: "This movie is already in the library"}
elsif movie.save
render status: :ok, json: {}
else
render status: :bad_request, json: {errors: movie.errors.messages}
end
end

private

def require_movie
@movie = Movie.find_by(title: params[:title])
unless @movie
render status: :not_found, json: { errors: { title: ["No movie with title #{params["title"]}"] } }
render status: :not_found, json: {errors: {title: ["No movie with title #{params["title"]}"]}}
end
end
end
2 changes: 1 addition & 1 deletion app/models/movie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,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?("https://image.tmdb.org/t/p/")
MovieWrapper.construct_image_url(orig_value)
else
orig_value
Expand Down
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
post "/rentals/:title/return", to: "rentals#check_in", as: "check_in"
get "/rentals/overdue", to: "rentals#overdue", as: "overdue"

root 'movies#index'
post "/movies/add-movie", to: "movies#add_movie", as: "add_movie"

root "movies#index"
end
7 changes: 4 additions & 3 deletions lib/movie_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ class MovieWrapper
DEFAULT_IMG_URL = "http://lorempixel.com/185/278/"

def self.search(query)
sleep(1)
url = BASE_URL + "search/movie?api_key=" + KEY + "&query=" + query
response = HTTParty.get(url)
response = HTTParty.get(url)
if response["total_results"] == 0
return []
else
Expand All @@ -28,11 +29,11 @@ def self.construct_movie(api_result)
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),
external_id: api_result["id"])
external_id: api_result["id"],
)
end

def self.construct_image_url(img_name)
return BASE_IMG_URL + DEFAULT_IMG_SIZE + img_name
end

end