Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@

# Ignore master key for decrypting credentials and more.
/config/master.key
/node_modules
18 changes: 18 additions & 0 deletions app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ def index
render status: :ok, json: data
end

def create
@movie = Movie.new(movie_params)
# byebug
if @movie.save
render json: @movie.as_json(only: [:id]), status: :created
return
else
render json: {
errors: @movie.errors.messages,
}, status: :bad_request
return
end
end

def show
render(
status: :ok,
Expand All @@ -29,4 +43,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)
end
end
6 changes: 4 additions & 2 deletions app/controllers/rentals_controller.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
require 'date'


class RentalsController < ApplicationController
before_action :require_movie, only: [:check_out, :check_in]
before_action :require_customer, only: [:check_out, :check_in]

# 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])

rental = Rental.new(movie: @movie, customer: @customer, due_date: Date.today + 5)
if rental.save
render status: :ok, json: {}
else
Expand Down
1 change: 1 addition & 0 deletions app/models/movie.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class Movie < ApplicationRecord
has_many :rentals
has_many :customers, through: :rentals
validates :title, presence: true, uniqueness: true

def available_inventory
self.inventory - Rental.where(movie: self, returned: false).length
Expand Down
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

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"
get "/rentals/overdue", to: "rentals#overdue", as: "overdue"
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"express": "~4.13.1",
"jade": "~1.11.0",
"morgan": "~1.9.1",
"react-router-dom": "^6.0.0-beta.0",
"sequelize": "^5.1.0",
"serve-favicon": "~2.3.0"
},
Expand Down
32 changes: 32 additions & 0 deletions test/controllers/movies_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,36 @@ class MoviesControllerTest < ActionDispatch::IntegrationTest

end
end

describe "create" do

let(:movie_data) {
{
movie: {
title: "Some Movie",
overview: "Some Overview",
release_date: "2017-01-11",
inventory: 10
}
}
}
it "cannot create video if no title" do
movie_data[:movie][:title] = nil

expect {
post movies_path, params: movie_data
}.must_differ "Movie.count", 0

must_respond_with :bad_request
end

it "can create a movie" do

expect {
post movies_path, params: movie_data[:movie]
}.must_differ 'Movie.count', 1

must_respond_with :success
end
end
end
32 changes: 32 additions & 0 deletions test/models/movie_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,36 @@ class MovieTest < ActiveSupport::TestCase
after_ai.must_equal before_ai + 1
end
end

describe "validations" do
it "is invalid without a title" do
movie = Movie.new(
title: nil
)

expect(movie.valid?).must_equal false
expect(movie.errors.messages).must_include :title
end

it "is valid for a movie with all required fields" do
movie = Movie.new(
title: "Test Movie"
)
expect(movie.valid?).must_equal true
end

it "won't add movie twice" do
movie = Movie.new(
title: "Test Movie"
)
movie.save
expect(movie.valid?).must_equal true

movie2 = Movie.new(
title: "Test Movie"
)
movie2.save
expect(movie2.valid?).must_equal false
end
end
end
Loading