diff --git a/.gitignore b/.gitignore index 4a494a75..3b91322a 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,5 @@ # Ignore master key for decrypting credentials and more. /config/master.key + +.env \ No newline at end of file diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 362e2791..190868ab 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -21,6 +21,16 @@ def show ) end + def create + movie = Movie.new(movie_params) + + if movie.save + render status: :ok, json: {} + else + render status: :bad_request, json: { errors: movie.errors.messages } + end + end + private def require_movie @@ -29,4 +39,8 @@ def require_movie render status: :not_found, json: { errors: { title: ["No movie with title #{params["title"]}"] } } end end + + def movie_params + return params.permit(:title, :overview, :release_date, :inventory, :image_url, :external_id) + end end diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 67e77073..92ee30a5 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -5,7 +5,7 @@ class RentalsController < ApplicationController # TODO: make sure that wave 2 works all the way def check_out rental = Rental.new(movie: @movie, customer: @customer, due_date: params[:due_date]) - + if rental.save render status: :ok, json: {} else diff --git a/app/models/movie.rb b/app/models/movie.rb index fda94941..b389c06c 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -2,6 +2,9 @@ class Movie < ApplicationRecord has_many :rentals has_many :customers, through: :rentals + validates :title, presence: true + validates :external_id, presence: true, uniqueness: true + def available_inventory self.inventory - Rental.where(movie: self, returned: false).length end diff --git a/config/routes.rb b/config/routes.rb index f4c99688..76715f9a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,7 +3,7 @@ resources :customers, only: [:index] - resources :movies, only: [:index, :show], param: :title + resources :movies, 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/movies_controller_test.rb b/test/controllers/movies_controller_test.rb index 9172cf6e..687f68fb 100644 --- a/test/controllers/movies_controller_test.rb +++ b/test/controllers/movies_controller_test.rb @@ -75,4 +75,50 @@ class MoviesControllerTest < ActionDispatch::IntegrationTest end end + + describe "create" do + before do + @valid_movie = { + title: "XXXXX", + overview: "XXXXXXXX", + release_date: "2017-01-11", + inventory: 10, + image_url: "https://lh3.googleusercontent.com/pw/ACtC-3dgueD28nFt8fbmnEVSWrDdgXdH4dy91CXoWO818YTNFQlfnH-GN1O9t3zX4UEOGH3cncMC2Ze9WfNm13ofTlzOV97WdprqYmUPbj5H0oTS7Qwi8QtAEV8RFNyrcCJy09V04GFZQySqt9yhxf2v37Cg=w401-h397-no?authuser=0", + external_id: 2342 + } + + @invalid_movie = { + title: nil, + overview: "XXXXXXXX", + release_date: "2017-01-11", + inventory: 10, + image_url: "https://lh3.googleusercontent.com/pw/ACtC-3dgueD28nFt8fbmnEVSWrDdgXdH4dy91CXoWO818YTNFQlfnH-GN1O9t3zX4UEOGH3cncMC2Ze9WfNm13ofTlzOV97WdprqYmUPbj5H0oTS7Qwi8QtAEV8RFNyrcCJy09V04GFZQySqt9yhxf2v37Cg=w401-h397-no?authuser=0", + external_id: 2342 + } + end + + it "can create a new valid movie instance/object" do + + expect{post movies_path, params: @valid_movie}.must_differ "Movie.count", 1 + must_respond_with :success + + last_movie = Movie.last + expect(last_movie.title).must_equal "XXXXX" + end + + it "cannot create an invalid movie" do + expect{post movies_path, params: @invalid_movie}.wont_change "Movie.count" + must_respond_with :bad_request + data = JSON.parse @response.body + data.must_include "errors" + end + + it "requires a valid movie title" do + post movies_path(@invalid_movie) + must_respond_with :bad_request + data = JSON.parse @response.body + data.must_include "errors" + data["errors"].must_include "title" + end + end end