forked from AdaGold/retro-video-store
-
Notifications
You must be signed in to change notification settings - Fork 15
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
Ac2-Ocelots-Catherine-Bandarchuk(Co-worker:Yvette Wu):retro-vide-store-api #9
Open
CatherineBandarchuk
wants to merge
50
commits into
ada-ac2:main
Choose a base branch
from
CatherineBandarchuk:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
50 commits
Select commit
Hold shift + click to select a range
e1fff00
create customer, rental, video models
CatherineBandarchuk 9f74ac6
complete_setup
CatherineBandarchuk 6db8edf
Add customer and video models
CatherineBandarchuk b837b14
add customer and videos models
CatherineBandarchuk a0e0591
update video model, add video routes
dp-wu 0c79d6b
added GET routes for customer
CatherineBandarchuk 204f435
Merge branch 'main' of github.com:CatherineBandarchuk/retro-video-store
CatherineBandarchuk d570019
fix typo in video_routes
dp-wu b7604d6
Merge branch 'main' of github.com:CatherineBandarchuk/retro-video-store
dp-wu 17ac26a
modify params for validate_input
dp-wu 6fb7766
add validation for delete and update request
dp-wu ded23be
fix create, update, delete return response, all wave01 video tests pa…
dp-wu 99ea8aa
minor format fix
dp-wu 1588264
modify create and delete requests
dp-wu 104b19a
added rental model
CatherineBandarchuk 92cf7a9
fixed update_customer_routes.py
CatherineBandarchuk ddc6fd9
refactor: create validate_routes.py
CatherineBandarchuk 83d237d
modified the models
CatherineBandarchuk 04e8e5e
Added pseudocode and comments
CatherineBandarchuk 5798ba7
added comments and pseudocode
CatherineBandarchuk 590f75d
added rental_routes
CatherineBandarchuk 1f19701
added get_customers_for_video
CatherineBandarchuk 8a8520c
fixed rental routes
CatherineBandarchuk 4967276
fix rentas
CatherineBandarchuk f60af70
Merge pull request #1 from CatherineBandarchuk/kate_retro_video_store
CatherineBandarchuk 0ffd70b
rebuild migration
CatherineBandarchuk dac19fd
fixed test1
CatherineBandarchuk dd33a95
added check_out_video_route
CatherineBandarchuk 901979e
complete first 6 tests of wave02
dp-wu 2ece984
fixed more issues
dp-wu d60686e
complete wave02
dp-wu 40e8b47
clean up some code
dp-wu 3fdf2b6
Added changes from updated version of tests wave3
CatherineBandarchuk 6f6fbe6
implement get_all_customers_with_query()
CatherineBandarchuk e11273b
implement get_video_rentals_for_customer_with_query
CatherineBandarchuk c7d4ca3
Added get_customers_who_rent_the_video_with_query
CatherineBandarchuk bdb950c
add get_customer_rental_history method
dp-wu 42f5d79
clean up code
dp-wu eefa8b2
complete get_all_overdue_customers method
dp-wu 486aa00
complete get_videos_rental_history method, untested
dp-wu 6024634
Remove routes.py, Clean code.
CatherineBandarchuk fc016ce
recreate db
dp-wu d5e38ef
install guniorn
dp-wu 3ef4619
changed the requirements.txt file
CatherineBandarchuk 0c09fe5
Merge branch 'main' of github.com:CatherineBandarchuk/retro-video-store
CatherineBandarchuk eef8d74
changed video_data.csv
CatherineBandarchuk 5c91ce0
fixed a logic error in get_customer_rental_history method
dp-wu 160fdef
Merge branch 'main' of github.com:CatherineBandarchuk/retro-video-store
dp-wu 31db575
Added get_all_rentals route
CatherineBandarchuk bd61cc0
update video rental history
dp-wu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,28 @@ | ||
from app import db | ||
|
||
class Customer(db.Model): | ||
id = db.Column(db.Integer, primary_key=True) | ||
id = db.Column(db.Integer, primary_key=True, autoincrement = True) | ||
name = db.Column(db.String, nullable = False) | ||
registered_at = db.Column(db.DateTime(timezone = True), nullable = False) | ||
registered_at = db.Column(db.DateTime(timezone = True)) | ||
postal_code = db.Column(db.String, nullable = False) | ||
phone = db.Column(db.String, nullable = False) | ||
videos_checked_out_count = db.Column(db.Integer, default = 0, nullable = False) | ||
videos_checked_out_count = db.Column(db.Integer) | ||
|
||
# server_default=func.now()) | ||
def to_dict(self): | ||
customer_as_dict = {} | ||
customer_as_dict["id"] = int(self.id) | ||
customer_as_dict["name"] = self.name | ||
customer_as_dict["postal_code"] = self.postal_code | ||
customer_as_dict["phone"] = self.phone | ||
customer_as_dict["registered_at"] = self.registered_at | ||
customer_as_dict["videos_checked_out_count"] = self.videos_checked_out_count | ||
|
||
return customer_as_dict | ||
|
||
@classmethod | ||
def from_dict(cls, customer_data): | ||
new_customer = Customer(name=customer_data["name"], | ||
postal_code=customer_data["postal_code"], | ||
phone=customer_data["phone"] | ||
) | ||
return new_customer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
from app import db | ||
from app.models.customer import Customer | ||
from flask import Blueprint, jsonify, abort, make_response, request | ||
from datetime import datetime | ||
|
||
customer_bp = Blueprint("customer_bp", __name__, url_prefix = "/customers") | ||
|
||
## Helper functions ## | ||
|
||
# Validating the id of the customer: id needs to be int and exists the planet with the id. | ||
# Returning the valid class instance if valid id | ||
def validate_model(cls, model_id): | ||
try: | ||
model_id = int(model_id) | ||
except: | ||
abort(make_response({"message":f"{cls.__name__} {model_id} invalid"}, 400)) | ||
#abort(make_response(jsonify(f"{cls.__name__} {model_id} invalid"), 400)) | ||
class_obj = cls.query.get(model_id) | ||
if not class_obj: | ||
abort(make_response({"message":f"{cls.__name__} {model_id} was not found"}, 404)) | ||
#abort(make_response(jsonify(f"{cls.__name__} {model_id} was not found"), 404)) | ||
return class_obj | ||
|
||
# Validating the user input to create or update the customer | ||
# Returning the valid JSON if valid input | ||
def validate_input(customer_value): | ||
invalid_dict = {} | ||
if "name" not in customer_value \ | ||
or not isinstance(customer_value["name"], str) \ | ||
or customer_value["name"] == "": | ||
invalid_dict["details"] = "Request body must include name." | ||
if "postal_code" not in customer_value \ | ||
or not isinstance(customer_value["postal_code"], str) \ | ||
or customer_value["postal_code"] == "": | ||
invalid_dict["details"] = "Request body must include postal_code." | ||
if "phone" not in customer_value \ | ||
or not isinstance(customer_value["phone"], str) \ | ||
or customer_value["phone"] == "": | ||
invalid_dict["details"] = "Request body must include phone." | ||
# return abort(make_response(jsonify("Invalid request"), 400)) | ||
return invalid_dict | ||
|
||
## Routes functions ## | ||
|
||
# Get all customers info (GET /customers) | ||
# Return JSON list | ||
@customer_bp.route("", methods = ["GET"]) | ||
def get_all_customers(): | ||
customers = Customer.query.all() | ||
customers_list = [] | ||
for customer in customers: | ||
customers_list.append(customer.to_dict()) | ||
|
||
return jsonify(customers_list), 200 | ||
|
||
# Get the customer info by id (GET /customers/<id>) | ||
# Return info in JSON format | ||
@customer_bp.route("/<customer_id>",methods=["GET"] ) | ||
def get_one_customer(customer_id): | ||
customer = validate_model(Customer, customer_id) | ||
return customer.to_dict() | ||
|
||
# Register a customer info (POST /customers) | ||
# Return sussess message "Customer {name} successfully registered" | ||
@customer_bp.route("", methods = ["POST"]) | ||
def register_customer(): | ||
customer_info = request.get_json() | ||
check_invalid_dict = validate_input(customer_info) | ||
|
||
if check_invalid_dict: | ||
return abort(make_response(jsonify("Invalid request"), 400)) | ||
|
||
new_customer = Customer.from_dict(customer_info) | ||
|
||
if not new_customer.videos_checked_out_count: | ||
new_customer.videos_checked_out_count = 0 | ||
|
||
new_customer.registered_at = datetime.now() | ||
|
||
db.session.add(new_customer) | ||
db.session.commit() | ||
db.session.refresh(new_customer) | ||
|
||
return make_response(f"Customer {new_customer.name} successfully registered", 201) | ||
#return make_response(jsonify(f"Customer {new_customer.name} successfully registered"), 201) | ||
|
||
# Update the customer info by id (PUT /customer/<id>) | ||
# Return sussess message "Customer {id} info successfully udated" | ||
@customer_bp.route("/<customer_id>",methods=["PUT"] ) | ||
def update_customer(customer_id): | ||
customer = validate_model(Customer, customer_id) | ||
request_body = validate_input(request.get_json()) | ||
|
||
customer.name = request_body["name"] | ||
customer.postal_code = request_body["postal_code"] | ||
customer.phone = request_body["phone"] | ||
customer.registered_at = request_body["registered_at"] | ||
customer.videos_checked_out_count = request_body["videos_checked_out_count"] | ||
|
||
db.session.commit() | ||
return make_response(jsonify(f"Customer {customer_id} successfully updated"), 200) | ||
|
||
# Delete the customer info by id (DELETE /customer/<id>) | ||
# Return sussess message "Customer {id} info successfully udated" | ||
@customer_bp.route("/<customer_id>",methods=["DELETE"]) | ||
def delete_customer(customer_id): | ||
customer = validate_model(Customer, customer_id) | ||
db.session.delete(customer) | ||
db.session.commit() | ||
|
||
return make_response(jsonify(f"Customer {customer.id} info successfully deleted"), 200) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"name": "Tom", | ||
"postal_code": "98007", | ||
"phone": "(123)456-7890", | ||
"videos_checked_out_count": 0 | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 would be a great candidate for a list comprehension - what could that look like? Are there other places you see across the project where a list comprehension would make sense?
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.
customer_list = [customer.to_dict() for customer in customers]