Skip to content
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@

# Ignore master key for decrypting credentials and more.
/config/master.key

.env
14 changes: 14 additions & 0 deletions app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion app/controllers/rentals_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions app/models/movie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 :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"
Expand Down
46 changes: 46 additions & 0 deletions test/controllers/movies_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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