diff --git a/.gitignore b/.gitignore index cb5b9b3e..d31cc8a5 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/app/controllers/videos_controller.rb b/app/controllers/videos_controller.rb index c9a2bb08..4622b955 100644 --- a/app/controllers/videos_controller.rb +++ b/app/controllers/videos_controller.rb @@ -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 diff --git a/config/routes.rb b/config/routes.rb index 16fc2214..1111b4bf 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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" diff --git a/test/controllers/videos_controller_test.rb b/test/controllers/videos_controller_test.rb index 730915ed..b3718375 100644 --- a/test/controllers/videos_controller_test.rb +++ b/test/controllers/videos_controller_test.rb @@ -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