diff --git a/.generators b/.generators new file mode 100644 index 00000000..16189766 --- /dev/null +++ b/.generators @@ -0,0 +1,8 @@ + + diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index c36d8cd0..ab59a744 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -7,7 +7,10 @@ def check_out rental = Rental.new(video: @video, customer: @customer, due_date: params[:due_date]) if rental.save - render status: :ok, json: {} + render status: :ok, + json: rental.as_json( + only: [:customer_id, :video_id, :due_date, :checkout_date], + ) else render status: :bad_request, json: { errors: rental.errors.messages } end diff --git a/app/controllers/videos_controller.rb b/app/controllers/videos_controller.rb index c9a2bb08..d59447e1 100644 --- a/app/controllers/videos_controller.rb +++ b/app/controllers/videos_controller.rb @@ -21,8 +21,27 @@ def show ) end + def create + @video = Video.new(video_params) + + if @video.save + render( + status: :ok, + json: @video.as_json( + only: [:title, :overview, :release_date, :external_id, :image_url], + ) + ) + else + render json: {ok: false, cause: "validation errors", errors: @video.errors}, status: :bad_request + end + end + private + def video_params + return params.permit(:external_id, :overview, :image_url, :release_date, :title, :inventory) + 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..78b3dcb7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,8 +3,8 @@ resources :customers, only: [:index] - resources :videos, only: [:index, :show], param: :title - + resources :videos, only: [:index, :show, :create], param: :title + # post "/videos/:title/add-to-library", to:"videos#add_to_library", as: "add_to_library" post "/rentals/:title/check-out", to: "rentals#check_out", as: "check_out" post "/rentals/:title/return", to: "rentals#check_in", as: "check_in" get "/rentals/overdue", to: "rentals#overdue", as: "overdue" diff --git a/test/controllers/videos_controller_test.rb b/test/controllers/videos_controller_test.rb index 730915ed..703d8c5a 100644 --- a/test/controllers/videos_controller_test.rb +++ b/test/controllers/videos_controller_test.rb @@ -77,4 +77,26 @@ class VideosControllerTest < ActionDispatch::IntegrationTest expect(data["errors"]).must_include "title" end end + + describe 'create' do + let(:video_hash) { + { + title: 'Alf the movie', + overview: 'The most early 90s movie of all time', + release_date: '2837-01-12', + inventory: 6, + external_id: 987, + image_url: 'http://blahblah.com', + } + } + + it 'can create a valid video' do + # Assert + expect { + post videos_path, params: video_hash + }.must_change 'Video.count', 1 + + must_respond_with :ok + end + end end