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

Paper - Lauren Covington #56

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

LaurenCovington
Copy link

No description provided.

Copy link
Contributor

@kaidamasaki kaidamasaki left a comment

Choose a reason for hiding this comment

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

Great job!

I pointed out some ways to DRY up your code but overall everything looks good!

phone_number = db.Column(db.String(12))
register_at = db.Column(db.DateTime)
videos_checked_out_count = db.Column(db.Integer, default=0)
rental = db.relationship('Rental', backref='rental', lazy=True)
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be rentals since it's a collection of them:

Suggested change
rental = db.relationship('Rental', backref='rental', lazy=True)
rentals = db.relationship('Rental', backref='rental', lazy=True)

"phone": self.phone_number,
"postal_code": self.postal_code,
"registered_at": check_registration,
"videos_checked_out_count": self.videos_checked_out_count
Copy link
Contributor

@kaidamasaki kaidamasaki May 28, 2021

Choose a reason for hiding this comment

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

You can use the relationship so that videos_checked_out_count is just the length of the rentals that haven't been checked in:

Suggested change
"videos_checked_out_count": self.videos_checked_out_count
"videos_checked_out_count": len([rental for rental in self.rentals if not rental.check_in_date])

check_out_date = db.Column('check_out_date', db.DateTime, default=datetime.now())
renter = db.relationship('Customer', backref='rentals', lazy=True, foreign_keys=customer_id)
video = db.relationship('Video', backref='rentals', lazy=True, foreign_keys=video_id)

Copy link
Contributor

Choose a reason for hiding this comment

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

It can be helpful to add a check in date:

Suggested change
check_in_date = db.Column('check_out_date', db.DateTime, default=datetime.now())

total_inventory = db.Column(db.Integer)
available_inventory = db.Column(db.Integer)
renter = db.relationship('Rental', backref='customer', lazy=True)

Copy link
Contributor

Choose a reason for hiding this comment

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

Like with Customer you can compute available_inventory with a relationship:

Suggested change
rentals = db.relationship('Rental', back_populates='video', lazy=True)

"title": self.title,
"release_date": self.release_date,
"total_inventory": self.total_inventory,
"available_inventory": self.available_inventory
Copy link
Contributor

@kaidamasaki kaidamasaki May 28, 2021

Choose a reason for hiding this comment

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

Like with Customer the available_inventory is just the total inventory minus length of the rentals that haven't been checked in:

Suggested change
"available_inventory": self.available_inventory
"available_inventory": self.total_inventory - len([rental for rental in self.rentals if not rental.check_in_date])

Comment on lines +37 to +39
single_customer = Customer.query.get(customer_id)
if not single_customer:
return make_response({"details": f"There is no customer in the database with ID #{customer_id}"}, 404)
Copy link
Contributor

Choose a reason for hiding this comment

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

You can simplify this with get_or_404.

Suggested change
single_customer = Customer.query.get(customer_id)
if not single_customer:
return make_response({"details": f"There is no customer in the database with ID #{customer_id}"}, 404)
single_customer = Customer.query.get_or_404(customer_id)

Comment on lines +50 to +53
new_customer = Customer(name=request_body["name"],
postal_code=request_body["postal_code"],
phone_number=request_body["phone"],
register_at=datetime.now())
Copy link
Contributor

Choose a reason for hiding this comment

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

Style/formatting:

Suggested change
new_customer = Customer(name=request_body["name"],
postal_code=request_body["postal_code"],
phone_number=request_body["phone"],
register_at=datetime.now())
new_customer = Customer(
name=request_body["name"],
postal_code=request_body["postal_code"],
phone_number=request_body["phone"],
register_at=datetime.now()
)

Comment on lines +96 to +97
if not videos:
return jsonify(list_of_videos)
Copy link
Contributor

Choose a reason for hiding this comment

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

You don't actually need to do this check since the loop will leave the list empty if there is nothing in it.

request_body = request.get_json()

# convert str to datetime object
request_body["release_date"] = datetime.fromisoformat(request_body["release_date"])
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice find with fromisoformat 😄

Comment on lines +252 to +259
customer_entity.append(entity)
dictified_ce["id"] = entity.customer_id
dictified_ce["name"] = entity.name
dictified_ce["phone"] = entity.phone_number
dictified_ce["postal_code"] = entity.postal_code
dictified_ce["registered_at"] = entity.register_at
dictified_ce["videos_checked_out_count"] = entity.videos_checked_out_count
hold_dictified_entities.append(dictified_ce)
Copy link
Contributor

Choose a reason for hiding this comment

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

You can use to_json instead of producing this dictionary manually (the fields match up):

Suggested change
customer_entity.append(entity)
dictified_ce["id"] = entity.customer_id
dictified_ce["name"] = entity.name
dictified_ce["phone"] = entity.phone_number
dictified_ce["postal_code"] = entity.postal_code
dictified_ce["registered_at"] = entity.register_at
dictified_ce["videos_checked_out_count"] = entity.videos_checked_out_count
hold_dictified_entities.append(dictified_ce)
customer_entity.append(entity)
hold_dictified_entities.append(entity.to_json())

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.

2 participants