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

Adahs branch #58

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

Adahs branch #58

wants to merge 13 commits into from

Conversation

Aghaile
Copy link

@Aghaile Aghaile commented Nov 18, 2021

No description provided.

Comment on lines +22 to +28
{
"id": customer.id,
"name": customer.name,
"postal_code": customer.postal_code,
"phone": customer.phone,
}
)
Copy link

Choose a reason for hiding this comment

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

this repeated code could be moved to your Customer Model as an instance method or moved to a helper function. It could look something like after the instance method is created

Suggested change
{
"id": customer.id,
"name": customer.name,
"postal_code": customer.postal_code,
"phone": customer.phone,
}
)
customer.to_customer_dict()
)



@customer_bp.route("", methods=["POST"])
def put_customer():
Copy link

Choose a reason for hiding this comment

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

i would rename this to something like post_customer() because this a post method

Comment on lines +97 to +108
# @customer_bp.route("/customers/<customer_id>", methods=["DELETE"])
# def delete_single_customer(customer_id):
# print("hiya")
# try:
# customer = Customer.query.get(customer_id)
# except:
# return jsonify(message=f"Customer {customer_id} was not found"), 404

# db.session.delete(customer)
# db.session.commit()

# return jsonify(id=customer.id), 200
Copy link

Choose a reason for hiding this comment

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

when pushing the final code remove any unused code.

Comment on lines +82 to +85
if "name" not in request_body:
return jsonify(None), 400
if "postal_code" not in request_body:
return jsonify(None), 400
Copy link

Choose a reason for hiding this comment

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

you could create a function called something like invalid_customer_info to handle the guard clauses here. You can use the same function in the Post method for customers


# return jsonify(id=customer.id), 200
@customer_bp.route("/<customer_id>", methods=["DELETE"])
def delete_single_customer(customer_id):
Copy link

Choose a reason for hiding this comment

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

💃🏽

if customer == None:
return jsonify(message=f"Customer {customer_id} was not found"), 404

rental_list = Rental.query.filter_by(customer_id=customer.id, checked_out=True)
Copy link

Choose a reason for hiding this comment

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

you already created a customer variable so you can use it here for line 137 like rental_list = customer.rentals and have a relationship in the customer model like rentals = db.relationship('Rental', back_populates='customer', lazy=True)

phone = db.Column(db.String)
registered_at = db.Column(db.DateTime, default=date.today())
# videos = db.relationship("Video", secondary="rental", backref="customers")
Copy link

Choose a reason for hiding this comment

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

Like my previous comment I would suggest putting rentals = db.relationship('Rental', back_populates='customer', lazy=True) here

# videos = db.relationship("Video", secondary="rental", backref="customers")

def customer_information(self):
Copy link

Choose a reason for hiding this comment

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

wait you already had an instance method!! There were other places you could've used it 👀

@@ -1,4 +1,16 @@
from app import db
from datetime import timedelta, date
from datetime import datetime, timedelta


class Rental(db.Model):
Copy link

Choose a reason for hiding this comment

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

💃🏽

from app import db


class Video(db.Model):
Copy link

Choose a reason for hiding this comment

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

💃🏽

Comment on lines +29 to +31
count_of_rentals = len(video.video_rentals)

video_avialable_inventory = video.total_inventory - count_of_rentals
Copy link

Choose a reason for hiding this comment

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

another suggestion would be to add video_checkout_count = db.Column(db.Integer, default=0 to you Customer model then add to the count and subtract from the inventory count. for videos

Suggested change
count_of_rentals = len(video.video_rentals)
video_avialable_inventory = video.total_inventory - count_of_rentals
customer.videos_checkout_count += 1
video.total_inventory -= 1

Comment on lines +39 to +40
today = datetime.today()
due_date = today + timedelta(days=7)
Copy link

Choose a reason for hiding this comment

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

make one line like due_date = date.today() + timedelta(days=7)

db.session.commit()

videos_customer_checked_out = Rental.query.filter_by(customer_id=customer.id, checked_out=True).count()
video_avialable_inventory = video.total_inventory - count_of_rentals
Copy link

@tgoslee tgoslee Nov 23, 2021

Choose a reason for hiding this comment

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

same suggestion from above you could add videos_checkout_count to your model then subtract from the count by doing customer.videos_checkout_count -= 1

def get_all_videos():
if request.method == "GET":
video = Video.query.all()
video_response = [video.video_information() for video in video]
Copy link

Choose a reason for hiding this comment

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

okay, list comprehension! Think about other places you could implement this

Comment on lines +23 to +30
if "title" not in request_body:
return jsonify(details="Request body must include title."), 400

if "release_date" not in request_body:
return jsonify(details="Request body must include release_date."), 400

if "total_inventory" not in request_body:
return jsonify(details="Request body must include total_inventory."), 400
Copy link

@tgoslee tgoslee Nov 23, 2021

Choose a reason for hiding this comment

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

you could create a function to handle these guard clauses dynamically and then use that function for customers and videos it could look something like

def validate_parameters(request_body, parameters):
           for attribute in parameters:
                if attribute not in request_body:
                       abort(make_response({"details": f"Request body must include {attribute}."}, 400))

return jsonify(new_video.video_information()), 201


@videos_bp.route("/<video_id>", methods=["GET"])
Copy link

Choose a reason for hiding this comment

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

💃🏽

return jsonify(video.video_information()), 200


@videos_bp.route("/<video_id>", methods=["PUT"])
Copy link

Choose a reason for hiding this comment

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

💃🏽

return jsonify(video.video_information()), 200


@videos_bp.route("/<video_id>", methods=["DELETE"])
Copy link

Choose a reason for hiding this comment

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

💃🏽

if video == None:
return jsonify(message=f"Video {video_id} was not found"), 404

rental_list = Rental.query.filter_by(video_id=video.id, checked_out=True)
Copy link

Choose a reason for hiding this comment

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

this could be videos.rentals if you add rentals = db.relationship('Rental', back_populates='video', lazy=True) to your video model

@tgoslee
Copy link

tgoslee commented Nov 23, 2021

Great job Adah and Juliana! I liked how your files were organized by routes and models! I saw that you were using helper functions in some places. I added some comments on refactoring and using some more helper functions. Let me know if you have questions.

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