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
34 changes: 34 additions & 0 deletions app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# require 'movie_wrapper'
class MoviesController < ApplicationController
before_action :require_movie, only: [:show]

def index
if params[:query]
puts "about to call Movie Wrapper"
data = MovieWrapper.search(params[:query])
else
puts "about to show all movies in rails db"
data = Movie.all
end

Expand All @@ -21,6 +24,37 @@ def show
)
end

def create
p "in the create method of rails API"

movie = Movie.new(
title: params[:title],
overview: params[:overview],
release_date: params[:release_date],
image_url: params[:image_url],
inventory: 1
)

if movie.save
p "movie is in database"
render :json => movie.to_json, :status => :ok
end
end

# def create
# pet = Pet.new(
# name: params[:name],
# age: params[:age],
# breed: params[:breed],
# about: params[:about],
# vaccinated: params[:vaccinated]
# )
#
# if pet.save
# render :json => pet.to_json, :status => :ok
# end
# end

private

def require_movie
Expand Down
2 changes: 1 addition & 1 deletion app/serializers/movie_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class MovieSerializer < ActiveModel::Serializer
attribute :id, if: -> { object.id != nil }

attributes :title, :overview, :release_date, :image_url, :external_id
attributes :title, :overview, :release_date, :image_url, :external_id, :inventory
end
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: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

resources :movies, only: [:index, :show], param: :title

post "/movies", to: "movies#create", as: "add_movie"

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
7 changes: 6 additions & 1 deletion lib/movie_wrapper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# require 'httparty'

class MovieWrapper
BASE_URL = "https://api.themoviedb.org/3/"
KEY = ENV["MOVIEDB_KEY"]
Expand All @@ -8,8 +10,9 @@ class MovieWrapper

def self.search(query)
url = BASE_URL + "search/movie?api_key=" + KEY + "&query=" + query
# puts url

response = HTTParty.get(url)

if response["total_results"] == 0
return []
else
Expand All @@ -18,6 +21,8 @@ def self.search(query)
end
return movies
end

puts "end of search of movieWrapper"
end

private
Expand Down