diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 362e2791..b4126a84 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -21,6 +21,27 @@ def show ) end + def create + movie = Movie.find_by(title: params[:title]) + if !movie + movie = Movie.new(movie_params) + movie.inventory = 1 + else + movie.inventory += 1 + end + if movie.save + render( + status: :ok, + json: movie.as_json( + only: [:id, :title, :overview, :release_date, :inventory, :external_id, :image_url], + ) + ) + else + render json: { errors: movie.errors.messages }, + status: :bad_request + end + end + private def require_movie @@ -29,4 +50,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 diff --git a/app/models/movie.rb b/app/models/movie.rb index 0016080b..b8492c0d 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -10,7 +10,7 @@ def image_url orig_value = read_attribute :image_url if !orig_value MovieWrapper::DEFAULT_IMG_URL - elsif external_id + elsif !orig_value.include?("https://image.tmdb.org/t/p/") && external_id MovieWrapper.construct_image_url(orig_value) else orig_value diff --git a/config/routes.rb b/config/routes.rb index f4c99688..b27c0c5e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,6 +4,7 @@ resources :customers, only: [:index] resources :movies, only: [:index, :show], param: :title + post "/movies", to: "movies#create" post "/rentals/:title/check-out", to: "rentals#check_out", as: "check_out" post "/rentals/:title/return", to: "rentals#check_in", as: "check_in" diff --git a/lib/movie_wrapper.rb b/lib/movie_wrapper.rb index 0152c40e..5142772d 100644 --- a/lib/movie_wrapper.rb +++ b/lib/movie_wrapper.rb @@ -27,7 +27,7 @@ def self.construct_movie(api_result) title: api_result["title"], overview: api_result["overview"], release_date: api_result["release_date"], - image_url: api_result["poster_path"], #(api_result["poster_path"] ? self.construct_image_url(api_result["poster_path"]) : nil), + image_url: api_result["poster_path"], external_id: api_result["id"]) end