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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@
# Ignore Byebug command history file.
.byebug_history
.env
.DS_Store
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ git_source(:github) do |repo_name|
"https://github.com/#{repo_name}.git"
end

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

gem 'awesome_print'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.0.1'
Expand Down
6 changes: 4 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,9 @@ GEM
slop (~> 3.4)
pry-rails (0.3.4)
pry (>= 0.9.10)
puma (3.6.2)
puma (3.11.4)
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 @@ -210,6 +211,7 @@ DEPENDENCIES
minitest-spec-rails
pry-rails
puma (~> 3.0)
rack-cors
rails (~> 5.0.1)
sass-rails (~> 5.0)
spring
Expand All @@ -222,4 +224,4 @@ DEPENDENCIES
will_paginate

BUNDLED WITH
1.16.1
1.16.2
25 changes: 24 additions & 1 deletion app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,29 @@ def show
json: @movie.as_json(
only: [:title, :overview, :release_date, :inventory],
methods: [:available_inventory]
)
)
)
end


def create
movie = Movie.find_by(external_id: params[:external_id])
if !movie
movie = Movie.new(movie_params)
else
render json: { ok: false, errors: movie.errors },
status: :bad_request
end

if movie.save
render json: movie.as_json, status: :ok
else
render json: { ok: false, errors: movie.errors },
status: :bad_request
end
end


private

def require_movie
Expand All @@ -29,4 +48,8 @@ def require_movie
render status: :not_found, json: { errors: { title: ["No movie with title #{params["title"]}"] } }
end
end

def movie_params
return params.permit(:title, :overview, :release_date, :image_url, :external_id, :inventory)
end
end
3 changes: 1 addition & 2 deletions app/controllers/rentals_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ class RentalsController < ApplicationController

# TODO: make sure that wave 2 works all the way
def check_out
rental = Rental.new(movie: @movie, customer: @customer, due_date: params[:due_date])

rental = Rental.new(movie: @movie, customer: @customer, due_date: Date.today + 7, returned: false)
if rental.save
render status: :ok, json: {}
else
Expand Down
2 changes: 1 addition & 1 deletion app/models/movie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def image_url
orig_value = read_attribute :image_url
if !orig_value
MovieWrapper::DEFAULT_IMG_URL
elsif external_id
elsif external_id && !orig_value.include?("http")
MovieWrapper.construct_image_url(orig_value)
else
orig_value
Expand Down
1 change: 1 addition & 0 deletions app/serializers/movie_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class MovieSerializer < ActiveModel::Serializer
attribute :id, if: -> { object.id != nil }
attribute :available_inventory, if: -> { object.id != nil }

attributes :title, :overview, :release_date, :image_url, :external_id
end
7 changes: 7 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,12 @@ class Application < Rails::Application
'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
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