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 Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
group :development, :test do
gem 'pry-rails'
gem 'dotenv-rails'
gem 'better_errors'
end

group :test do
Expand Down
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ GEM
ansi (1.5.0)
arel (9.0.0)
awesome_print (1.8.0)
better_errors (2.5.0)
coderay (>= 1.0.0)
erubi (>= 1.0.0)
rack (>= 0.9.0)
bootsnap (1.3.2)
msgpack (~> 1.0)
builder (3.2.3)
Expand Down Expand Up @@ -177,6 +181,7 @@ PLATFORMS
DEPENDENCIES
active_model_serializers
awesome_print
better_errors
bootsnap (>= 1.1.0)
byebug
dotenv-rails
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/customers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def index

render json: data.as_json(
only: [:id, :name, :registered_at, :address, :city, :state, :postal_code, :phone, :account_credit],
methods: [:movies_checked_out_count]
methods: [:movies_checked_out_count, :movies_checked_out]
)
end

Expand Down
19 changes: 19 additions & 0 deletions app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@


class MoviesController < ApplicationController
before_action :require_movie, only: [:show]

Expand All @@ -21,6 +23,23 @@ def show
)
end

def create
params["image_url"].slice! "https://image.tmdb.org/t/p/w185"

movie = Movie.new(
title: params["title"],
overview: params["overview"],
release_date: params["release_date"],
image_url: params["image_url"],
external_id: params["external_id"])

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

private

def require_movie
Expand Down
15 changes: 15 additions & 0 deletions app/models/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,19 @@ class Customer < ApplicationRecord
def movies_checked_out_count
self.rentals.where(returned: false).length
end

def movies_checked_out
outstanding_rentals = self.rentals.where(returned: false)

checked_out = outstanding_rentals.map do |rental|
{
title: rental.movie.title,
checkout_date: rental.checkout_date,
due_date: rental.due_date,
overdue: (rental.due_date < Date.today ? true : false)
}
end

return checked_out
end
end
2 changes: 2 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, uniqueness: true

def available_inventory
self.inventory - Rental.where(movie: self, returned: false).length
end
Expand Down
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
resources :movies, only: [:index, :show,], param: :title
post "/movies/", to: "movies#create", as: "add_to_library"

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