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

def create
movie = Movie.new(movie_params)
if movie.save
render json: movie.as_json, status: :created
return
else
render json: {errors: movie.errors.messages}, status: :bad_request
return
end
end

private

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

def movie_params
params.permit(:title, :overview, :release_date, :image_url, :external_id)
end

end
1 change: 0 additions & 1 deletion app/controllers/rentals_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ class RentalsController < ApplicationController
# TODO: make sure that wave 2 works all the way
def check_out
rental = Rental.new(movie: @movie, customer: @customer, due_date: params[:due_date])

if rental.save
render status: :ok, json: {}
else
Expand Down
3 changes: 3 additions & 0 deletions app/models/movie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ class Movie < ApplicationRecord
has_many :rentals
has_many :customers, through: :rentals

validates :title, :overview, :release_date, :image_url, :external_id, presence: true
validates :external_id, uniqueness: true

def available_inventory
self.inventory - Rental.where(movie: self, returned: false).length
end
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
35 changes: 35 additions & 0 deletions test/controllers/movies_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,39 @@ class MoviesControllerTest < ActionDispatch::IntegrationTest

end
end

describe "create" do
before do
@valid_movie = {
title: "something",
overview: "something",
release_date: "01-01-2020",
inventory: 5,
image_url: "https://designerdoginfo.files.wordpress.com/2012/10/apricot-cavoodle-puppy-on-blue-blanket.jpg",
external_id: 6
}
@invalid_movie = {
inventory: 5,
}
end




it "can create a new movie with valid data" do
expect { post movies_path, params: @valid_movie }.must_differ "Movie.count", 1
end

it "responds with bad request and errors for invalid data" do
expect { post movies_path, params: @invalid_movie }.wont_change "Movie.count"

body = check_response(expected_type: Hash, expected_status: :bad_request)
expect(body.keys).must_include "errors"
expect(body["errors"].keys).must_include "title"
expect(body["errors"].keys).must_include "overview"
expect(body["errors"].keys).must_include "release_date"
expect(body["errors"].keys).must_include "external_id"
end

end
end
4 changes: 3 additions & 1 deletion test/models/movie_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ class MovieTest < ActiveSupport::TestCase
"title": "Hidden Figures",
"overview": "Some text",
"release_date": "1960-06-16",
"inventory": 8
"inventory": 8,
"image_url": "https://designerdoginfo.files.wordpress.com/2012/10/apricot-cavoodle-puppy-on-blue-blanket.jpg",
"external_id": 5,
}
}

Expand Down
9 changes: 9 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,13 @@ class ActiveSupport::TestCase
fixtures :all

# Add more helper methods to be used by all tests here...
# helper method
def check_response(expected_type:, expected_status: :success)
must_respond_with expected_status
expect(response.header['Content-Type']).must_include 'json'

body = JSON.parse(response.body)
expect(body).must_be_kind_of expected_type
return body
end
end