Leaves AND Branches - Elizabeth AND Macaria#29
Leaves AND Branches - Elizabeth AND Macaria#29north108 wants to merge 18 commits intoAda-C12:masterfrom
Conversation
…working properly at this point.
added decrementing and incrementing of available_inventory. Smoke tes…
Video StoreWhat We're Looking For
|
| if (!movie.available_inventory) | ||
| movie.available_inventory = movie.inventory | ||
| end | ||
|
|
||
| movie.available_inventory -= 1 | ||
| movie.available_inventory_will_change! | ||
| movie.save |
There was a problem hiding this comment.
This kind of business logic looks like a prime candidate for a model method!
| if rental = Rental.find_by(customer_id: customer.id, movie_id: movie.id) | ||
| Customer.check_in_movie(customer) | ||
|
|
||
| if (!movie.available_inventory) | ||
| movie.available_inventory = movie.inventory | ||
| end | ||
|
|
||
| movie.available_inventory += 1 | ||
| movie.available_inventory_will_change! | ||
| movie.save | ||
|
|
||
| rental.status = "checked_in" | ||
| rental.save |
There was a problem hiding this comment.
This is more business logic to put in the model.
| def self.check_out_movie(id) | ||
| customer = Customer.find_by(id: id) | ||
| movie_count = customer.movies_checked_out_count | ||
| customer.update(movies_checked_out_count: movie_count + 1) | ||
|
|
||
| customer.save | ||
| return customer | ||
| end |
There was a problem hiding this comment.
So the checked_out_count has nothing to do with the number of Rentals the customer has. Hmmm... Any kind of subtle bug could get these out of sync. This isn't an out-and-out bug, but in an imperfect system it's an invitation for these kinds of problems.
| def self.due_date(rental) | ||
| due_date = Date.today + 7.days | ||
| rental.due_date = due_date | ||
| rental.save | ||
| return rental | ||
| end | ||
|
|
||
| def self.status_checkout(rental) | ||
| rental.status = "checked out" | ||
| rental.save | ||
| return rental | ||
| end |
There was a problem hiding this comment.
These make much more sense as instance methods as opposed to class methods.
| class Movie < ApplicationRecord | ||
| has_many :rentals, dependent: :nullify | ||
| validates :title, presence: true | ||
| validates :inventory, presence: true, numericality: {greater_than: 0} |
There was a problem hiding this comment.
Running the seeds file, I'm getting validation errors, so your seeds file doesn't work with this validation.
| end | ||
|
|
||
| describe "check_out_movie" do | ||
| it "increases the customers movie count if given valid data" do |
There was a problem hiding this comment.
You should have a test with invalid data. Also this method doesn't make much sense in that someone could have a larger checked out count without more rentals.
| expect(result).must_equal true | ||
| end | ||
|
|
||
| it "is invalid with a movie_id that is not a number" do |
There was a problem hiding this comment.
You should also have a test so that it's invalid to check out a movie, if there are none left in inventory.
| MOVIE_FIELDS = ["available_inventory", "inventory", "overview", "release_date", "title"] | ||
|
|
||
| describe MoviesController do | ||
| describe "index" do |
| must_respond_with :bad_request | ||
| body = JSON.parse(response.body) | ||
| expect(body['errors']).must_include "BAD Request" |
There was a problem hiding this comment.
You should also have some validation error messages.
| end | ||
| end | ||
|
|
||
| describe "check_in" do |
There was a problem hiding this comment.
What about invalid check ins and checkouts? Trying to check out a movie with no more inventory, or checking in a movie that's not checked out. More testing needed.
Video Store API
Congratulations! You're submitting your assignment!
If you didn't get to the functionality the question is asking about, reply with what you would have done if you had completed it.
Comprehension Questions
POST /rentals/check-inendpoint? What does the time complexity depend on? Explain your reasoning.