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
15 changes: 15 additions & 0 deletions app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ def index
render status: :ok, json: data
end

def create
movie = Movie.new(movie_params)
puts params[:movie].to_hash
if movie.save
render status: :ok, json: { id: movie.id }
else
render status: :bad_request, json: { errors: movie.errors.messages }
end

end

def show
render(
status: :ok,
Expand All @@ -29,4 +40,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(:title, :inventory, :overview, :release_date, :image_url)
end
end
2 changes: 2 additions & 0 deletions app/models/movie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ def available_inventory

def image_url
orig_value = read_attribute :image_url
puts orig_value.class
# if orig_value == nil
if !orig_value
MovieWrapper::DEFAULT_IMG_URL
else
Expand Down
8 changes: 7 additions & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ class Application < Rails::Application

config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
origins '*', 'localhost/8081'
resource '*', :headers => :any, :methods => [:get, :post, :put, :delete, :options]
end
# Allow CORS (cross origin resource sharing)
# Read More: https://demisx.github.io/rails-api/2014/02/18/configure-accept-headers-cors.html
# config.Access-Control-Allow-Origin.action_dispatch.default_headers = {
# Access-Control-Allow-Origin = 'http://my-web-service-consumer-site.com'
# Access-Control-Request-Method = %w{GET POST OPTIONS}.join(",")

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
5 changes: 4 additions & 1 deletion lib/movie_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ def self.construct_movie(api_result)
end

def self.construct_image_url(img_name)
return BASE_IMG_URL + DEFAULT_IMG_SIZE + img_name
puts "BASE_IMG_URL"
img_url = BASE_IMG_URL + DEFAULT_IMG_SIZE + img_name
puts "constructed url for image: #{img_url}"
return img_url
end

end