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
Next Next commit
Added attributes to Customer and Video models, added routes to custom…
…ers_routes.py
  • Loading branch information
lisabethu88 committed Jan 4, 2023
commit ac3dbc82f63733f595327ede8011bb3e587a8652
104 changes: 104 additions & 0 deletions app/customers_routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
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 flask import Blueprint, jsonify, abort, make_response, request
"""
add 404 error checking if customer does not exist and edge cases
"""
customers_bp = Blueprint("customers_bp", __name__, url_prefix="/customers")

# POST /customers
@customers_bp.route("",methods=["POST"])
def create_customer():
customer_data = request.get_json()
new_customer = Customer(
name = customer_data["name"],
postal_code = customer_data["postal_code"],
phone_number = customer_data["phone_number"],
register_at = customer_data["register_at"],
videos_checked_out_count = customer_data["videos_checked_out_count"]
)

db.session.add(new_customer)
db.session.commit()

return make_response(f"Customer {new_customer.name} created", 201)

# GET /customers X
@customers_bp.route("", methods=["GET"])
def get_customers_optional_query():
customer_query = Customer.query
# queries??
sort_query = request.args.get("sort")
if sort_query:
if sort_query == "desc":
customer_query = customer_query.order_by(Customer.videos_checked_out_count.desc())
else:
customer_query = customer_query.order_by(Customer.videos_checked_out_count.asc())


customers = customer_query.all()
customer_response = []
for customer in customers:
customer_response.append({
"id": customer.id,
"name": customer.name,
"postal_code": customer.postal_code,
"phone_number": customer.phone_number,
"register_at": customer.register_at,
"videos_checked_out_count": customer.videos_checked_out_count
})

return jsonify(customer_response)

# GET /customers/<id>
@customers_bp.route("/<customer_id>", methods=["GET"])
def get_customer_by_id(customer_id):
customer_to_return = validate_id_and_return_customer(customer_id)

return jsonify({
"id": customer_to_return.id,
"name": customer_to_return.name,
"postal_code": customer_to_return.postal_code,
"phone_number": customer_to_return.phone_number,
"register_at": customer_to_return.register_at,
"videos_checked_out_count": customer_to_return.videos_checked_out_count
})

# PUT /customers/<id>
@customers_bp.route("/<customer_id>", methods=["PUT"])
def replace_customer_with_id(customer_id):
customer_data = request.get_json()
customer_to_update = validate_id_and_return_customer(customer_id)

customer_to_update.name = customer_data["name"],
customer_to_update.postal_code = customer_data["postal_code"],
customer_to_update.phone_number = customer_data["phone_number"],
customer_to_update.register_at = customer_data["register_at"],
customer_to_update.videos_checked_out_count = customer_data["videos_checked_out_count"]

db.session.commit()

return make_response(f"Customer {customer_to_update.name} updated", 200)

# DELETE /customers/<id>
@customers_bp.route("/<customer_id>", methods=["DELETE"])
def delete_customer_by_id(customer_id):
customer_to_delete = validate_id_and_return_customer(customer_id)
db.session.delete(customer_to_delete)
db.session.commit()

return make_response(f"Customer {customer_to_delete.name} deleted", 200)

# Helper Function
def validate_id_and_return_customer(customer_id):
try:
customer_id_as_int = int(customer_id)
except:
msg = f"Customer's id {customer_id} is not an integer"
abort(make_response({"message": msg}, 400))

customer = Customer.query.get(customer_id_as_int)
if customer:
return customer

abort(make_response({"message": f"Customer with id {customer_id} not found"}, 404))
7 changes: 7 additions & 0 deletions app/models/customer.py
Original file line number Diff line number Diff line change
@@ -2,3 +2,10 @@

class Customer(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.Text)
postal_code = db.Column(db.Varchar(10)) # 10 or 5?
phone_number = db.Column(db.Varchar(20)) # ??
register_at = db.Column(db.Datetime)
videos_checked_out_count = db.Column(db.Integer)


2 changes: 1 addition & 1 deletion app/models/rental.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from app import db

class Rental(db.Model):
id = db.Column(db.Integer, primary_key=True)
id = db.Column(db.Integer, primary_key=True)
3 changes: 3 additions & 0 deletions app/models/video.py
Original file line number Diff line number Diff line change
@@ -2,3 +2,6 @@

class Video(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.Text)
release_date = db.Column(db.Datetime)
total_inventory = db.Column(db.Integer)
Empty file removed app/routes.py
Empty file.
5 changes: 5 additions & 0 deletions app/videos_routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from app import db
from app.models.video import Videos
from flask import Blueprint, jsonify, abort, make_response, request

videos_bp = Blueprint("videos_bp", __name__, url_prefix="/videos")
1 change: 1 addition & 0 deletions migrations/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Generic single-database configuration.
45 changes: 45 additions & 0 deletions migrations/alembic.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# A generic, single database configuration.

[alembic]
# template used to generate migration files
# file_template = %%(rev)s_%%(slug)s

# set to 'true' to run the environment during
# the 'revision' command, regardless of autogenerate
# revision_environment = false


# Logging configuration
[loggers]
keys = root,sqlalchemy,alembic

[handlers]
keys = console

[formatters]
keys = generic

[logger_root]
level = WARN
handlers = console
qualname =

[logger_sqlalchemy]
level = WARN
handlers =
qualname = sqlalchemy.engine

[logger_alembic]
level = INFO
handlers =
qualname = alembic

[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic

[formatter_generic]
format = %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %H:%M:%S
96 changes: 96 additions & 0 deletions migrations/env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
from __future__ import with_statement

import logging
from logging.config import fileConfig

from sqlalchemy import engine_from_config
from sqlalchemy import pool
from flask import current_app

from alembic import context

# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config

# Interpret the config file for Python logging.
# This line sets up loggers basically.
fileConfig(config.config_file_name)
logger = logging.getLogger('alembic.env')

# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
config.set_main_option(
'sqlalchemy.url',
str(current_app.extensions['migrate'].db.engine.url).replace('%', '%%'))
target_metadata = current_app.extensions['migrate'].db.metadata

# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.


def run_migrations_offline():
"""Run migrations in 'offline' mode.

This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.

Calls to context.execute() here emit the given string to the
script output.

"""
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url, target_metadata=target_metadata, literal_binds=True
)

with context.begin_transaction():
context.run_migrations()


def run_migrations_online():
"""Run migrations in 'online' mode.

In this scenario we need to create an Engine
and associate a connection with the context.

"""

# this callback is used to prevent an auto-migration from being generated
# when there are no changes to the schema
# reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html
def process_revision_directives(context, revision, directives):
if getattr(config.cmd_opts, 'autogenerate', False):
script = directives[0]
if script.upgrade_ops.is_empty():
directives[:] = []
logger.info('No changes in schema detected.')

connectable = engine_from_config(
config.get_section(config.config_ini_section),
prefix='sqlalchemy.',
poolclass=pool.NullPool,
)

with connectable.connect() as connection:
context.configure(
connection=connection,
target_metadata=target_metadata,
process_revision_directives=process_revision_directives,
**current_app.extensions['migrate'].configure_args
)

with context.begin_transaction():
context.run_migrations()


if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
24 changes: 24 additions & 0 deletions migrations/script.py.mako
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""${message}

Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}

"""
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}

# revision identifiers, used by Alembic.
revision = ${repr(up_revision)}
down_revision = ${repr(down_revision)}
branch_labels = ${repr(branch_labels)}
depends_on = ${repr(depends_on)}


def upgrade():
${upgrades if upgrades else "pass"}


def downgrade():
${downgrades if downgrades else "pass"}