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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
# If you find yourself ignoring temporary files generated by your text editor
# or operating system, you probably want to add a global ignore instead:
# git config --global core.excludesfile '~/.gitignore_global'

.env
.idea
.generators
# Ignore bundler config.
/.bundle

Expand Down
17 changes: 17 additions & 0 deletions app/controllers/videos_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,25 @@ def show
)
end

def create
video = Video.new(video_params)
if Video.find_by(external_id: params[:external_id]).nil?
if video.save
render json: video.as_json(only: [:id]), status: :created
return
else
render json: { errors: video.errors.messages }, status: :bad_request
return
end
end
end

private

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

def require_video
@video = Video.find_by(title: params[:title])
unless @video
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 :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
32 changes: 32 additions & 0 deletions test/controllers/videos_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,36 @@ class VideosControllerTest < ActionDispatch::IntegrationTest
expect(data["errors"]).must_include "title"
end
end

describe "create" do
let(:video_params) {
{
title: "Meg Thee Santa",
overview: "T'was the night before huh?",
release_date: '2030-12-12',
}
}
it "can create a valid video" do
expect {
post videos_path, params: video_params
}.must_change "Video.count", 1

must_respond_with :created
end

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

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