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

Trang & Shelan - VideoStoreConsumer API #8

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
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
Binary file added .DS_Store
Binary file not shown.
57 changes: 37 additions & 20 deletions app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,49 @@
class MoviesController < ApplicationController
before_action :require_movie, only: [:show]
before_action :require_movie, only: [:show]

def index
if params[:query]
data = MovieWrapper.search(params[:query])
else
data = Movie.all
end
def create
movie = Movie.new(movie_params)

render status: :ok, json: data
end
if movie.save
render json: movie.as_json(only: [:id]), status: :ok
else
render json: {
errors: movie.errors.messages
}, status: :bad_request
end
end

def show
render(
status: :ok,
json: @movie.as_json(
only: [:title, :overview, :release_date, :inventory],
methods: [:available_inventory]
)
)
end
def index
if params[:query]
data = MovieWrapper.search(params[:query])
else
data = Movie.all
end

render status: :ok, json: data
end

private

def require_movie
def show
render(
status: :ok,
json: @movie.as_json(
only: [:title, :overview, :release_date, :inventory],
methods: [:available_inventory]
)
)
end

private

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

def movie_params
params.permit(:title, :overview, :release_date, :image_url, :external_id)
end
end
4 changes: 4 additions & 0 deletions app/models/movie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ class Movie < ApplicationRecord
has_many :rentals
has_many :customers, through: :rentals

validates :external_id, :presence => true, :uniqueness => true

def available_inventory
self.inventory - Rental.where(movie: self, returned: false).length
end
Expand All @@ -16,4 +18,6 @@ def image_url
orig_value
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
53 changes: 28 additions & 25 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

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "customers", force: :cascade do |t|
t.string "name"
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"
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"
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