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

bo and sai's video store API #16

Open
wants to merge 74 commits into
base: master
Choose a base branch
from
Open
Changes from 3 commits
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
5fe5fd3
made new empty api app
ssamant May 9, 2017
d01445a
Merge pull request #1 from ssamant/generate_new_api_app
ssamant May 9, 2017
b5bdffa
baseline with test route
ssamant May 9, 2017
8d5c69e
Merge pull request #2 from ssamant/baseline
ssamant May 9, 2017
056e985
added gems needed for testing and error views
botrethewey May 9, 2017
80e3529
Merge pull request #3 from ssamant/add-gems
botrethewey May 9, 2017
33b86fa
generated models for movies and customers
botrethewey May 9, 2017
a602dea
Merge pull request #4 from ssamant/add-models
botrethewey May 9, 2017
2cebec7
updated gemfile.lock
botrethewey May 9, 2017
2117324
added coverage to gitignore
ssamant May 9, 2017
196cf06
Merge branch 'master' of https://github.com/ssamant/VideoStoreAPI
ssamant May 9, 2017
3886f7d
Merge branch 'master' of https://github.com/ssamant/VideoStoreAPI
botrethewey May 9, 2017
5a031f2
schema created
botrethewey May 9, 2017
23a3ada
schema file
ssamant May 9, 2017
bee7a24
Merge branch 'master' of https://github.com/ssamant/VideoStoreAPI
ssamant May 9, 2017
732afca
added default value for account credit in Customer model
ssamant May 9, 2017
cb26563
validations for customer and movie models
ssamant May 9, 2017
8abd0ab
Merge pull request #5 from ssamant/modify_models
ssamant May 9, 2017
fb97a18
trying to fix regex in customer model
ssamant May 9, 2017
7ca8c02
adjusting regex
ssamant May 9, 2017
91ccd40
tests for customer model
ssamant May 9, 2017
a4a0e95
tests for Movie model
ssamant May 9, 2017
c40ab78
Merge pull request #6 from ssamant/model_tests
ssamant May 9, 2017
563e704
initially generted controllers for customers and movies
botrethewey May 9, 2017
638714c
update routes
botrethewey May 9, 2017
7be22ef
added controller tests for customers
botrethewey May 9, 2017
de6fe7a
update customers controller
botrethewey May 9, 2017
8ef5f20
Merge pull request #7 from ssamant/add-controllers
botrethewey May 9, 2017
121b2c1
movie controller tests and controller index and show actions
ssamant May 10, 2017
df86273
test for custom method available_inventory and method
ssamant May 10, 2017
ff507d5
Merge pull request #8 from ssamant/movie_controller_testing
ssamant May 10, 2017
7847e96
update error handling for movie show action
botrethewey May 10, 2017
221d35e
Merge pull request #9 from ssamant/update-error-handling
botrethewey May 10, 2017
2d37a9e
wip: added custom method to customer model
botrethewey May 10, 2017
0785c12
Merge pull request #10 from ssamant/add-custom-method-to-customer
botrethewey May 10, 2017
4cebdb2
finishing test and controller action for movie count
ssamant May 10, 2017
fa4b7cd
Merge pull request #11 from ssamant/customer_movie_count
ssamant May 10, 2017
7e4c786
added routes for rental optionals
ssamant May 10, 2017
2f7cc15
created rental model
ssamant May 10, 2017
da6c2b1
controller for Rentals
ssamant May 10, 2017
4e4757f
started model tests for rental
ssamant May 10, 2017
3537ec2
testing rental model validations for due date
ssamant May 10, 2017
a251452
Merge pull request #12 from ssamant/check-out-rental
ssamant May 10, 2017
4e447f9
db:reset after pull
botrethewey May 11, 2017
39cf376
removed extra unused defult routes
botrethewey May 11, 2017
4530b4d
added entity relationship test
botrethewey May 11, 2017
4dbe4e9
added simple cov
botrethewey May 11, 2017
f431b51
clean up model file
botrethewey May 11, 2017
48957ad
update rental model test
botrethewey May 11, 2017
740928d
Merge pull request #13 from ssamant/bt-update-rentals-test
botrethewey May 11, 2017
44848e1
added some tests
botrethewey May 11, 2017
d31ed5b
Merge pull request #14 from ssamant/add-rental-controller
botrethewey May 11, 2017
0cf9d41
wrote controller action for rental create and updated tests
ssamant May 11, 2017
e34603e
Merge pull request #15 from ssamant/rental-create-controller
ssamant May 11, 2017
d7394fa
updated tests and controller logic
botrethewey May 12, 2017
2097e8b
Merge pull request #16 from ssamant/update-rental-logic
botrethewey May 12, 2017
b2b9bd5
logic and tests for Movie model method available inventory
ssamant May 12, 2017
8245341
tests for custom method check_availability in rental model
ssamant May 12, 2017
ff405d5
finised testing for rental model custom method
ssamant May 12, 2017
b72001a
Merge pull request #17 from ssamant/rental_logic_inventory
ssamant May 12, 2017
e442f2e
fix typo in movie controllers test
botrethewey May 12, 2017
70ee7ef
added tests for the rentals custom method in model
botrethewey May 12, 2017
2b3de18
Merge pull request #18 from ssamant/update-rental-controller-test
botrethewey May 12, 2017
510a49d
fixed error message in rental model test
botrethewey May 12, 2017
029a914
updated logic on movies_checked_out_count method
botrethewey May 12, 2017
0f368f0
updated tests for the custom method
botrethewey May 12, 2017
968032d
Merge pull request #19 from ssamant/update-customer-custom-method
botrethewey May 12, 2017
bf6306b
TDD! wrote controller tests for Rental update action
ssamant May 12, 2017
d19069c
tests for update action in Rental
ssamant May 12, 2017
8ad3f29
WIP overdue action in rental controller
ssamant May 12, 2017
bb987ed
Merge pull request #20 from ssamant/overdue_action
ssamant May 12, 2017
79cd476
hash to give to json in overdue action with all the required fields
ssamant May 13, 2017
656104e
WIP changed error message in check_inventory method to give estimated…
ssamant May 13, 2017
df6b2c1
Merge pull request #21 from ssamant/sandbox_overdue
ssamant May 13, 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
31 changes: 26 additions & 5 deletions app/controllers/rentals_controller.rb
Original file line number Diff line number Diff line change
@@ -26,15 +26,36 @@ def update
end

