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

Ann and Regan's Fine Fine Video Store API #3

Open
wants to merge 61 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
0433510
Initial setup
pancake-batfish May 9, 2017
541278f
Generate customer, movie and rental models
pancake-batfish May 9, 2017
f28cc23
Add sketched-out routes to routes file
pancake-batfish May 9, 2017
25f84c6
added customer yml fixtures
gofarann May 9, 2017
4e9d845
Stub in tests for movie model
pancake-batfish May 9, 2017
253a37f
added tests to customer model
gofarann May 9, 2017
6ff5942
First movie validations tests
pancake-batfish May 9, 2017
2c6ea17
Add presence validations for title and inventory
pancake-batfish May 9, 2017
3f7ea24
customer model test
gofarann May 9, 2017
73f1289
Add tests and validations for uniqueness
pancake-batfish May 9, 2017
b485b74
Add inventory tests and validations for movie
pancake-batfish May 9, 2017
a4b7449
Add test for relation of movie to rental
pancake-batfish May 9, 2017
e9fd684
all model test validations passed thus far
gofarann May 9, 2017
bc3edac
merge conflicts
gofarann May 9, 2017
e7482be
Stub in rental model tests
pancake-batfish May 9, 2017
21d1345
Merge branch 'rental_tests'
pancake-batfish May 9, 2017
8c3bfc2
Add test to require associated movie for rental
pancake-batfish May 9, 2017
7daf8f7
Add chronic gem and defaults for rental model
pancake-batfish May 9, 2017
e9ef964
added rental model tests
gofarann May 9, 2017
4432ab3
merge deconflicted
gofarann May 9, 2017
95c9159
Generate rentals and customers controllers
pancake-batfish May 9, 2017
c79bf6c
Uncomment first routes
pancake-batfish May 9, 2017
f23cd92
added movie index controller method and tests
gofarann May 9, 2017
5244c59
Add tests and controller action for customer index
pancake-batfish May 10, 2017
2641f41
Add custom method to customers and return value in json for customer …
pancake-batfish May 10, 2017
55f2513
Merge branch 'customer_index'
pancake-batfish May 10, 2017
0df9bad
Rename constant in tests to avoid conflict
pancake-batfish May 10, 2017
cc59455
added test stubs for checkout method
gofarann May 10, 2017
eab2a5e
First test movie show
pancake-batfish May 10, 2017
4a16b11
Additional tests and action for movie show
pancake-batfish May 10, 2017
a8c2d9c
Add negative test for movie show
pancake-batfish May 10, 2017
94d9e28
Complete movie show tests
pancake-batfish May 10, 2017
a8d152b
Stub in tests for checkin
pancake-batfish May 10, 2017
184273b
First rental update test
pancake-batfish May 10, 2017
bc286bb
rental controller tests
gofarann May 10, 2017
ad93c60
Start to build out update method
pancake-batfish May 10, 2017
6342b68
more checkout tests
gofarann May 10, 2017
9589853
Add tests and logic for invalid customer id or movie title
pancake-batfish May 11, 2017
579c021
Complete checkin tests
pancake-batfish May 11, 2017
208b099
Add seed for rental
pancake-batfish May 11, 2017
2a4149a
more rental checkout contorller testing
gofarann May 11, 2017
88234c8
Merge pull request #1 from pancake-batfish/checkin
gofarann May 11, 2017
5b04d87
Merge branch 'master' into rental_post_checkout
pancake-batfish May 11, 2017
ffe2e2d
Merge pull request #2 from pancake-batfish/rental_post_checkout
pancake-batfish May 11, 2017
670d719
added decrease inventory logic
gofarann May 11, 2017
38c070c
Stub in tests for customers sort
pancake-batfish May 11, 2017
779f024
WIP sort tests
pancake-batfish May 11, 2017
24c9aae
Add movies checked out count method
pancake-batfish May 12, 2017
be2894f
added overdue controller tests
gofarann May 12, 2017
4105087
Add test and logic to increase movie inventory on checkin
pancake-batfish May 12, 2017
3268737
Revise tests and methods to use available_inventory rather than inven…
pancake-batfish May 12, 2017
6be0e6a
Merge branch 'optional_parameters'
pancake-batfish May 12, 2017
e8fb922
making changes in overdue method
gofarann May 12, 2017
6aea9c7
resolved overdue method errors
gofarann May 12, 2017
d9f0a4a
resolved merge conflicts
gofarann May 12, 2017
05d9189
Merge branch 'master' of https://github.com/pancake-batfish/VideoStor…
pancake-batfish May 12, 2017
b625ff9
Tests and action to accept sort parameter for customer index
pancake-batfish May 12, 2017
ee99fa7
fixed display for overdue
gofarann May 12, 2017
53d58d6
took out due date in past test
gofarann May 12, 2017
675237d
Merge branch 'master' of https://github.com/pancake-batfish/VideoStor…
pancake-batfish May 12, 2017
eeab146
Merge branch 'sort_customers'
pancake-batfish May 12, 2017
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.
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# See https://help.github.com/articles/ignoring-files for more about ignoring files.
#
# If you find yourself ignoring temporary files generated by your text editor
# or operating system, you probably want to add a global ignore instead:
# git config --global core.excludesfile '~/.gitignore_global'

