diff --git a/Gemfile b/Gemfile index d5c7affd..d837ba0e 100644 --- a/Gemfile +++ b/Gemfile @@ -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 diff --git a/Gemfile.lock b/Gemfile.lock index b39604fa..f80bcea9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -208,6 +209,7 @@ DEPENDENCIES minitest-spec-rails pry-rails puma (~> 3.0) + rack-cors rails (~> 5.0.1) sass-rails (~> 5.0) spring diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 362e2791..6e10d8a7 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -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 @@ -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 diff --git a/app/serializers/movie_serializer.rb b/app/serializers/movie_serializer.rb index 8a1cdc35..c1871302 100644 --- a/app/serializers/movie_serializer.rb +++ b/app/serializers/movie_serializer.rb @@ -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 diff --git a/config/application.rb b/config/application.rb index 2ac21a62..9f8a6f91 100644 --- a/config/application.rb +++ b/config/application.rb @@ -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 diff --git a/config/routes.rb b/config/routes.rb index 54bf033e..954e941d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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" diff --git a/lib/movie_wrapper.rb b/lib/movie_wrapper.rb index 7bd05c0e..0e0c2b62 100644 --- a/lib/movie_wrapper.rb +++ b/lib/movie_wrapper.rb @@ -1,3 +1,5 @@ +# require 'httparty' + class MovieWrapper BASE_URL = "https://api.themoviedb.org/3/" KEY = ENV["MOVIEDB_KEY"] @@ -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 @@ -18,6 +21,8 @@ def self.search(query) end return movies end + + puts "end of search of movieWrapper" end private