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

Ocelots - Larissa and Soumya #19

Open
wants to merge 68 commits into
base: main
Choose a base branch
from
Open
Changes from 11 commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
6665a56
Wave 0:Setup added migrations folder
smysh Jan 3, 2023
75fb009
Registered /customers blueprint
ctlaultdel Jan 4, 2023
9f10b69
Added customer class attributes and datatypes
ctlaultdel Jan 4, 2023
4f2d3d4
Updated migrations with new customer class columns and datatypes
ctlaultdel Jan 4, 2023
ee5ccc7
Added routes for customers blueprint
ctlaultdel Jan 4, 2023
7a0dfb3
Updated customer attributes and added class methods
ctlaultdel Jan 4, 2023
c56bda9
Added routes for displaying all customers, one customer by id, and po…
ctlaultdel Jan 4, 2023
2c78b55
Upgraded migrations to reflect changes in customer attributes
ctlaultdel Jan 4, 2023
d9b1bca
Added update_attr() method which allows a value of a customer instanc…
ctlaultdel Jan 5, 2023
15505c1
Added delete_a_customer route and modify_a_customer route
ctlaultdel Jan 5, 2023
7f6d6be
added compare_type=True to Migrations
ctlaultdel Jan 5, 2023
86c9e70
Got default datetime working for registered_at customer data
ctlaultdel Jan 5, 2023
7b92a0f
Refactored routes to make use of customer methods
ctlaultdel Jan 5, 2023
037e36f
Added return True boolean to customer.update_attr() if update successful
ctlaultdel Jan 5, 2023
c278adb
added video model and to_dict() and from_dict() methods.
smysh Jan 5, 2023
35d93a4
added video blueprint and registered it with the app.
smysh Jan 5, 2023
9a17221
Merge pull request #1 from ctlaultdel/LA-costumer-model
ctlaultdel Jan 5, 2023
7da4e5b
added routes to get and create videos.
smysh Jan 5, 2023
f8a9bed
Merge remote-tracking branch 'origin' into ss-videos-model
smysh Jan 5, 2023
5041c29
moved routes to separate routes folder. Refactored validate_customer_…
smysh Jan 5, 2023
020dece
fix an issue that was making some tests fail.
smysh Jan 5, 2023
5e365e6
Some more refactoring to create separate validate_request_attributes …
smysh Jan 6, 2023
95c0283
Merge pull request #2 from ctlaultdel/ss-videos-model
ctlaultdel Jan 6, 2023
9af0ef6
Wave 1 tests passing. Further refactored to combine validate_post_req…
smysh Jan 6, 2023
8b60f76
Merge pull request #3 from ctlaultdel/ss-videos-model
ctlaultdel Jan 7, 2023
8d9c65d
Merge pull request #4 from ada-ac2/main
ctlaultdel Jan 7, 2023
f04affb
imported classes and blueprint for rentals endpoint
ctlaultdel Jan 7, 2023
6fa6a57
added customers/customer_id/rentals endpoint
ctlaultdel Jan 7, 2023
774e714
Reinitialized migrations
ctlaultdel Jan 7, 2023
5021439
New module for rentals routes
ctlaultdel Jan 7, 2023
26e19a8
Set up all attributes for models
ctlaultdel Jan 7, 2023
67f05df
refactor
ctlaultdel Jan 7, 2023
46d9040
added clauses to_dict method to return more info about customer and v…
ctlaultdel Jan 7, 2023
f8ddbc0
added display_rentals_by_customer_id endpoint
ctlaultdel Jan 7, 2023
74f2bd9
added available inventory to video model.
smysh Jan 7, 2023
29f62dc
added route to checkout video using POST /rentals/checkout
smysh Jan 7, 2023
5d28269
added route for video check-in. Also added updates to video_check_out…
smysh Jan 7, 2023
e1a734c
added status to rental model.
smysh Jan 8, 2023
9f9f2d4
Wave 1 tests are passing with these fixes.
smysh Jan 8, 2023
4259c63
Wave 2 video checkout tests are passing with these changes.
smysh Jan 8, 2023
4f60d99
updated rental check_in to add more checks.
smysh Jan 8, 2023
df45839
added getting rentals by video_id.
smysh Jan 8, 2023
5664ef5
Made some fixes so Wave 2 all tests are passing.
smysh Jan 8, 2023
aea5f0c
Merge pull request #5 from ctlaultdel/ss-videos-model
smysh Jan 8, 2023
4225112
Merge branch 'main' into LA-wave2
ctlaultdel Jan 8, 2023
fcc26ba
Merge pull request #6 from ctlaultdel/LA-wave2
ctlaultdel Jan 8, 2023
b76799b
modified id to customer_id in response and included videos_checked_ou…
ctlaultdel Jan 8, 2023
0cf86e6
corrected key error problem in wave 1 and wave 2 tests'
ctlaultdel Jan 8, 2023
5733651
LA-wave-03-partial
ctlaultdel Jan 9, 2023
643624e
Fixed typo
ctlaultdel Jan 9, 2023
577623c
updated migrations.
smysh Jan 9, 2023
3a03c90
fixed issue where rentals was showing checked in videos also.
smysh Jan 9, 2023
82e861a
LA-sort-and-count-queries-working
ctlaultdel Jan 9, 2023
7e5be26
Merge branch 'main' of https://github.com/ctlaultdel/retro-video-stor…
smysh Jan 9, 2023
c0e359f
fixes after merge.
smysh Jan 9, 2023
719c105
LA-pagination-feature-working
ctlaultdel Jan 9, 2023
9661d76
Merge branch 'main' of https://github.com/ctlaultdel/retro-video-stor…
smysh Jan 9, 2023
353fb08
bug fixing for merges.
smysh Jan 9, 2023
496aa80
added customer id to response in get rentals.
smysh Jan 9, 2023
27e5bb5
comment out optional Wav4 tests.
smysh Jan 9, 2023
6dceb47
validate request args count and page_num are int type.
smysh Jan 9, 2023
237305f
bug fix for customer query.
smysh Jan 9, 2023
5234714
Merge pull request #7 from ctlaultdel/ss-videos-model
ctlaultdel Jan 9, 2023
d7477d7
Co-authored-by: Larissa Ault <ctlaultdel@users.noreply.github.com>
smysh Jan 9, 2023
59ffd97
Merge pull request #8 from ctlaultdel/ss-videos-model
ctlaultdel Jan 9, 2023
ae23326
Added requirements and Procfile for heroku deployment
ctlaultdel Jan 9, 2023
ce4084d
Refroze the dependencies while in activated venv
ctlaultdel Jan 9, 2023
842ccdd
Deleted extra line
ctlaultdel 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
2 changes: 1 addition & 1 deletion app/models/customer.py
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ class Customer(db.Model):
phone = db.Column(db.String, nullable=False)
registered_at = db.Column(db.DateTime, default=datetime.now())
rentals = db.relationship("Rental", back_populates="customer")
videos_checked_out_count = db.Column(db.Integer)
videos_checked_out_count = db.Column(db.Integer, default=0)

def to_dict(self):
"""
35 changes: 18 additions & 17 deletions app/models/rental.py
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ class Rental(db.Model):
video = db.relationship("Video", back_populates="rentals")
video_id = db.Column(db.Integer, db.ForeignKey("video.id"))

Choose a reason for hiding this comment

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

Nice job setting up the relationships, here

due_date = db.Column(db.DateTime, default=dt.datetime.now()+dt.timedelta(days=7))
status = db.Column(db.String(), default="checked_out")
n_video_copies = db.Column(db.Integer, default=1)

def to_dict(self):
@@ -25,6 +26,14 @@ def to_dict(self):
rental_dict.update(self.video.to_dict())
return rental_dict

@classmethod
def from_dict(cls, rental_data):
new_rental = Rental(
customer_id=rental_data["customer_id"],
video_id=rental_data["video_id"]
)
return new_rental

# def check_out_video(self, n):
# """
# Updates video available inventory when customer checks out video(s)
@@ -43,21 +52,13 @@ def to_dict(self):
# # increment the customer's number of rented videos
# self.customer.check_out_videos()



def check_in_video(self, n):
"""
Updates videos checked out count and video available inventory
:params:
- n: number of videos to check in
"""
self.num_duplicates_video -= n
self.video.calculate_available_inventory()
# def check_in_video(self, n):
# """
# Updates videos checked out count and video available inventory
# :params:
# - n: number of videos to check in
# """
# self.num_duplicates_video -= n
# self.video.calculate_available_inventory()

@classmethod
def from_dict(cls, rental_data):
new_rental = Rental(
due_date=rental_data["due_date"],
videos_checked_out=rental_data["videos_checked_out"],
)
return new_rental

10 changes: 8 additions & 2 deletions app/models/video.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
from app import db

def mydefault(context):
return context.get_current_parameters()['total_inventory']

