diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 863982c2a..5f2f79848 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -3,7 +3,47 @@ class MoviesController < ApplicationController def index movies = Movie.all - render json: movies.as_json + render( + json: movies.as_json(only: [:id, :title, :release_date]), + status: :ok + ) + end + + def show + movie = Movie.find_by(id: params[:id]) + + if movie + render( + json: movie.as_json(only: [:title, :overview, :release_date, :inventory]), + status: :ok + ) + else + render( + json: { "not found" => true }, + status: :not_found + ) + end + end + + def create + movie = Movie.new(movie_params) + + if movie.save + render( + json: { id: movie.id }, + status: :ok + ) + else + render( + json: { errors: movie.errors.messages }, + status: :bad_request + ) + end + end + +private + def movie_params + params.require(:movie).permit(:title, :overview, :release_date, :inventory) end diff --git a/app/models/movie.rb b/app/models/movie.rb index dc614df15..182143100 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -1,2 +1,5 @@ class Movie < ApplicationRecord + + validates :title, presence: true + validates :inventory, numericality: { greater_than: 0 } end diff --git a/db/schema.rb b/db/schema.rb index 40b43857d..00ae47220 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,13 +10,11 @@ # # It's strongly recommended that you check this file into your version control system. - -ActiveRecord::Schema.define(version: 20171106213051) do +ActiveRecord::Schema.define(version: 20171106213616) 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 "registered_at" @@ -28,7 +26,15 @@ t.datetime "created_at", null: false t.datetime "updated_at", null: false t.float "account_credit" + end + create_table "movies", force: :cascade do |t| + t.string "title" + t.string "overview" + t.string "release_date" + t.integer "inventory" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false end end diff --git a/test/controllers/movies_controller_test.rb b/test/controllers/movies_controller_test.rb index f2d06c62e..bbf6beda6 100644 --- a/test/controllers/movies_controller_test.rb +++ b/test/controllers/movies_controller_test.rb @@ -17,7 +17,64 @@ must_respond_with :success body = JSON.parse(response.body) - body.must_be empty? + body.must_be :empty? + end + + it "returns the correct information" do + get movies_path + body = JSON.parse(response.body) + + body.each do |movie| + movie.keys.sort.must_equal ["id", "release_date", "title"] + end + end + end + + describe "show" do + it "returns a hash of information for one movie" do + get movie_path(movies(:jaws).id) + must_respond_with :success + + body = JSON.parse(response.body) + body.must_be_kind_of Hash + body["title"].must_equal movies(:jaws).title + end + + it "returns not_found when movie doesn't exist" do + get movie_path(Movie.last.id + 1) + must_respond_with :not_found + + body = JSON.parse(response.body) + body.must_equal "not found" => true + end + end + + describe "create" do + let(:movie_data) { + { + title: "Pirates of the Caribbean", + overview: "great movie with pirates", + release_date: "2001-05-09", + inventory: 3 + } + } + + it "creates a movie" do + proc { + post movies_path, params: {movie: movie_data} + }.must_change 'Movie.count', 1 + must_respond_with :success + end + + it "responds with bad_request if invalid data" do + movie_data[:inventory] = nil + proc { + post movies_path, params: {movie: movie_data} + }.wont_change 'Movie.count' + must_respond_with :bad_request + + body = JSON.parse(response.body) + body.must_equal "errors" => {"inventory" => ["is not a number"]} end end end diff --git a/test/fixtures/customers.yml b/test/fixtures/customers.yml index f1002930e..ef01a8091 100644 --- a/test/fixtures/customers.yml +++ b/test/fixtures/customers.yml @@ -11,7 +11,7 @@ one: city: Hillsboro state: OR, postal_code: 24309 - phone": (322) 510-8695 + phone: (322) 510-8695 account_credit: 13.15 # column: value # diff --git a/test/fixtures/movies.yml b/test/fixtures/movies.yml index dc3ee79b5..5e737617f 100644 --- a/test/fixtures/movies.yml +++ b/test/fixtures/movies.yml @@ -4,8 +4,14 @@ # model remove the "{}" from the fixture names and add the columns immediately # below each fixture, per the syntax in the comments below # -one: {} -# column: value -# -two: {} -# column: value +psycho: + title: "Psycho" + overview: "When larcenous real estate clerk Marion Crane goes on the lam with a wad of cash and hopes of starting a new life, she ends up at the notorious Bates Motel, where manager Norman Bates cares for his housebound mother. The place seems quirky, but fine… until Marion decides to take a shower." + release_date: "1960-06-16" + inventory: 8 + +jaws: + title: "The Exorcist" + overview: "12-year-old Regan MacNeil begins to adapt an explicit new personality as strange events befall the local area of Georgetown. Her mother becomes torn between science and superstition in a desperate bid to save her daughter, and ultimately turns to her last hope: Father Damien Karras, a troubled priest who is struggling with his own faith." + release_date: "1973-12-26" + inventory: 7 diff --git a/test/models/customer_test.rb b/test/models/customer_test.rb index cbe618fa0..d6e29f30e 100644 --- a/test/models/customer_test.rb +++ b/test/models/customer_test.rb @@ -1,13 +1,9 @@ require "test_helper" describe Customer do - let(:customer) { Customer.new } - it "must be valid" do - value(customer).must_be :valid? - end # at least one positive and one negative test case for each relation, validation, and custom function - + end diff --git a/test/models/movie_test.rb b/test/models/movie_test.rb index 34d1d30a5..52cae63c3 100644 --- a/test/models/movie_test.rb +++ b/test/models/movie_test.rb @@ -1,9 +1,36 @@ require "test_helper" describe Movie do - let(:movie) { Movie.new } + describe "validations" do + before do + @movie_params = { + overview: "this is an overview", + release_date: "some date" + } + end + it "works when the right things are there" do + movie = movies(:psycho) + movie.must_be :valid? + end - it "must be valid" do - value(movie).must_be :valid? + it "wont save if there is no name" do + movie = Movie.new(@movie_params) + movie.wont_be :valid? + movie.errors.messages.must_include :title + end + + it "wont save if there is no inventory" do + movie = Movie.new(@movie_params) + movie.wont_be :valid? + movie.errors.messages.must_include :inventory + end + + it "must have an inventory greater_than 0" do + @movie_params[:inventory] = -12 + movie = Movie.new(@movie_params) + movie.wont_be :valid? + movie.errors.messages.must_include :inventory + end end + end