Skip to content

Commit

Permalink
created API endpoint to add movies to DB
Browse files Browse the repository at this point in the history
  • Loading branch information
shubha-rajan committed Jun 25, 2019
1 parent 181dd19 commit 9329e5e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
19 changes: 16 additions & 3 deletions app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,26 @@ def show
status: :ok,
json: @movie.as_json(
only: [:title, :overview, :release_date, :inventory],
methods: [:available_inventory]
)
)
methods: [:available_inventory],
),
)
end

def create
movie = Movie.new(movie_params)
if movie.save
render status: :ok, json: movie.as_json(only: [:id, :title, :overview, :release_date, :image_url, :inventory], methods: [:available_inventory])
else
render status: :bad_request, json: { errors: movie.errors.messages }
end
end

private

def movie_params
params.permit(:title, :overview, :inventory, :release_date, :image_url, :external_id)
end

def require_movie
@movie = Movie.find_by(title: params[:title])
unless @movie
Expand Down
5 changes: 2 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@

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"

root 'movies#index'

root "movies#index"
end

0 comments on commit 9329e5e

Please sign in to comment.