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

def create
existing_movie = Movie.find_by(title: params["title"])
if existing_movie
puts existing_movie
existing_movie.inventory += 1
if existing_movie.save
render status: :ok, json: {
id: existing_movie.id
}
else
render status: :bad_request, json: {errors: 'could not add movie'}
end
else
new_movie = Movie.create(movie_params)
if new_movie.id
render status: :ok, json:{
id: new_movie.id
}
else
render status: :bad_request, json: {errors: 'could not create movie'}
end
end
end

private

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

def movie_params
params.require(:movie).permit(:title, :overview, :release_date, :image_url)

end
end
14 changes: 8 additions & 6 deletions app/models/movie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ def available_inventory
end

def image_url
orig_value = read_attribute :image_url
if !orig_value
MovieWrapper::DEFAULT_IMG_URL
else
MovieWrapper.construct_image_url(orig_value)
end
orig_value = read_attribute :image_url
if !orig_value
MovieWrapper::DEFAULT_IMG_URL
elsif external_id
MovieWrapper.construct_image_url(orig_value)
else
orig_value
end
end
end
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
5 changes: 5 additions & 0 deletions db/migrate/20170620213100_set_default_inventory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class SetDefaultInventory < ActiveRecord::Migration[5.0]
def change
change_column_default :movies, :inventory, 1
end
end
8 changes: 4 additions & 4 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20170612201103) do
ActiveRecord::Schema.define(version: 20170620213100) do

create_table "customers", force: :cascade do |t|
t.string "name"
Expand All @@ -29,9 +29,9 @@
t.string "title"
t.text "overview"
t.date "release_date"
t.integer "inventory"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "inventory", default: 1
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "image_url"
end

Expand Down
6 changes: 4 additions & 2 deletions lib/movie_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ 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"])
#(api_result["poster_path"] ? self.construct_image_url(api_result["poster_path"]) : nil),

end

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

end