class Video(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
title = db.Column(db.String())
release_date = db.Column(db.Date())
total_inventory = db.Column(db.Integer())
available_inventory = db.Column(db.Integer(),default=mydefault)
rentals = db.relationship("Rental", back_populates="video")

def to_dict(self):
@@ -13,7 +17,7 @@ def to_dict(self):
video_as_dict["title"] = self.title
video_as_dict["release_date"] = self.release_date
video_as_dict["total_inventory"] = self.total_inventory

video_as_dict["available_inventory"] = self.available_inventory
return video_as_dict

def calculate_available_inventory(self):
@@ -27,6 +31,8 @@ def from_dict(cls, video_data):
new_video = Video(
title=video_data["title"],
release_date=video_data["release_date"],
total_inventory=video_data["total_inventory"],
total_inventory=video_data["total_inventory"]
)
return new_video


8 changes: 6 additions & 2 deletions app/routes/customer_routes.py
Original file line number Diff line number Diff line change
@@ -101,5 +101,9 @@ def display_customer_rentals(customer_id):
customer = validate_model(Customer, customer_id)
rentals_response = []
for rental in customer.rentals:
rentals_response.append(rental.to_dict())
return make_response(jsonify(rentals_response), 200)
rentals_response.append({
"release_date": rental.video.release_date,
"title": rental.video.title,
"due_date": rental.due_date
})
return make_response(jsonify(rentals_response), 200)
65 changes: 64 additions & 1 deletion app/routes/rentals_routes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,69 @@
from app import db
from flask import Blueprint, jsonify, make_response, abort, request
from ..models.rental import Rental
from ..models.customer import Customer
from ..models.video import Video
from ..routes.customer_routes import validate_model
from ..routes.customer_routes import validate_request

# ~~~~~~ initialize rentals blueprint ~~~~~~
rentals_bp = Blueprint("rentals", __name__, url_prefix="/rentals")
rentals_bp = Blueprint("rentals", __name__, url_prefix="/rentals")

@rentals_bp.route("/check-out", methods=["POST"])
def checkout_video():
reqs = {"customer_id", "video_id"}
request_body = validate_request(request, reqs)

video_id = request_body["video_id"]
video = validate_model(Video, video_id)

customer_id = request_body["customer_id"]
customer = validate_model(Customer, customer_id)

if video and customer:
if video.available_inventory<=0:
return make_response(jsonify({"message": f"Could not perform checkout"}), 400)

new_rental = Rental.from_dict(request_body)

new_rental.customer = customer
new_rental.customer.videos_checked_out_count += 1

Choose a reason for hiding this comment

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

Great example making use of the relationships you've set up, to be able to cleanly use SQLAlchemy's OOP interface!


new_rental.video = video
new_rental.video.available_inventory -= 1

db.session.add(new_rental)
db.session.commit()

return make_response(jsonify(new_rental.to_dict()), 200)
else:
return make_response(jsonify({"message": f"Video {video_id} or Customer {customer_id} are invalid"}), 404)

@rentals_bp.route("/check-in", methods=["POST"])
def checkin_video():
reqs = {"customer_id", "video_id"}
request_body = validate_request(request, reqs)

video_id = request_body["video_id"]
video = validate_model(Video, video_id)

customer_id = request_body["customer_id"]
customer = validate_model(Customer, customer_id)

if video and customer:
rental = Rental.query.filter_by(video_id=video_id, customer_id=customer_id).first()
if not rental:
return make_response(jsonify({"message": f"No outstanding rentals for customer {customer_id} and video {video_id}"}), 400)

if rental.status == "checked_in":
return make_response(jsonify({"message": f"Cannot check_in video already checked_in"}), 400)

rental.status = "checked_in"
rental.customer.videos_checked_out_count -= 1
rental.video.available_inventory += 1

db.session.commit()
return make_response(jsonify(rental.to_dict()), 200)
else:
# if not found error out
return make_response(jsonify({"message": f"Video {video_id} or Customer {customer_id} are not found"}), 400)
19 changes: 17 additions & 2 deletions app/routes/video_routes.py
Original file line number Diff line number Diff line change
@@ -69,8 +69,23 @@ def delete_video_by_id(video_id):
}, 200)

@video_bp.route("<video_id>/rentals", methods=["GET"])
def display_rentals_by_customer_id(video_id):
def display_rentals_by_video_id(video_id):
video = validate_model(Video, video_id)
rentals = Rental.query.get(video_id)
return make_response(
jsonify(rentals.to_dict()), 200)
jsonify(rentals.to_dict()), 200)

def get_rentals_by_video_id(video_id):

video = validate_model(Video, video_id)
rentals_response = []
for rental in video.rentals:
rentals_response.append({
"due_date": rental.due_date,
"name": rental.customer.name,
"phone": rental.customer.phone,
"postal_code": rental.customer.postal_code
})

return jsonify(rentals_response)

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""added status to rental and available_inventory to video models.

Revision ID: 7f3a3cc43a5c
Revises: 8b1b64498a3f
Create Date: 2023-01-07 16:24:54.863815

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '7f3a3cc43a5c'
down_revision = '8b1b64498a3f'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('rentals', sa.Column('status', sa.String(), nullable=True))
op.add_column('video', sa.Column('available_inventory', sa.Integer(), nullable=True))
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('video', 'available_inventory')
op.drop_column('rentals', 'status')
# ### end Alembic commands ###