diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 362e2791..b75c8d01 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -3,7 +3,12 @@ class MoviesController < ApplicationController def index if params[:query] - data = MovieWrapper.search(params[:query]) + data = MovieWrapper.search(params[:query]) # all ids are null, external_id is not null + hash = {} # {external_id => id} + Movie.all.select(:id, :external_id).each {|movie| hash[movie.external_id] = movie.id } + data.each_with_index do |item,i| + data[i][:id] = hash[item[:external_id]] + end else data = Movie.all end @@ -21,6 +26,22 @@ def show ) end + def create + movie = Movie.new( + title: params[:title], + overview: params[:overview], + release_date: params[:release_date], + image_url: params[:image_url], + inventory: 1, + external_id: params[:external_id] + ) + if movie.save + render status: :ok, json: movie + else + render status: :bad_request, json: { errors: rental.errors.messages } + end + end + private def require_movie diff --git a/config/environments/development.rb b/config/environments/development.rb index 6f719704..a230e5de 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -21,7 +21,7 @@ 'Cache-Control' => 'public, max-age=172800' } else - config.action_controller.perform_caching = false + config.action_controller.perform_caching = true config.cache_store = :null_store end @@ -29,7 +29,7 @@ # Don't care if the mailer can't send. config.action_mailer.raise_delivery_errors = false - config.action_mailer.perform_caching = false + config.action_mailer.perform_caching = true # Print deprecation notices to the Rails logger. config.active_support.deprecation = :log diff --git a/config/routes.rb b/config/routes.rb index f4c99688..76715f9a 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/schema.rb b/db/schema.rb index ffb28f7e..8c8b42d4 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -2,48 +2,51 @@ # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. # -# Note that this schema.rb definition is the authoritative source for your -# database schema. If you need to create the application database on another -# system, you should be using db:schema:load, not running all the migrations -# from scratch. The latter is a flawed and unsustainable approach (the more migrations -# you'll amass, the slower it'll run and the greater likelihood for issues). +# This file is the source Rails uses to define your schema when running `rails +# db:schema:load`. When creating a new database, `rails db:schema:load` tends to +# be faster and is potentially less error prone than running all of your +# migrations from scratch. Old migrations may fail to apply correctly if those +# migrations use external dependencies or application code. # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20180618042754) do +ActiveRecord::Schema.define(version: 2018_06_18_042754) do + + # These are extensions that must be enabled in order to support this database + enable_extension "plpgsql" create_table "customers", force: :cascade do |t| - t.string "name" + t.string "name" t.datetime "registered_at" - t.string "address" - t.string "city" - t.string "state" - t.string "postal_code" - t.string "phone" - t.float "account_credit" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.string "address" + t.string "city" + t.string "state" + t.string "postal_code" + t.string "phone" + t.float "account_credit" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false end create_table "movies", force: :cascade do |t| - 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.string "image_url" - t.integer "external_id" + 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.string "image_url" + t.integer "external_id" end create_table "rentals", force: :cascade do |t| - t.integer "customer_id" - t.integer "movie_id" - t.date "checkout_date" - t.date "due_date" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.boolean "returned" + t.integer "customer_id" + t.integer "movie_id" + t.date "checkout_date" + t.date "due_date" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.boolean "returned" t.index ["customer_id"], name: "index_rentals_on_customer_id" t.index ["movie_id"], name: "index_rentals_on_movie_id" end diff --git a/test/controllers/movies_controller_test.rb b/test/controllers/movies_controller_test.rb index 9172cf6e..02505900 100644 --- a/test/controllers/movies_controller_test.rb +++ b/test/controllers/movies_controller_test.rb @@ -75,4 +75,27 @@ class MoviesControllerTest < ActionDispatch::IntegrationTest end end + + describe "create" do + it "can create a movie with valid info" do + movie_hash = + { + title: "Test", + overview: "Test overview", + inventory: 1, + external_id: 12345 + } + + expect { + post movies_path, params: movie_hash + }.must_differ "Movie.count", 1 + + new_movie = Movie.last + expect(new_movie.title).must_equal movie_hash[:title] + expect(new_movie.overview).must_equal movie_hash[:overview] + expect(new_movie.inventory).must_equal movie_hash[:inventory] + expect(new_movie.external_id).must_equal movie_hash[:external_id] + must_respond_with :success + end + end end