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 @@ -5,6 +5,7 @@ git_source(:github) do |repo_name|
"https://github.com/#{repo_name}.git"
end

gem 'rack-cors', require: 'rack/cors'
gem 'awesome_print'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.0.1'
Expand Down
4 changes: 3 additions & 1 deletion 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 Down
14 changes: 14 additions & 0 deletions app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ def show
)
end

def create
movie = MovieWrapper.construct_movie(movie_params)
if movie.save
render json: movie.as_json(only: [:id]), status: :ok
else
render json: { ok: false, errors: movie.errors },
status: :bad_request
end
end

private

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

def movie_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.where(returned: [false, nil]).length
end
end
5 changes: 4 additions & 1 deletion app/models/movie.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
class Movie < ApplicationRecord
has_many :rentals
has_many :customers, through: :rentals
validates :title, uniqueness: true
# validates :overview, uniqueness: { scope: :title }

def available_inventory
self.inventory - Rental.where(movie: self, returned: false).length
return 0 if self.inventory.nil?
self.inventory - Rental.where(movie: self, returned: [false, nil]).length
end

def image_url
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: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@

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"


Expand Down
3 changes: 2 additions & 1 deletion lib/movie_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def self.search(query)
return []
else
movies = response["results"].map do |result|
result["image_url"] = result["poster_path"]
self.construct_movie(result)
end
return movies
Expand All @@ -27,7 +28,7 @@ def self.construct_movie(api_result)
title: api_result["title"],
overview: api_result["overview"],
release_date: api_result["release_date"],
image_url: api_result["poster_path"], #(api_result["poster_path"] ? self.construct_image_url(api_result["poster_path"]) : nil),
image_url: api_result["image_url"],
external_id: api_result["id"])
end

Expand Down