-
Notifications
You must be signed in to change notification settings - Fork 19
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
base: master
Are you sure you want to change the base?
Changes from 3 commits
5fe5fd3
d01445a
b5bdffa
8d5c69e
056e985
80e3529
33b86fa
a602dea
2cebec7
2117324
196cf06
3886f7d
5a031f2
23a3ada
bee7a24
732afca
cb26563
8abd0ab
fb97a18
7ca8c02
91ccd40
a4a0e95
c40ab78
563e704
638714c
7be22ef
de6fe7a
8ef5f20
121b2c1
df86273
ff507d5
7847e96
221d35e
2d37a9e
0785c12
4cebdb2
fa4b7cd
7e4c786
2f7cc15
da6c2b1
4e4757f
3537ec2
a251452
4e447f9
39cf376
4530b4d
4dbe4e9
f431b51
48957ad
740928d
44848e1
d31ed5b
0cf9d41
e34603e
d7394fa
2097e8b
b2b9bd5
8245341
ff405d5
b72001a
e442f2e
70ee7ef
2b3de18
510a49d
029a914
0f368f0
968032d
bf6306b
d19069c
8ad3f29
bb987ed
79cd476
656104e
df6b2c1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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? | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Noting the earliest due date is a nice touch 👍 |
||
end | ||
end | ||
end | ||
|
There was a problem hiding this comment.
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! 🔍