Skip to content
Merged
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
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ gem 'puma', '~> 3.7'
# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible
# gem 'rack-cors'

gem "active_model_serializers"

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
Expand Down
9 changes: 9 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ GEM
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.3)
active_model_serializers (0.10.6)
actionpack (>= 4.1, < 6)
activemodel (>= 4.1, < 6)
case_transform (>= 0.2)
jsonapi-renderer (>= 0.1.1.beta1, < 0.2)
activejob (5.1.4)
activesupport (= 5.1.4)
globalid (>= 0.3.6)
Expand All @@ -48,6 +53,8 @@ GEM
debug_inspector (>= 0.0.1)
builder (3.2.3)
byebug (9.1.0)
case_transform (0.2)
activesupport
coderay (1.1.2)
concurrent-ruby (1.0.5)
crass (1.0.2)
Expand All @@ -61,6 +68,7 @@ GEM
jquery-turbolinks (2.1.0)
railties (>= 3.1.0)
turbolinks
jsonapi-renderer (0.1.3)
listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
Expand Down Expand Up @@ -151,6 +159,7 @@ PLATFORMS
ruby

DEPENDENCIES
active_model_serializers
better_errors
binding_of_caller
byebug
Expand Down
29 changes: 29 additions & 0 deletions app/controllers/customers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,33 @@ def index
)
end

def current
checked_out(:current)
end

def history
checked_out(:history)
end

private

def checked_out(status)
customer = Customer.find_by(id: params[:id])
checked_out = customer.checked_out(status)

unless checked_out.empty?
render(
json: checked_out,
status: :ok
)
else
render(
json: { errors: {
records: ["No records found."]}
},
status: :not_found
)
end
end

end
28 changes: 27 additions & 1 deletion app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def show

if movie
render(
json: movie.as_json(only: [:title, :overview, :release_date, :inventory, :available_inventory]),
json: movie.as_json(only: [:available_inventory, :title, :overview, :inventory, :release_date]),
status: :ok
)
else
Expand Down Expand Up @@ -52,10 +52,36 @@ def create
end
end

def current
checked_out(:current)
end

def history
checked_out(:history)
end

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

def checked_out(status)
movie = Movie.find_by(id: params[:id])
checked_out = movie.checked_out(status)

unless checked_out.empty?
render(
json: checked_out,
status: :ok
)
else
render(
json: { errors: {
records: ["No records found."]}
},
status: :not_found
)
end
end

end
17 changes: 16 additions & 1 deletion app/models/customer.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
class Customer < ApplicationRecord
has_many :rentals
has_many :movies, through: :rentals
# has_many :movies, through: :rentals


validates :name, presence: true

def checked_out(status)
checked_out = []
self.rentals.each do |rental|
if (status == :current && rental.checkin_date == nil) || (status == :history && rental.checkin_date)
info = {
checkout_date: rental.checkout_date,
due_date: rental.due_date,
title: rental.movie.title,
}
checked_out << info
end
end
return checked_out
end

end
22 changes: 21 additions & 1 deletion app/models/movie.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
class Movie < ApplicationRecord
has_many :rentals
has_many :customers, through: :rentals
# has_many :customers, through: :rentals

validates :title, presence: true
validates :inventory, numericality: { greater_than: 0 }

# scope :current, -> { where(checkin_date: nil) }
#is there a way to do scope for a specific instance?

# attribute :available_inventory, :integer, default: :inventory

after_create :set_avail_inv_attribute
Expand All @@ -13,5 +16,22 @@ def set_avail_inv_attribute
self.update_columns(available_inventory: self.inventory) if available_inventory.nil?
end

def checked_out(status)
checked_out = []
self.rentals.each do |rental|
if (status == :current && rental.checkin_date == nil) || (status == :history && rental.checkin_date)
info = {
customer_id: rental.customer_id,
checkout_date: rental.checkout_date,
due_date: rental.due_date,
name: rental.customer.name,
postal_code: rental.customer.postal_code
}
checked_out << info
end
end
return checked_out
end


end
3 changes: 3 additions & 0 deletions app/serializers/movie_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class MovieSerializer < ActiveModel::Serializer
# attributes :available_inventory, :title, :overview, :inventory, :release_date
end
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
resources :movies, only: [:index, :show, :create]
get '/movies/:id/current', to:'movies#current', as: 'current_rentals_movie'
get '/movies/:id/history', to:'movies#history', as: 'historic_rentals_movie'
resources :customers, only: [:index]
get '/customers/:id/current', to:'customers#current', as: 'current_rentals_customer' #these are dumb path names...
get '/customers/:id/history', to:'customers#history', as: 'historic_rentals_customer'

get '/rentals/overdue', to: 'rentals#overdue', as: "overdue"
post 'rentals/check-out', to: 'rentals#checkout', as: "checkout"
Expand Down
53 changes: 53 additions & 0 deletions test/controllers/customers_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,57 @@
end
end

describe "current" do
it "returns an array of current rentals for one customer" do
get current_rentals_customer_path(customers(:Shelley).id)
must_respond_with :success

body = JSON.parse(response.body)
body.must_be_kind_of Array
body[0]["title"].must_equal movies(:psycho).title
end


it "returns not_found when no rentals exist" do
get current_rentals_customer_path(customers(:Curran).id)
must_respond_with :not_found

body = JSON.parse(response.body)
body.must_equal "errors"=>{"records"=>["No records found."]}
end

it "returns the correct information" do
get current_rentals_customer_path(customers(:Shelley).id)
body = JSON.parse(response.body)

