Skip to content
Open
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ gem 'jbuilder', '~> 2.5'
# gem 'capistrano-rails', group: :development

gem 'will_paginate'
gem 'rack-cors'

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
Expand Down
6 changes: 4 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,9 @@ GEM
slop (~> 3.4)
pry-rails (0.3.4)
pry (>= 0.9.10)
puma (3.6.2)
puma (3.11.4)
rack (2.0.1)
rack-cors (1.0.2)
rack-test (0.6.3)
rack (>= 1.0)
rails (5.0.1)
Expand Down Expand Up @@ -210,6 +211,7 @@ DEPENDENCIES
minitest-spec-rails
pry-rails
puma (~> 3.0)
rack-cors
rails (~> 5.0.1)
sass-rails (~> 5.0)
spring
Expand All @@ -222,4 +224,4 @@ DEPENDENCIES
will_paginate

BUNDLED WITH
1.16.1
1.16.2
78 changes: 47 additions & 31 deletions app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,48 @@
class MoviesController < ApplicationController
before_action :require_movie, only: [:show]

def index
if params[:query]
data = MovieWrapper.search(params[:query])
else
data = Movie.all
end

render status: :ok, json: data
end

def show
render(
status: :ok,
json: @movie.as_json(
only: [:title, :overview, :release_date, :inventory],
methods: [:available_inventory]
)
)
end

private

def require_movie
@movie = Movie.find_by(title: params[:title])
unless @movie
render status: :not_found, json: { errors: { title: ["No movie with title #{params["title"]}"] } }
end
end
end
before_action :require_movie, only: [:show]

def index
if params[:query]
data = MovieWrapper.search(params[:query])
else
data = Movie.all
end

render status: :ok, json: data
end

def show
render(
status: :ok,
json: @movie.as_json(
only: [:title, :overview, :release_date, :inventory],
methods: [:available_inventory]
)
)
end

def create
movie = Movie.new(movie_params)
if movie.save
render json: movie.as_json(except: [:created_at, :updated_at], status: :ok)
else
render json: {
errors: movie.errors.messages
}, status: :bad_request
end
end


private

def require_movie
@movie = Movie.find_by(title: params[:title])
unless @movie
render status: :not_found, json: { errors: { title: ["No movie with title #{params["title"]}"] } }
end
end

def movie_params
params.permit(:title, :inventory, :release_date, :overview, :image_url)
end
end
12 changes: 7 additions & 5 deletions config/application.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require_relative 'boot'

require 'rack/cors'
require 'rails/all'

# Require the gems listed in Gemfile, including any gems
Expand All @@ -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' => '*',
'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, :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
1 change: 1 addition & 0 deletions node_modules/.bin/loose-envify

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 70 additions & 0 deletions node_modules/asap/CHANGES.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions node_modules/asap/LICENSE.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading