-
Notifications
You must be signed in to change notification settings - Fork 25
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
Cara/Brandy/Jamila Octos C9 #15
base: master
Are you sure you want to change the base?
Conversation
Video StoreWhat We're Looking For
|
def show | ||
@customer = Customer.find_by(id: cust_params[:id]) | ||
if @customer.nil? | ||
render json: { |
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.
While this works, you don't typically use strong params for URL parameters.
def create | ||
@movie = Movie.new(movie_params) | ||
@movie.save! | ||
render :create, status: :ok |
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.
Using bang (!
) methods works great for the console or scripts like the seed file, but in your actual controller actions you should be calling the non-bang version and checking the result. As-is, this will produce an exception rather than a helpful error message, which is not a good user experience.
Something like this would be more appropriate:
@movie = Movie.new(movie_params)
if @movie.save
render :create, status: :ok
else
render json: { errors: @movie.errors.messages }, status: :bad_request
end
if @movie.nil? | ||
render json: { | ||
errors: { | ||
id: ["No movie with ID #{movie_params[:id]}"] |
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.
This is a good example of well-done error handling.
customer_id = check_params[:customer_id] | ||
movie_id = check_params[:movie_id] | ||
|
||
find_customer_and_movie(customer_id, movie_id) |
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.
I like the idea of moving the shared logic to find the customer and the movie into a helper method. Why not take it a step further and make it a controller filter?
|
||
if @rental.save | ||
|
||
Rental.process_checkout(@customer, @movie) |
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.
This dance of checking the dependencies, creating the rentals, and then updating the dependent models is pretty complicated - it might make sense to encapsulate the whole thing in a model method. One thing that might help is using a transaction - go look this up!
def self.check_dependencies(customer, movie) | ||
if customer.nil? | ||
return { | ||
customer_id: ["No such customer"] |
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.
Since the customer and movie are about to be associated with an instance of Rental
, it might make sense to make this an instance method.
|
||
it "Creates a new movie" do | ||
# this is taking the count before and after | ||
before_count = Movie.count |
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.
What if you send bad data?
describe Rental do | ||
|
||
describe "relations" do | ||
it "must respond to customer" do |
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.
You've done such a good job of encapsulating your logic in the model! One of the big advantages of that is that you can test the logic in isolation here, but you're not doing that.
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