diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 362e2791..d00326f4 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -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 @@ -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 diff --git a/app/models/movie.rb b/app/models/movie.rb index 0327a4d6..8ac98060 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -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 diff --git a/config/routes.rb b/config/routes.rb index 54bf033e..8eff1ff6 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" diff --git a/db/migrate/20170620213100_set_default_inventory.rb b/db/migrate/20170620213100_set_default_inventory.rb new file mode 100644 index 00000000..6ba066dc --- /dev/null +++ b/db/migrate/20170620213100_set_default_inventory.rb @@ -0,0 +1,5 @@ +class SetDefaultInventory < ActiveRecord::Migration[5.0] + def change + change_column_default :movies, :inventory, 1 + end +end diff --git a/db/schema.rb b/db/schema.rb index 15e75977..56909b67 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" @@ -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 diff --git a/lib/movie_wrapper.rb b/lib/movie_wrapper.rb index 7bd05c0e..43826b57 100644 --- a/lib/movie_wrapper.rb +++ b/lib/movie_wrapper.rb @@ -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