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
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 @@ -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)
Expand Down Expand Up @@ -208,6 +209,7 @@ DEPENDENCIES
minitest-spec-rails
pry-rails
puma (~> 3.0)
rack-cors
rails (~> 5.0.1)
sass-rails (~> 5.0)
spring
Expand Down
29 changes: 23 additions & 6 deletions app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,25 @@ def index

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

def create

modified_params = movie_params
modified_params[:image_url] = modified_params[:image_url].gsub("https://image.tmdb.org/t/p/w185","")
movie = Movie.new(modified_params)

if movie.save
render status: :ok, json: { title: movie.title }
else
render status: :bad_request, json: { errors: movie.errors.messages }
end
end

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

def movie_params
params.require(:movie).permit(:id, :title, :overview, :release_date, :image_url, :external_id)
end
end
3 changes: 3 additions & 0 deletions app/models/movie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ class Movie < ApplicationRecord
has_many :rentals
has_many :customers, through: :rentals

validates_uniqueness_of :title, :scope => [:release_date]


def available_inventory
self.inventory - Rental.where(movie: self, returned: false).length
end
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' => '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
4 changes: 3 additions & 1 deletion lib/movie_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,18 @@ def self.search(query)
private

def self.construct_movie(api_result)

Movie.new(
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["poster_path"] , #(api_result["poster_path"] ? self.construct_image_url(api_result["poster_path"]) : nil),
external_id: api_result["id"])
end

def self.construct_image_url(img_name)
return BASE_IMG_URL + DEFAULT_IMG_SIZE + img_name

end

end