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
23 changes: 22 additions & 1 deletion app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@
'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

# 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
Expand Down
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
63 changes: 33 additions & 30 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions test/controllers/movies_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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