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

def create
video = Video.new(video_params)

if video.save
render json: video.as_json(only: [:id]), status: :created
else
render json: { errors: video.errors.messages }, status: :bad_request
end
end

private

def require_video
Expand All @@ -29,4 +39,7 @@ def require_video
render status: :not_found, json: { errors: { title: ["No video with title #{params["title"]}"] } }
end
end
def video_params
return params.permit(:title, :overview, :release_date, :inventory, :available_inventory)
end
end
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 :videos, only: [:index, :show], param: :title
resources :videos, 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
36 changes: 36 additions & 0 deletions test/controllers/videos_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,40 @@ class VideosControllerTest < ActionDispatch::IntegrationTest
expect(data["errors"]).must_include "title"
end
end
describe "create" do
let(:video_params) {
{
title: "Alf the movie",
overview: "The most early 90s movie of all time",
release_date: "2020-12-25",
inventory: 6,
available_inventory: 6
}
}
it "can create a valid video" do
# Assert
expect {
post videos_path, params: video_params
}.must_differ "Video.count", 1

must_respond_with :created
end

it "will respond with bad request and errors for an invalid movie" do
# Arrange
video_params[:title] = nil

# Assert
expect {
post videos_path, params: video_params
}.wont_change "Video.count"
body = JSON.parse(response.body)

expect(body.keys).must_include "errors"
expect(body["errors"].keys).must_include "title"
expect(body["errors"]["title"]).must_include "can't be blank"

must_respond_with :bad_request
end
end
end