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 @@ -9,6 +9,8 @@ end
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.0.1'
# Use sqlite3 as the database for Active Record
gem 'rack-cors'
# CORS
gem 'sqlite3'
# Use Puma as the app server
gem 'puma', '~> 3.0'
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
15 changes: 15 additions & 0 deletions app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,22 @@ def show
)
end

def create
# raise
movie = Movie.new(movie_params)
# puts "#{movie}"
if
movie.save
render status: :ok, json: {title: movie.title}
else
render status: :bad_request, json: { errors: movie.errors.messages }
end
end

private
def movie_params
params.require(:movie).permit(:title, :overview, :release_date, :image_url, :external_id)
end

def require_movie
@movie = Movie.find_by(title: params[:title])
Expand Down
6 changes: 6 additions & 0 deletions app/models/movie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ def available_inventory

def image_url
orig_value = read_attribute :image_url
puts "original value"
puts orig_value
if !orig_value
MovieWrapper::DEFAULT_IMG_URL
elsif orig_value.include? MovieWrapper::DEFAULT_IMG_SIZE
orig_value
else
MovieWrapper.construct_image_url(orig_value)
# puts "movie image"
# puts movie_image
end
end
end
9 changes: 9 additions & 0 deletions config/initializers/cors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Rails.application.config.middleware.insert_before 0, "Rack::Cors" do
allow do
origins 'localhost:8081'

resource '*',
headers: :any,
methods: [:get, :post, :put, :patch, :delete, :options, :head]
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
7 changes: 6 additions & 1 deletion lib/movie_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,21 @@ def self.search(query)
private

def self.construct_movie(api_result)
# puts "**********************"
# puts api_result["poster_path"]
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
# return img_name
end

end