Changed database model
This commit is contained in:
@@ -4,7 +4,7 @@ from apiflask import APIBlueprint, abort
|
||||
|
||||
from auth import auth
|
||||
from db import db
|
||||
from helper_functions import get_user_id_from_username, get_username_or_abort_401, make_response
|
||||
from helper_functions import make_response, get_email_or_abort_401
|
||||
from models import Share
|
||||
from schema import SymbolSchema, SymbolResponseSchema, DeleteSuccessfulSchema
|
||||
|
||||
@@ -18,17 +18,17 @@ __location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file
|
||||
@shares_blueprint.auth_required(auth)
|
||||
@shares_blueprint.doc(summary="Add new symbol", description="Adds new symbol for current user")
|
||||
def add_symbol(data):
|
||||
username = get_username_or_abort_401()
|
||||
email = get_email_or_abort_401()
|
||||
|
||||
check_if_symbol_data_exists(data)
|
||||
|
||||
symbol = data['symbol']
|
||||
|
||||
check_share = db.session.query(Share).filter_by(symbol=symbol, user_id=get_user_id_from_username(username)).first()
|
||||
check_share = db.session.query(Share).filter_by(symbol=symbol, email=email).first()
|
||||
if check_share is None:
|
||||
# Keyword doesn't exist yet for this user
|
||||
new_symbol = Share(
|
||||
user_id=get_user_id_from_username(username),
|
||||
email=email,
|
||||
symbol=symbol
|
||||
)
|
||||
db.session.add(new_symbol)
|
||||
@@ -45,16 +45,21 @@ def add_symbol(data):
|
||||
@shares_blueprint.auth_required(auth)
|
||||
@shares_blueprint.doc(summary="Removes existing symbol", description="Removes existing symbol for current user")
|
||||
def remove_symbol(data):
|
||||
username = get_username_or_abort_401()
|
||||
email = get_email_or_abort_401()
|
||||
|
||||
check_if_symbol_data_exists(data)
|
||||
|
||||
symbol = data['symbol']
|
||||
|
||||
db.session.query(Share).filter_by(symbol=symbol, user_id=get_user_id_from_username(username)).delete()
|
||||
db.session.commit()
|
||||
check_share = db.session.query(Share).filter_by(symbol=symbol, email=email).first()
|
||||
|
||||
return make_response({}, 200, "Successfully removed symbol")
|
||||
if check_share is None:
|
||||
return make_response({}, 500, "Symbol doesn't exist for this user")
|
||||
else:
|
||||
db.session.query(Share).filter_by(symbol=symbol, email=email).delete()
|
||||
db.session.commit()
|
||||
|
||||
return make_response({}, 200, "Successfully removed symbol")
|
||||
|
||||
|
||||
@shares_blueprint.route('/shares', methods=['GET'])
|
||||
@@ -62,10 +67,10 @@ def remove_symbol(data):
|
||||
@shares_blueprint.auth_required(auth)
|
||||
@shares_blueprint.doc(summary="Returns all symbols", description="Returns all symbols for current user")
|
||||
def get_symbol():
|
||||
username = get_username_or_abort_401()
|
||||
email = get_email_or_abort_401()
|
||||
|
||||
return_symbols = []
|
||||
symbols = db.session.query(Share).filter_by(user_id=get_user_id_from_username(username)).all()
|
||||
symbols = db.session.query(Share).filter_by(email=email).all()
|
||||
|
||||
if symbols is not None:
|
||||
for row in symbols:
|
||||
|
||||
Reference in New Issue
Block a user