# Ignore bundler config.
/.bundle

# Ignore all logfiles and tempfiles.
/log/*
/tmp/*
!/log/.keep
!/tmp/.keep

# Ignore Byebug command history file.
.byebug_history
56 changes: 56 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
source 'https://rubygems.org'

git_source(:github) do |repo_name|
repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
"https://github.com/#{repo_name}.git"
end


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.0.2'
# Use postgresql as the database for Active Record
gem 'pg', '~> 0.18'
# Use Puma as the app server
gem 'puma', '~> 3.0'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
# gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible
# gem 'rack-cors'

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platform: :mri
end

group :development do
gem 'listen', '~> 3.0.5'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

group :development do
gem 'better_errors'
gem 'binding_of_caller'
gem 'pry-rails'
end

gem 'awesome_print'
gem 'foundation-rails'
gem 'chronic'

group :test do
gem 'minitest-rails'
gem 'minitest-reporters'
end
181 changes: 181 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
GEM
remote: https://rubygems.org/
specs:
actioncable (5.0.2)
actionpack (= 5.0.2)
nio4r (>= 1.2, < 3.0)
websocket-driver (~> 0.6.1)
actionmailer (5.0.2)
actionpack (= 5.0.2)
actionview (= 5.0.2)
activejob (= 5.0.2)
mail (~> 2.5, >= 2.5.4)
rails-dom-testing (~> 2.0)
actionpack (5.0.2)
actionview (= 5.0.2)
activesupport (= 5.0.2)
rack (~> 2.0)
rack-test (~> 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
actionview (5.0.2)
activesupport (= 5.0.2)
builder (~> 3.1)
erubis (~> 2.7.0)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.3)
activejob (5.0.2)
activesupport (= 5.0.2)
globalid (>= 0.3.6)
activemodel (5.0.2)
activesupport (= 5.0.2)
activerecord (5.0.2)
activemodel (= 5.0.2)
activesupport (= 5.0.2)
arel (~> 7.0)
activesupport (5.0.2)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (~> 0.7)
minitest (~> 5.1)
tzinfo (~> 1.1)
ansi (1.5.0)
arel (7.1.4)
awesome_print (1.7.0)
babel-source (5.8.35)
babel-transpiler (0.7.0)
babel-source (>= 4.0, < 6)
execjs (~> 2.0)
better_errors (2.1.1)
coderay (>= 1.0.0)
erubis (>= 2.6.6)
rack (>= 0.9.0)
binding_of_caller (0.7.2)
debug_inspector (>= 0.0.1)
builder (3.2.3)
byebug (9.0.6)
chronic (0.10.2)
coderay (1.1.1)
concurrent-ruby (1.0.5)
debug_inspector (0.0.3)
erubis (2.7.0)
execjs (2.7.0)
ffi (1.9.18)
foundation-rails (6.3.1.0)
railties (>= 3.1.0)
sass (>= 3.3.0, < 3.5)
sprockets-es6 (>= 0.9.0)
globalid (0.4.0)
activesupport (>= 4.2.0)
i18n (0.8.1)
listen (3.0.8)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
loofah (2.0.3)
nokogiri (>= 1.5.9)
mail (2.6.5)
mime-types (>= 1.16, < 4)
method_source (0.8.2)
mime-types (3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2016.0521)
mini_portile2 (2.1.0)
minitest (5.10.1)
minitest-rails (3.0.0)
minitest (~> 5.8)
railties (~> 5.0)
minitest-reporters (1.1.14)
ansi
builder
minitest (>= 5.0)
ruby-progressbar
nio4r (2.0.0)
nokogiri (1.7.1)
mini_portile2 (~> 2.1.0)
pg (0.20.0)
pry (0.10.4)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
pry-rails (0.3.6)
pry (>= 0.10.4)
puma (3.8.2)
rack (2.0.2)
rack-test (0.6.3)
rack (>= 1.0)
rails (5.0.2)
actioncable (= 5.0.2)
actionmailer (= 5.0.2)
actionpack (= 5.0.2)
actionview (= 5.0.2)
activejob (= 5.0.2)
activemodel (= 5.0.2)
activerecord (= 5.0.2)
activesupport (= 5.0.2)
bundler (>= 1.3.0, < 2.0)
railties (= 5.0.2)
sprockets-rails (>= 2.0.0)
rails-dom-testing (2.0.2)
activesupport (>= 4.2.0, < 6.0)
nokogiri (~> 1.6)
rails-html-sanitizer (1.0.3)
loofah (~> 2.0)
railties (5.0.2)
actionpack (= 5.0.2)
activesupport (= 5.0.2)
method_source
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rake (12.0.0)
rb-fsevent (0.9.8)
rb-inotify (0.9.8)
ffi (>= 0.5.0)
ruby-progressbar (1.8.1)
sass (3.4.23)
slop (3.6.0)
spring (2.0.1)
activesupport (>= 4.2)
spring-watcher-listen (2.0.1)
listen (>= 2.7, < 4.0)
spring (>= 1.2, < 3.0)
sprockets (3.7.1)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
sprockets-es6 (0.9.2)
babel-source (>= 5.8.11)
babel-transpiler
sprockets (>= 3.0.0)
sprockets-rails (3.2.0)
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets (>= 3.0.0)
thor (0.19.4)
thread_safe (0.3.6)
tzinfo (1.2.3)
thread_safe (~> 0.1)
websocket-driver (0.6.5)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.2)

PLATFORMS
ruby

DEPENDENCIES
awesome_print
better_errors
binding_of_caller
byebug
chronic
foundation-rails
listen (~> 3.0.5)
minitest-rails
minitest-reporters
pg (~> 0.18)
pry-rails
puma (~> 3.0)
rails (~> 5.0.2)
spring
spring-watcher-listen (~> 2.0.0)
tzinfo-data

BUNDLED WITH
1.14.6
6 changes: 6 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.

require_relative 'config/application'

Rails.application.load_tasks
4 changes: 4 additions & 0 deletions app/channels/application_cable/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module ApplicationCable
class Channel < ActionCable::Channel::Base
end
end
4 changes: 4 additions & 0 deletions app/channels/application_cable/connection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module ApplicationCable
class Connection < ActionCable::Connection::Base
end
end
2 changes: 2 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class ApplicationController < ActionController::API
end
Empty file added app/controllers/concerns/.keep
Empty file.
13 changes: 13 additions & 0 deletions app/controllers/customers_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class CustomersController < ApplicationController

def index
if ["name", "registered_at", "postal_code"].include?(params[:sort])
customers = Customer.order(params[:sort])
else
customers = Customer.all
end

render json: customers.as_json(only: [:id, :name, :registered_at, :postal_code, :phone],
methods: :movies_checked_out_count), status: :ok
end
end
26 changes: 26 additions & 0 deletions app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class MoviesController < ApplicationController

# before_action :find_movie, :only => [:show]

def index
movies = Movie.all
render json: movies.as_json(only: [:title, :release_date]), status: :ok
end

def show
movie = Movie.find_by(title: params[:title])
if movie.nil?
render json: { nothing: true }, status: :not_found
else
render json: movie.as_json(except: [:id, :updated_at, :created_at],
methods: :available_inventory), status: :ok
end
end
end

# private
#
# def find_movie
# movie = Movie.find_by(title: params[:title])
# return movie
# end
75 changes: 75 additions & 0 deletions app/controllers/rentals_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
class RentalsController < ApplicationController

def create
movie = Movie.find_by_title(params[:title])

if movie.nil? || movie.available_inventory < 1
id = 0
else
id = movie.id
end

@rental = Rental.new(movie_id: id, customer_id: rental_params[:customer_id], due_date: rental_params[:due_date] )

if @rental.save
# movie.inventory -= 1
# movie.save
render status: :ok, json: @rental.as_json
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to filter what's returned here to eliminate the created_at and updated_at timestamps from the database record. They seem to be in a different timezone than the local user's timezone (i.e., for me they said it was already tomorrow, "2017-06-05T03:08:04.102Z", while the checkout date was today as expected, "2017-06-04T00:00:00.000Z"). It's not a big deal but could be somewhat confusing from the API user's standpoint.

else
render status: :error, json: { error: @rental.errors.full_messages }
end
end


def update
movie = find_movie(params[:title])
customer = find_customer(params[:customer_id])
if movie.nil?
# if movie title not found
render status: :bad_request, json: { error: "no movie found with that title"}
elsif customer.nil?
# if customer id not valid
render status: :bad_request, json: { error: "no customer found"}
else
# find the rental in question based on params[:title] and params[:customer_id]
rental = Rental.find_by(customer_id: customer.id, movie_id: movie.id)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this might not be a watertight way of identifying rentals. For instance, supposed a customer has three simultaneously checked-out copies of Jaws. I believe that find_by will only give you the one of these that happens to be first in the list. Since you don't currently limit retrieval by whether the rental is checked in, I think the same (already checked in) Jaws rental instance would be retrieved each time, making it impossible to check the other Jaws rental instances in. At least, this seems to be what I observe when I test via Postman. Not a big deal, but maybe something to look into if you plan to use this API in a future project.

# if found, set checkin_date to today's date
if rental
rental.checkin_date = Date.today
rental.save
# movie.inventory += 1
# movie.save
render status: :ok, json: { nothing: true }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might just be my own lack of familiarity with API development, but I found the {"nothing": true} JSON response a little confusing, especially since it could appear in the context of both error statuses and 200 OK statuses. It might be helpful for users of the API if you could customize the "nothing": true responses to provide a little more specific information, i.e., {"result": "Rental of Movie X by Customer Y was successfully checked in"}. Though this may be a convention/requirement of the assignment that I'm just not aware of :)

else
# if rental doesn't exist (customer id and movie id valid but movie never checked out)
render status: :bad_request, json: { error: "that movie is not checked out to that customer"}
end
end
end


def overdue
overdue_rentals = Rental.over_due_rentals
if overdue_rentals.length == 0
render json: { overdue: "none" }, status: :ok
else
render json: overdue_rentals.as_json(only: [:customer_id, :checkout_date, :due_date], methods: [:customer_name, :customer_postal_code, :movie_title]), status: :ok
end
end

private

def rental_params
params.permit(:customer_id, :due_date)
end


def find_movie(title)
Movie.find_by_title(title)
end

def find_customer(id)
Customer.find_by_id(id)
end

end
2 changes: 2 additions & 0 deletions app/jobs/application_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class ApplicationJob < ActiveJob::Base
end
Loading