From a77f27f0e064d230b0c9cf8809a48f763dfc5c39 Mon Sep 17 00:00:00 2001 From: Rachael Gomez Date: Thu, 21 Jan 2021 13:47:05 -0800 Subject: [PATCH] created create function and added the route. It has been tested and is working on postman but the tests are not working --- app/controllers/videos_controller.rb | 13 ++++++++ config/routes.rb | 2 +- test/controllers/videos_controller_test.rb | 36 ++++++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/app/controllers/videos_controller.rb b/app/controllers/videos_controller.rb index c9a2bb08..8aea7f68 100644 --- a/app/controllers/videos_controller.rb +++ b/app/controllers/videos_controller.rb @@ -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 @@ -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 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..5b98ec68 100644 --- a/test/controllers/videos_controller_test.rb +++ b/test/controllers/videos_controller_test.rb @@ -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