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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
17 changes: 17 additions & 0 deletions app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
class MoviesController < ApplicationController
before_action :require_movie, only: [:show]

def create
new_movie = Movie.new(movie_params)
new_movie.inventory = 5

if new_movie.save
render status: :ok, json: {}
return
else
render status: :bad_request, json: { errors: new_movie.errors.messages }
return
end
end

def index
if params[:query]
data = MovieWrapper.search(params[:query])
Expand Down Expand Up @@ -29,4 +42,8 @@ def require_movie
render status: :not_found, json: { errors: { title: ["No movie with title #{params["title"]}"] } }
end
end

def movie_params
params.permit(:title, :overview, :release_date, :image_url, :external_id)
end
end
2 changes: 2 additions & 0 deletions app/models/movie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ class Movie < ApplicationRecord
has_many :rentals
has_many :customers, through: :rentals

validates :title, presence: 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
2 changes: 1 addition & 1 deletion node_modules/.bin/uglifyjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading