diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 362e2791..70f478dd 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -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: :created + return + else + render json: {ok: false, errors: new_movie.errors.messages}, status: :bad_request + return + end + end + private def require_movie @@ -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 + params.permit(:title, :release_date, :overview, :inventory) + end end diff --git a/config/routes.rb b/config/routes.rb index f4c99688..76715f9a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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"