body[0].keys.sort.must_equal ["checkout_date", "due_date", "title"]
end
end

describe "history" do
it "returns an array of historic rentals for one movie" do
get historic_rentals_customer_path(customers(:Shelley).id)
must_respond_with :success

body = JSON.parse(response.body)
body.must_be_kind_of Array
body[0]["title"].must_equal movies(:jaws).title
end

it "returns not_found when movie doesn't exist" do
get historic_rentals_customer_path(customers(:Curran).id)
must_respond_with :not_found

body = JSON.parse(response.body)
body.must_equal "errors"=>{"records"=>["No records found."]}
end

it "returns the correct information" do
get historic_rentals_customer_path(customers(:Shelley).id)
body = JSON.parse(response.body)

body[0].keys.sort.must_equal ["checkout_date", "due_date", "title"]
end
end

end
59 changes: 59 additions & 0 deletions test/controllers/movies_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@
body = JSON.parse(response.body)
body.must_equal "errors"=>{"id"=>["Movie with id 673001845 not found"]}
end

it "returns the correct information" do
get movie_path(movies(:jaws).id)
body = JSON.parse(response.body)

body.keys.sort.must_equal ["available_inventory", "inventory", "overview", "release_date", "title"]
end
end

describe "create" do
Expand Down Expand Up @@ -77,4 +84,56 @@
body.must_equal "errors" => {"inventory" => ["is not a number"]}
end
end

describe "current" do
it "returns an array of current rentals for one movie" do
get current_rentals_movie_path(movies(:jaws).id)
must_respond_with :success

body = JSON.parse(response.body)
body.must_be_kind_of Array
body[0]["customer_id"].must_equal rentals(:rental_two).customer_id
end

it "returns not_found when no rentals exist" do
get current_rentals_movie_path(movies(:lambs).id)
must_respond_with :not_found

body = JSON.parse(response.body)
body.must_equal "errors"=>{"records"=>["No records found."]}
end

it "returns the correct information" do
get current_rentals_movie_path(movies(:jaws).id)
body = JSON.parse(response.body)

body[0].keys.sort.must_equal ["checkout_date", "customer_id", "due_date", "name", "postal_code"]
end
end

describe "history" do
it "returns an array of historic rentals for one movie" do
get historic_rentals_movie_path(movies(:jaws).id)
must_respond_with :success

body = JSON.parse(response.body)
body.must_be_kind_of Array
body[0]["customer_id"].must_equal rentals(:rental_three).customer_id
end

it "returns not_found when movie doesn't exist" do
get historic_rentals_movie_path(movies(:lambs).id)
must_respond_with :not_found

body = JSON.parse(response.body)
body.must_equal "errors"=>{"records"=>["No records found."]}
end

it "returns the correct information" do
get historic_rentals_movie_path(movies(:jaws).id)
body = JSON.parse(response.body)

body[0].keys.sort.must_equal ["checkout_date", "customer_id", "due_date", "name", "postal_code"]
end
end
end
10 changes: 10 additions & 0 deletions test/fixtures/customers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,13 @@ Roanna:
postal_code: 15867
phone: (323) 336-1841
account_credit: 50.39

Curran:
name: Curran Stout
registered_at: Wed, 16 Apr 2014 21:40:20 -0700
address: 123 S 23rd St.
city: Seattle
state: WA
postal_code: 94267
phone: (908) 949-6758
movies_checked_out_count: 0
10 changes: 8 additions & 2 deletions test/fixtures/movies.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ psycho:
overview: "When larcenous real estate clerk Marion Crane goes on the lam with a wad of cash and hopes of starting a new life, she ends up at the notorious Bates Motel, where manager Norman Bates cares for his housebound mother. The place seems quirky, but fine… until Marion decides to take a shower."
release_date: "1960-06-16"
inventory: 8

jaws:
title: "The Exorcist"
title: "Jaws"
overview: "12-year-old Regan MacNeil begins to adapt an explicit new personality as strange events befall the local area of Georgetown. Her mother becomes torn between science and superstition in a desperate bid to save her daughter, and ultimately turns to her last hope: Father Damien Karras, a troubled priest who is struggling with his own faith."
release_date: "1973-12-26"
inventory: 7

lambs:
title: "The Silence of the Lambs"
overview: "FBI trainee Clarice Starling ventures into a maximum-security asylum to pick the diseased brain of Hannibal Lecter, a psychiatrist turned homicidal cannibal. Starling needs clues to help her capture a serial killer. Unfortunately, her Faustian relationship with Lecter soon leads to his escape, and now two deranged killers are on the loose."
release_date: "1991-02-14"
inventory: 3
7 changes: 7 additions & 0 deletions test/fixtures/rentals.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,10 @@ rental_two:
movie: jaws
due_date: 2017-12-22
checkout_date: 2017-11-07

rental_three:
customer: Shelley
movie: jaws
due_date: 2017-12-22
checkout_date: 2017-11-07
checkin_date: 2017-12-07
17 changes: 17 additions & 0 deletions test/models/customer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,21 @@
end
end


describe "checked_out" do
it "returns an array with rentals" do
customer = customers(:Shelley)
current = customer.checked_out(:current)
current.must_be_kind_of Array
current[0].must_be_kind_of Hash
end

it "returns an empty array if nothing status if there are no applicable rentals" do
customer = customers(:Roanna)
current = customer.checked_out(:history)
current.must_be_kind_of Array
current.must_be :empty?
end
end

end
Loading