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 - Evelynn and Nara #2

Open
wants to merge 2 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
25 changes: 25 additions & 0 deletions app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,27 @@ def show
)
end

def create
movie = Movie.find_by(title: params[:title])
if !movie
movie = Movie.new(movie_params)
movie.inventory = 1
else
movie.inventory += 1
end
if movie.save
render(
status: :ok,
json: movie.as_json(
only: [:id, :title, :overview, :release_date, :inventory, :external_id, :image_url],
)
)
else
render json: { errors: movie.errors.messages },
status: :bad_request
end
end

private

def require_movie
Expand All @@ -29,4 +50,8 @@ def require_movie
render status: :not_found, json: { errors: { title: ["No movie with title #{params["title"]}"] } }
end
end

def movie_params
params.permit(:title, :overview, :release_date, :image_url, :external_id)
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 !orig_value.include?("https://image.tmdb.org/t/p/") && external_id
MovieWrapper.construct_image_url(orig_value)
else
orig_value
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion lib/movie_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ 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"],
external_id: api_result["id"])
end

Expand Down