Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Severin & Laura -- Carets #6

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ git_source(:github) do |repo_name|
"https://github.com/#{repo_name}.git"
end

gem 'rack-cors', :require => 'rack/cors'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.0.1'
Expand Down
4 changes: 3 additions & 1 deletion 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 (1.0.2)
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 All @@ -220,4 +222,4 @@ DEPENDENCIES
will_paginate

BUNDLED WITH
1.14.6
1.15.4
37 changes: 31 additions & 6 deletions app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,37 @@ 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, :vote_average, :inventory],
methods: [:available_inventory]
)
)
end

def create
@movie = Movie.find_by(title: params[:title])

if @movie
@movie.inventory += 1
@movie.save
else
if params[:title]
@movie = MovieWrapper.search(params[:title])
@movie[0].inventory = 1

rating = ['G', 'PG', 'PG-13', 'R', 'NC-17']
@movie[0].rating = rating.sample

if @movie[0].save
render status: :ok, json: { errors: { title: ["Movie with title #{params["title"]} saved!"] } }
else
render status: :not_found, json: { errors: { title: ["No movie with title #{params["title"]}"] } }
end
else
render status: :not_found, json: { errors: { title: ["No movie with title #{params["title"]}"] } }
end
end
end

private
Expand Down
2 changes: 1 addition & 1 deletion app/models/movie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def image_url
elsif external_id
MovieWrapper.construct_image_url(orig_value)
else
orig_value
MovieWrapper.construct_image_url(orig_value)
end
end
end
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, :average_rating, :rating, :image_url, :external_id
end
10 changes: 9 additions & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,16 @@ class Application < Rails::Application
config.eager_load_paths << Rails.root.join('lib')

config.action_dispatch.default_headers = {
'Access-Control-Allow-Origin' => 'http://localhost:8081',
'Access-Control-Allow-Origin' => 'http://localhost:8080',
'Access-Control-Allow-Origin' => '*',
'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, :options]
end
end
end
end
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

resources :customers, only: [:index]

resources :movies, only: [:index, :show], param: :title
# added update route
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddRatingAndAverageRatingToMovie < ActiveRecord::Migration[5.0]
def change
add_column :movies, :rating, :string
add_column :movies, :average_rating, :float
end
end
8 changes: 5 additions & 3 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20170608201920) do
ActiveRecord::Schema.define(version: 20171220192206) do

create_table "customers", force: :cascade do |t|
t.string "name"
Expand All @@ -30,9 +30,11 @@
t.text "overview"
t.date "release_date"
t.integer "inventory"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "image_url"
t.string "rating"
t.float "average_rating"
end

create_table "rentals", force: :cascade do |t|
Expand Down
6 changes: 6 additions & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,11 @@

JSON.parse(File.read('db/seeds/movies.json')).each do |movie_data|
movies = MovieWrapper.search(movie_data["title"])

movies.first.inventory = movie_data["inventory"]

rating = ['G', 'PG', 'PG-13', 'R', 'NC-17']
movies.first.rating = rating.sample

movies.first.save unless movies.empty?
end
2 changes: 2 additions & 0 deletions lib/movie_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ def self.search(query)
private

def self.construct_movie(api_result)

Movie.new(
title: api_result["title"],
overview: api_result["overview"],
average_rating: api_result["vote_average"],
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),
external_id: api_result["id"])
Expand Down