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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,4 @@ gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem 'httparty'

gem "active_model_serializers"
gem 'rack-cors', require: 'rack/cors'
12 changes: 7 additions & 5 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ GEM
coffee-script-source
execjs
coffee-script-source (1.12.2)
concurrent-ruby (1.0.4)
concurrent-ruby (1.0.5)
debug_inspector (0.0.3)
dotenv (2.2.0)
dotenv-rails (2.2.0)
Expand Down Expand Up @@ -118,8 +118,9 @@ GEM
slop (~> 3.4)
pry-rails (0.3.4)
pry (>= 0.9.10)
puma (3.6.2)
rack (2.0.1)
puma (3.11.4)
rack (2.0.5)
rack-cors (1.0.2)
rack-test (0.6.3)
rack (>= 1.0)
rails (5.0.1)
Expand Down Expand Up @@ -163,7 +164,7 @@ GEM
spring-watcher-listen (2.0.1)
listen (>= 2.7, < 4.0)
spring (>= 1.2, < 3.0)
sprockets (3.7.1)
sprockets (3.7.2)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
sprockets-rails (3.2.0)
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
13 changes: 13 additions & 0 deletions app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ def show
)
end

def create
movie = Movie.create(movies_params)
if movie.valid?
render json: { id: movie.id }, status: :ok
else
render json: { errors: movie.errors }, status: :bad_request
end
end

private

def require_movie
Expand All @@ -29,4 +38,8 @@ def require_movie
render status: :not_found, json: { errors: { title: ["No movie with title #{params["title"]}"] } }
end
end

def movies_params
return params.permit(:title, :overview, :release_date, :image_url, :external_id)
end
end
2 changes: 1 addition & 1 deletion app/models/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ class Customer < ApplicationRecord
has_many :movies, through: :rentals

def movies_checked_out_count
self.rentals.where(returned: false).length
self.rentals.count
end
end
1 change: 1 addition & 0 deletions app/models/movie.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class Movie < ApplicationRecord
has_many :rentals
has_many :customers, through: :rentals
validates_uniqueness_of :title

def available_inventory
self.inventory - Rental.where(movie: self, returned: false).length
Expand Down
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' => '*',
'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
3 changes: 1 addition & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@

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"
get "/rentals/overdue", to: "rentals#overdue", as: "overdue"


end