diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 362e2791..28adefad 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -11,6 +11,17 @@ def index render status: :ok, json: data end + def create + movie = Movie.new(movie_params) + puts params[:movie].to_hash + if movie.save + render status: :ok, json: { id: movie.id } + else + render status: :bad_request, json: { errors: movie.errors.messages } + end + + end + def show render( status: :ok, @@ -29,4 +40,8 @@ def require_movie render status: :not_found, json: { errors: { title: ["No movie with title #{params["title"]}"] } } end end + + def movie_params + params.require(:movie).permit(:title, :inventory, :overview, :release_date, :image_url) + end end diff --git a/app/models/movie.rb b/app/models/movie.rb index 0327a4d6..302369ee 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -10,6 +10,8 @@ def available_inventory def image_url orig_value = read_attribute :image_url + puts orig_value.class + # if orig_value == nil if !orig_value MovieWrapper::DEFAULT_IMG_URL else diff --git a/config/application.rb b/config/application.rb index 9f8a6f91..6c5922ac 100644 --- a/config/application.rb +++ b/config/application.rb @@ -17,9 +17,15 @@ class Application < Rails::Application config.middleware.insert_before 0, Rack::Cors do allow do - origins '*' + origins '*', 'localhost/8081' resource '*', :headers => :any, :methods => [:get, :post, :put, :delete, :options] end +# Allow CORS (cross origin resource sharing) +# Read More: https://demisx.github.io/rails-api/2014/02/18/configure-accept-headers-cors.html + # config.Access-Control-Allow-Origin.action_dispatch.default_headers = { + # Access-Control-Allow-Origin = 'http://my-web-service-consumer-site.com' + # Access-Control-Request-Method = %w{GET POST OPTIONS}.join(",") + end end end diff --git a/config/routes.rb b/config/routes.rb index 54bf033e..8eff1ff6 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" diff --git a/lib/movie_wrapper.rb b/lib/movie_wrapper.rb index 7bd05c0e..d8f19a73 100644 --- a/lib/movie_wrapper.rb +++ b/lib/movie_wrapper.rb @@ -32,7 +32,10 @@ def self.construct_movie(api_result) end def self.construct_image_url(img_name) - return BASE_IMG_URL + DEFAULT_IMG_SIZE + img_name + puts "BASE_IMG_URL" + img_url = BASE_IMG_URL + DEFAULT_IMG_SIZE + img_name + puts "constructed url for image: #{img_url}" + return img_url end end