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

Spruce - Cabebe & Zandra #39

Open
wants to merge 32 commits into
base: master
Choose a base branch
from
Open

Spruce - Cabebe & Zandra #39

wants to merge 32 commits into from

Conversation

zandra2
Copy link

@zandra2 zandra2 commented Nov 15, 2021

No description provided.

cabebe-bloop and others added 30 commits November 8, 2021 15:55
Copy link

@audreyandoy audreyandoy left a comment

Choose a reason for hiding this comment

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

Great work Zandra and Cabebe! What a great team! Y'all met all the learning goals for this project and passed all the tests.

I provided feedback on how y'all can refactor by omitting redundant logic, using helper methods, and list comprehension.

Y'all earned a 🟢 Green 🟢 !

customer_id = db.Column(db.Integer, primary_key=True, autoincrement=True)

Choose a reason for hiding this comment

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

Integer primary key columns will auto-increment by default so it's not required to provide that setting.

We can read more about the autoincrement attribute in the SQL_Alchemy documentation

Comment on lines +6 to +11
phone = db.Column(db.String)
postal_code = db.Column(db.String)
# server_default tells sqlA to pass the default value as part of the CREATE TABLE
# func.now() or func.current_timestamp() - they are aliases of each other. This tells DB to calcaate the timestamp itself
registered_at = db.Column(db.DateTime(timezone=True), server_default=func.now())

Choose a reason for hiding this comment

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

At the moment, any of these columns can contain null values. Consider revisiting each column to determine which column should truly have null values versus not. For example, is it useful to store a Customer without a name or phone number? Those two columns can be considered required information to store about a customer, despite our requirements not strictly describing that behavior.

Comment on lines +18 to +30
dict = {
"id": self.customer_id,
"name": self.name,
"phone": self.phone,
"postal_code": self.postal_code,
# weekday|day of month (16)|month name|year|local version of time|, UTC offset +HHMM or -HHMM
"registered_at": self.registered_at.strftime("%a, %-d %b %Y %X %z")
}
# if self.registered_at is not None:
# dict["registered_at"] = self.registered_at.strfttime("%a, %-d %b %Y %X %z")

return dict

Choose a reason for hiding this comment

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

Great helper function!

rental_id = db.Column(db.Integer, primary_key=True, autoincrement=True)

Choose a reason for hiding this comment

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

👍

#how many I own
total_inventory = db.Column(db.Integer)
#customers = db.relationship("Rental", back_populates="video")

Choose a reason for hiding this comment

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

Video should have a relationship with Rental so this line shouldn't be commented. We want to be able to identify videos that are 'rented' :

Suggested change
#customers = db.relationship("Rental", back_populates="video")
rentals = db.relationship("Rental", backref="video")

Comment on lines +55 to +57
return jsonify(customer.customer_dict()), 200

return jsonify(customer.customer_dict()), 200

Choose a reason for hiding this comment

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

The duplicate return statement should be removed.

Suggested change
return jsonify(customer.customer_dict()), 200
return jsonify(customer.customer_dict()), 200
return jsonify(customer.customer_dict()), 200

Comment on lines +67 to +78
if "name" not in request_body or "phone" not in request_body or "postal_code" not in request_body:
return jsonify(), 400

customer.name = request_body["name"]
customer.phone = request_body["phone"]
customer.postal_code = request_body["postal_code"]

if "name" in request_body or "phone" in request_body or "postal_code" in request_body:
response_body ={}
response_body["name"] = customer.name
response_body["phone"] = customer.phone
response_body["postal_code"] = customer.postal_code

Choose a reason for hiding this comment

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

The guard clause in lines 67-68 already checks for non-existent name, phone, or postal_code values by exiting the function early. This means we can refactor by removing the check for these same fields on line 74.

Suggested change
if "name" not in request_body or "phone" not in request_body or "postal_code" not in request_body:
return jsonify(), 400
customer.name = request_body["name"]
customer.phone = request_body["phone"]
customer.postal_code = request_body["postal_code"]
if "name" in request_body or "phone" in request_body or "postal_code" in request_body:
response_body ={}
response_body["name"] = customer.name
response_body["phone"] = customer.phone
response_body["postal_code"] = customer.postal_code
if "name" not in request_body or "phone" not in request_body or "postal_code" not in request_body:
return jsonify(), 400
customer.name = request_body["name"]
customer.phone = request_body["phone"]
customer.postal_code = request_body["postal_code"]
response_body ={}
response_body["name"] = customer.name
response_body["phone"] = customer.phone
response_body["postal_code"] = customer.postal_code

Choose a reason for hiding this comment

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

We can refactor this section even more by building the response dictionary in the return statement using the Customer customer_dict method:

Suggested change
if "name" not in request_body or "phone" not in request_body or "postal_code" not in request_body:
return jsonify(), 400
customer.name = request_body["name"]
customer.phone = request_body["phone"]
customer.postal_code = request_body["postal_code"]
if "name" in request_body or "phone" in request_body or "postal_code" in request_body:
response_body ={}
response_body["name"] = customer.name
response_body["phone"] = customer.phone
response_body["postal_code"] = customer.postal_code
if "name" not in request_body or "phone" not in request_body or "postal_code" not in request_body:
return jsonify(), 400
customer.name = request_body["name"]
customer.phone = request_body["phone"]
customer.postal_code = request_body["postal_code"]
db.session.commit()
return jsonify(customer.customer_dict()), 200

return jsonify(response_body), 200


@customers_bp.route("/<customer_id>", methods=["DELETE"])

Choose a reason for hiding this comment

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

👍



@customers_bp.route("/<customer_id>", methods=["DELETE"])
def customer_delete(customer_id):

Choose a reason for hiding this comment

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

👍

#I DON'T KNOW IF THIS IS CORRECT
#adding videos attribute to Customer Model
videos = db.relationship("Video", secondary="rental", backref="customers")

Choose a reason for hiding this comment

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

The secondary attribute can be removed from this relationship. The secondary attribute is used with pure join tables (whose sole purpose is just to map out the relationship between two tables). In this case, rental is actually not a pure join table and contains its own columns we want to keep track of ( due_date ).

So in this case, adding the relationship with backref to rental in both Video and Customer models would suffice.

rentals = db.relationship("rentals",  backref="customers")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants