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

Ac2-Ocelots-Catherine-Bandarchuk(Co-worker:Yvette Wu):retro-vide-store-api #9

Open
wants to merge 50 commits into
base: main
Choose a base branch
from
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 Jan 4, 2023
9f74ac6
complete_setup
CatherineBandarchuk Jan 4, 2023
6db8edf
Add customer and video models
CatherineBandarchuk Jan 5, 2023
b837b14
add customer and videos models
CatherineBandarchuk Jan 5, 2023
a0e0591
update video model, add video routes
dp-wu Jan 5, 2023
0c79d6b
added GET routes for customer
CatherineBandarchuk Jan 5, 2023
204f435
Merge branch 'main' of github.com:CatherineBandarchuk/retro-video-store
CatherineBandarchuk Jan 5, 2023
d570019
fix typo in video_routes
dp-wu Jan 5, 2023
b7604d6
Merge branch 'main' of github.com:CatherineBandarchuk/retro-video-store
dp-wu Jan 5, 2023
17ac26a
modify params for validate_input
dp-wu Jan 5, 2023
6fb7766
add validation for delete and update request
dp-wu Jan 5, 2023
ded23be
fix create, update, delete return response, all wave01 video tests pa…
dp-wu Jan 5, 2023
99ea8aa
minor format fix
dp-wu Jan 5, 2023
1588264
modify create and delete requests
dp-wu Jan 5, 2023
104b19a
added rental model
CatherineBandarchuk Jan 5, 2023
92cf7a9
fixed update_customer_routes.py
CatherineBandarchuk Jan 5, 2023
ddc6fd9
refactor: create validate_routes.py
CatherineBandarchuk Jan 6, 2023
83d237d
modified the models
CatherineBandarchuk Jan 6, 2023
04e8e5e
Added pseudocode and comments
CatherineBandarchuk Jan 6, 2023
5798ba7
added comments and pseudocode
CatherineBandarchuk Jan 6, 2023
590f75d
added rental_routes
CatherineBandarchuk Jan 6, 2023
1f19701
added get_customers_for_video
CatherineBandarchuk Jan 6, 2023
8a8520c
fixed rental routes
CatherineBandarchuk Jan 7, 2023
4967276
fix rentas
CatherineBandarchuk Jan 7, 2023
f60af70
Merge pull request #1 from CatherineBandarchuk/kate_retro_video_store
CatherineBandarchuk Jan 7, 2023
0ffd70b
rebuild migration
CatherineBandarchuk Jan 7, 2023
dac19fd
fixed test1
CatherineBandarchuk Jan 7, 2023
dd33a95
added check_out_video_route
CatherineBandarchuk Jan 7, 2023
901979e
complete first 6 tests of wave02
dp-wu Jan 7, 2023
2ece984
fixed more issues
dp-wu Jan 7, 2023
d60686e
complete wave02
dp-wu Jan 7, 2023
40e8b47
clean up some code
dp-wu Jan 7, 2023
3fdf2b6
Added changes from updated version of tests wave3
CatherineBandarchuk Jan 7, 2023
6f6fbe6
implement get_all_customers_with_query()
CatherineBandarchuk Jan 8, 2023
e11273b
implement get_video_rentals_for_customer_with_query
CatherineBandarchuk Jan 8, 2023
c7d4ca3
Added get_customers_who_rent_the_video_with_query
CatherineBandarchuk Jan 8, 2023
bdb950c
add get_customer_rental_history method
dp-wu Jan 8, 2023
42f5d79
clean up code
dp-wu Jan 8, 2023
eefa8b2
complete get_all_overdue_customers method
dp-wu Jan 8, 2023
486aa00
complete get_videos_rental_history method, untested
dp-wu Jan 8, 2023
6024634
Remove routes.py, Clean code.
CatherineBandarchuk Jan 8, 2023
fc016ce
recreate db
dp-wu Jan 8, 2023
d5e38ef
install guniorn
dp-wu Jan 8, 2023
3ef4619
changed the requirements.txt file
CatherineBandarchuk Jan 8, 2023
0c09fe5
Merge branch 'main' of github.com:CatherineBandarchuk/retro-video-store
CatherineBandarchuk Jan 8, 2023
eef8d74
changed video_data.csv
CatherineBandarchuk Jan 9, 2023
5c91ce0
fixed a logic error in get_customer_rental_history method
dp-wu Jan 9, 2023
160fdef
Merge branch 'main' of github.com:CatherineBandarchuk/retro-video-store
dp-wu Jan 9, 2023
31db575
Added get_all_rentals route
CatherineBandarchuk Jan 9, 2023
bd61cc0
update video rental history
dp-wu Jan 10, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions app/__init__.py
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@ def create_app(test_config=None):

# import models for Alembic Setup
from app.models.customer import Customer
from app.models.video import Video
#from app.models.video import Video
from app.models.rental import Rental

# Setup DB
@@ -35,7 +35,6 @@ def create_app(test_config=None):
#from .routes.customer_routes import customer_bp
#app.register_blueprint(customer_bp)

from .routes.video_routes import video_bp
app.register_blueprint(video_bp)

# from .routes.video_routes import video_bp
# app.register_blueprint(video_bp)
return app
25 changes: 21 additions & 4 deletions app/models/customer.py
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
111 changes: 111 additions & 0 deletions app/routes/customer_routes.py
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())
Comment on lines +48 to +50

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?

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]


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)
6 changes: 6 additions & 0 deletions customers_data.csv
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
}