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
Ocelots - Eva and Lisa #16
Open
wich229
wants to merge
23
commits into
ada-ac2:main
Choose a base branch
from
wich229: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 1 commit
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
ac3dbc8
Added attributes to Customer and Video models, added routes to custom…
lisabethu88 328da39
Changed attributes in models
lisabethu88 117d079
add attrs in customer.py and video.py
wich229 4518081
need to add validate function to see if tests pass
lisabethu88 c98f2fe
customer_routes fixed and pass test.
wich229 1c939cc
wave1 customer and video routes.
wich229 fa58a4e
add rental model and routes_helper.py
wich229 75587be
working on rental routes
lisabethu88 0d51e49
merge changes
lisabethu88 c70fe36
Created route to create a rental
lisabethu88 0fa01d9
completed wave 02
lisabethu88 32abd41
wave 02 changes
lisabethu88 5de4710
rental routes changes
lisabethu88 c3ebae1
changes
lisabethu88 627beb2
fixed bugs
lisabethu88 69d642e
added helper functions, refactored routes
lisabethu88 f836317
refactored wave 01 and wave 02
lisabethu88 90935f2
wave3 pass tests before test on postman
wich229 8b42ff2
tested postman
wich229 5dbd2a1
trim datatime response
wich229 2289536
during testing on postman fixed some bugs
wich229 2b54534
Merge pull request #1 from wich229/Eva_wave03
wich229 17d1f93
add gunicorn in requirements.txt
wich229 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
Next
Next commit
Added attributes to Customer and Video models, added routes to custom…
…ers_routes.py
- Loading branch information
commit ac3dbc82f63733f595327ede8011bb3e587a8652
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
from app import db | ||
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)) |
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,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) |
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
Empty file.
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,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") |
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 @@ | ||
Generic single-database configuration. |
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,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 |
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,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() |
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,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"} |
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.
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.