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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
36 changes: 29 additions & 7 deletions app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'pry'
class MoviesController < ApplicationController
before_action :require_movie, only: [:show]

Expand All @@ -17,16 +18,37 @@ def show
json: @movie.as_json(
only: [:title, :overview, :release_date, :inventory],
methods: [:available_inventory]
)
)
)
end

private
def create
@movie = Movie.new(movie_params)

def require_movie
@movie = Movie.find_by(title: params[:title])
unless @movie
render status: :not_found, json: { errors: { title: ["No movie with title #{params["title"]}"] } }
@movie.image_url = movie_params["image_url"].slice(31..-1)

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


private

def movie_params
params.permit(:title, :overview, :release_date, :image_url, :external_id, :inventory)
end

def require_movie
@movie = Movie.find_by(title: params[:title])
unless @movie
render status: :not_found, json: { errors: { title: ["No movie with title #{params["title"]}"] } }
end
end
end
end
2 changes: 1 addition & 1 deletion app/controllers/rentals_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def check_out
rental = Rental.new(movie: @movie, customer: @customer, due_date: params[:due_date])

if rental.save
render status: :ok, json: {}
render status: :ok, json: {message: "Successfully checked out #{@movie.title} to #{@customer.name}"}
else
render status: :bad_request, json: { errors: rental.errors.messages }
end
Expand Down
2 changes: 1 addition & 1 deletion config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ default: &default
development:
<<: *default
database: <APPLICATION_NAME>_development
host: localhost
# ...
test:
<<: *default
Expand All @@ -24,4 +25,3 @@ production:
database: <APPLICATION_NAME>_production
username: <APPLICATION_NAME>
password: <%= ENV['<APPLICATION_NAME>_DATABASE_PASSWORD'] %>

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
59 changes: 31 additions & 28 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,43 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20180618042754) do
ActiveRecord::Schema.define(version: 2018_06_18_042754) do

create_table "customers", force: :cascade do |t|
t.string "name"
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "customers", id: :serial, force: :cascade do |t|
t.string "name"
t.datetime "registered_at"
t.string "address"
t.string "city"
t.string "state"
t.string "postal_code"
t.string "phone"
t.float "account_credit"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "address"
t.string "city"
t.string "state"
t.string "postal_code"
t.string "phone"
t.float "account_credit"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "movies", force: :cascade do |t|
t.string "title"
t.text "overview"
t.date "release_date"
t.integer "inventory"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "image_url"
t.integer "external_id"
create_table "movies", id: :serial, force: :cascade do |t|
t.string "title"
t.text "overview"
t.date "release_date"
t.integer "inventory"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "image_url"
t.integer "external_id"
end

create_table "rentals", force: :cascade do |t|
t.integer "customer_id"
t.integer "movie_id"
t.date "checkout_date"
t.date "due_date"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "returned"
create_table "rentals", id: :serial, force: :cascade do |t|
t.integer "customer_id"
t.integer "movie_id"
t.date "checkout_date"
t.date "due_date"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "returned"
t.index ["customer_id"], name: "index_rentals_on_customer_id"
t.index ["movie_id"], name: "index_rentals_on_movie_id"
end
Expand Down
1 change: 1 addition & 0 deletions node_modules/.bin/loose-envify

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions node_modules/@babel/runtime/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions node_modules/@babel/runtime/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

100 changes: 100 additions & 0 deletions node_modules/@babel/runtime/helpers/AsyncGenerator.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions node_modules/@babel/runtime/helpers/AwaitValue.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions node_modules/@babel/runtime/helpers/applyDecoratedDescriptor.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions node_modules/@babel/runtime/helpers/arrayWithHoles.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions node_modules/@babel/runtime/helpers/arrayWithoutHoles.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions node_modules/@babel/runtime/helpers/assertThisInitialized.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading