diff --git a/Gemfile b/Gemfile index d5c7affd..d837ba0e 100644 --- a/Gemfile +++ b/Gemfile @@ -37,6 +37,8 @@ gem 'jbuilder', '~> 2.5' gem 'will_paginate' +gem 'rack-cors', :require => 'rack/cors' + group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platform: :mri diff --git a/Gemfile.lock b/Gemfile.lock index b39604fa..f80bcea9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -119,6 +119,7 @@ GEM pry (>= 0.9.10) puma (3.6.2) rack (2.0.1) + rack-cors (0.4.1) rack-test (0.6.3) rack (>= 1.0) rails (5.0.1) @@ -208,6 +209,7 @@ DEPENDENCIES minitest-spec-rails pry-rails puma (~> 3.0) + rack-cors rails (~> 5.0.1) sass-rails (~> 5.0) spring diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 362e2791..9df2c676 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -7,21 +7,42 @@ def index else data = Movie.all end - render status: :ok, json: data end + def create + print "In create method:" + + movie_data = { + title: movie_params[:title], + overview: movie_params[:overview], + release_date: movie_params[:release_date], + image_url: movie_params[:image_url][31..-1] + } + + movie = Movie.new(movie_data) + existing_movie = Movie.find_by(title: params[:title], release_date: params[:release_date]) + puts existing_movie + if existing_movie.nil? + movie.save + end + end + + def show render( - status: :ok, - json: @movie.as_json( - only: [:title, :overview, :release_date, :inventory], - methods: [:available_inventory] - ) - ) + status: :ok, + json: @movie.as_json( + only: [:title, :overview, :release_date, :inventory], + methods: [:available_inventory] + ) + ) end private + def movie_params + params.require(:movie).permit(:title, :overview, :release_date, :image_url) + end def require_movie @movie = Movie.find_by(title: params[:title]) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 92744380..1c0c2c72 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -1,7 +1,13 @@ class RentalsController < ApplicationController - before_action :require_movie, only: [:check_out, :check_in] + before_action :require_movie, only: [:check_out] + before_action :require_movie_checkin, only: [:check_in ] before_action :require_customer, only: [:check_out, :check_in] + def index + rentals = Rental.all + render status: :ok, json: rentals + end + def check_out rental = Rental.new(movie: @movie, customer: @customer, due_date: params[:due_date], returned: false) @@ -52,6 +58,13 @@ def require_movie end end + def require_movie_checkin + @movie = Movie.find_by id: params[:movie_id] + unless @movie + render status: :not_found, json: { errors: { title: ["No movie with id #{params[:movie_id]}"] } } + end + end + def require_customer @customer = Customer.find_by id: params[:customer_id] unless @customer diff --git a/app/models/movie.rb b/app/models/movie.rb index 0327a4d6..109107aa 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -14,6 +14,7 @@ def image_url MovieWrapper::DEFAULT_IMG_URL else MovieWrapper.construct_image_url(orig_value) + # return orig_value end end end diff --git a/config/application.rb b/config/application.rb index 2ac21a62..9f8a6f91 100644 --- a/config/application.rb +++ b/config/application.rb @@ -15,9 +15,11 @@ class Application < Rails::Application #this loads everything in the lib folder automatically config.eager_load_paths << Rails.root.join('lib') - config.action_dispatch.default_headers = { - 'Access-Control-Allow-Origin' => 'http://localhost:8081', - 'Access-Control-Request-Method' => %w{GET POST OPTIONS}.join(",") - } + config.middleware.insert_before 0, Rack::Cors do + allow do + origins '*' + resource '*', :headers => :any, :methods => [:get, :post, :put, :delete, :options] + end + end end end diff --git a/config/routes.rb b/config/routes.rb index 54bf033e..ba266ced 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,9 +4,11 @@ resources :customers, only: [:index] resources :movies, only: [:index, :show], param: :title - + post "/movies", to: "movies#create" + get "/rentals", to: "rentals#index" + # post "/rentals", to: "rentals#checkout" post "/rentals/:title/check-out", to: "rentals#check_out", as: "check_out" - post "/rentals/:title/return", to: "rentals#check_in", as: "check_in" + post "/rentals/:movie_id/return", to: "rentals#check_in", as: "check_in" get "/rentals/overdue", to: "rentals#overdue", as: "overdue"