Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,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)
Expand Down Expand Up @@ -209,6 +210,7 @@ DEPENDENCIES
pg (~> 0.18)
pry-rails
puma (~> 3.0)
rack-cors
rails (~> 5.0.1)
sass-rails (~> 5.0)
spring
Expand Down
20 changes: 19 additions & 1 deletion app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,21 @@ def show
def create
# check if the movie is already in the rails db by checking if there is a movie in the rails db with the same title and release date from params (use find by)
# if the movie already exists in the rails db, then increase the inventory by 1
# set inventory to 1 when we create the movie in the rails db
# set inventory to 1 when we create the movie in the rails db

# movie = Movie.find_by(title: params[:title])
# if movie
# movie.inventory += 1
# else
movie = Movie.new(movie_params)
if movie.save
render status: :ok, json: {id: movie.id}
else
render status: :bad_request, json: { errors: movie.errors.messages }
end
# end


end


Expand All @@ -36,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.require.permit(:title, :overview, :release_date, :inventory, :image_url)
end
end
10 changes: 6 additions & 4 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down