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 - Eva and Lisa #16

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
tested postman
  • Loading branch information
wich229 committed Jan 8, 2023
commit 8b42ff265710dd53fa9823e0fcc087bf585700ed
3 changes: 0 additions & 3 deletions app/customers_routes.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
from app import db

Choose a reason for hiding this comment

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

If there's a single route file it's okay to leave it in the app folder, but if we have more than one, for organization we should create a folder to hold all the route files.

from app.models.customer import Customer
from app.models.rental import Rental
from app.models.video import Video
from flask import Blueprint, jsonify, abort, make_response, request
from app.routes_helper import validate_model
import datetime
from flask_sqlalchemy import Pagination


4 changes: 2 additions & 2 deletions app/models/video.py
Original file line number Diff line number Diff line change
@@ -5,8 +5,8 @@ class Video(db.Model):
id = db.Column(db.Integer, primary_key = True, autoincrement = True)
title = db.Column(db.String)
release_date = db.Column(db.DateTime, default=datetime.date.today())
total_inventory = db.Column(db.Integer)
available_inventory = db.Column(db.Integer)
total_inventory = db.Column(db.Integer, default=0)
available_inventory = db.Column(db.Integer, default=0)

Choose a reason for hiding this comment

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

This is another field that we could derive, this time using total_inventory and the count of current rentals with this video id.

customers = db.relationship("Customer", secondary="rental", back_populates="videos")

def to_dict(self):
26 changes: 21 additions & 5 deletions app/videos_routes.py
Original file line number Diff line number Diff line change
@@ -15,12 +15,28 @@ def get_all_videos():

###### refactor ######
sort_query = request.args.get("sort")
if sort_query == "asc":
videos_query = videos_query.order_by(Video.name.asc())
if sort_query == "desc":
videos_query = videos_query.order_by(Video.name.desc())
# check sort
if sort_query == "title":
videos_query = videos_query.order_by(Video.title.asc())
elif sort_query == "release_date":
videos_query = videos_query.order_by(Video.release_date.asc())
else:
videos_query = videos_query.order_by(Video.id.asc())


videos = videos_query.all()
count_query = request.args.get("count", type=int)
page_num_query = request.args.get("page_num", type=int)

# check count
if count_query and not page_num_query:
page = videos_query.paginate(page=1, per_page=count_query)
videos = page.items
elif count_query and page_num_query:
page = videos_query.paginate(page=page_num_query, per_page=count_query)
videos = page.items
else:
videos = videos_query.all()


videos_response = []
for video in videos:
30 changes: 30 additions & 0 deletions migrations/versions/313fd793f3c1_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""empty message

Revision ID: 313fd793f3c1
Revises: 4f91cfaaeb9c
Create Date: 2023-01-08 14:46:07.293738

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '313fd793f3c1'
down_revision = '4f91cfaaeb9c'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('rental', 'status')
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.add_column('rental', sa.Column('status', sa.VARCHAR(), autoincrement=False, nullable=True))
# ### end Alembic commands ###