def overdue

#what is the syntax for a conditional for due date?

Choose a reason for hiding this comment

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

Remember to clean up comments! 🔍

rentals = Rental.where("returned = ? AND due_date <= ?", false, Date.current-1)

rentals = Rental.where("returned = ? AND due_date <= ?", false, Date.current)
if rentals
#array of customer ids for customers who have overdue rentals
customer_ids = rentals.map { |rental| rental.customer_id }

overdue_rentals = []
customer_ids.each do |id|
customer = Customer.find_by_id(id)
hash = Hash.new

Choose a reason for hiding this comment

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

For consistency you could do {} (since you do [] for a new array)...but this is me completely nitpicking bc you did such a great job 😁

hash[:name] = customer.name
hash[:customer_id] = id
hash[:postal_code] = customer.postal_code

rentals.group_by { |rental| rental.customer_id }
rentals = []
customer.rentals.each do |rental|
rental_hash = Hash.new
rental_hash[:title] = Movie.find_by_id(rental.movie_id).title
rental_hash[:checkout_date] = rental.created_at.to_date
rental_hash[:due_date] = rental.due_date
rentals << rental_hash
end
hash[:rentals] = rentals
overdue_rentals << hash
end
render status: :ok, json: overdue_rentals.as_json
else
render status: :bad_request, json: { overdue: "There are no overdue rentals" }
end

render json: rentals.as_json
end

private
4 changes: 3 additions & 1 deletion app/models/rental.rb
Original file line number Diff line number Diff line change
@@ -20,7 +20,9 @@ def initialize rental_params
def check_inventory
if movie
if movie.available_inventory <= 0

Choose a reason for hiding this comment

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

Would the avail inventory ever go below 0?

errors.add(:availability, "Sorry, this movie is not in stock")
rentals = Rental.where(movie_id: movie.id)
earliest = rentals.min_by { |rental| rental.due_date}
errors.add(:availability, "Sorry, this movie is not in stock. One is due back on #{earliest.due_date}")

Choose a reason for hiding this comment

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

Noting the earliest due date is a nice touch 👍

end
end
end