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
16 changes: 16 additions & 0 deletions app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ def show
)
end

def create
new_movie = Movie.new(movie_params)
if new_movie.save
render json: new_movie.as_json(only: [:id]), status: :ok
else
render json: {
ok: false,
erros: new_movie.errors.messages
}, status: :bad_request
end
end

private

def require_movie
Expand All @@ -29,4 +41,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, :available_inventory, :image_url,)
end
end
7 changes: 7 additions & 0 deletions app/models/movie.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Movie < ApplicationRecord
validates :title, uniqueness: true, presence: true

has_many :rentals
has_many :customers, through: :rentals

Expand All @@ -16,4 +18,9 @@ def image_url
MovieWrapper.construct_image_url(raw_value)
end
end

def initialize(params)
super(params)
self.inventory = 1
end
end
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