Added portfolio endpoint
This commit is contained in:
31
webservice/api_blueprint_portfolio.py
Normal file
31
webservice/api_blueprint_portfolio.py
Normal file
@@ -0,0 +1,31 @@
|
||||
import os
|
||||
|
||||
from flask import Blueprint, jsonify
|
||||
|
||||
from db import db
|
||||
from helper_functions import get_username_from_token_data, extract_token_data, get_token, get_user_id_from_username, return_401
|
||||
from models import Transaction
|
||||
|
||||
portfolio_blueprint = Blueprint('portfolio', __name__, url_prefix='/api')
|
||||
__location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))
|
||||
|
||||
|
||||
@portfolio_blueprint.route('/portfolio', methods=['GET'])
|
||||
def get_portfolio():
|
||||
# get username from jwt token
|
||||
username = get_username_from_token_data(extract_token_data(get_token()))
|
||||
if username is None: # If token not provided or invalid -> return 401 code
|
||||
return return_401()
|
||||
|
||||
return_portfolio = {}
|
||||
transactions = db.session.query(Transaction).filter_by(user_id=get_user_id_from_username(username)).all()
|
||||
|
||||
if transactions is not None:
|
||||
for row in transactions:
|
||||
if row.symbol in return_portfolio:
|
||||
return_portfolio[row.symbol]['count'] += row.count
|
||||
return_portfolio[row.symbol]['last_transaction'] += row.time
|
||||
else:
|
||||
return_portfolio[row.symbol] = {"count": row.count, "last_transaction": row.time}
|
||||
|
||||
return jsonify({"status": 200, "text": "Successfully loaded symbols", "data": return_portfolio})
|
@@ -7,6 +7,7 @@ from api_blueprint_keyword import keyword_blueprint
|
||||
from api_blueprint_shares import shares_blueprint
|
||||
from api_blueprint_user import users_blueprint
|
||||
from api_blueprint_transactions import transaction_blueprint
|
||||
from webservice.api_blueprint_portfolio import portfolio_blueprint
|
||||
|
||||
|
||||
def create_app():
|
||||
@@ -27,6 +28,7 @@ def create_app():
|
||||
application.register_blueprint(keyword_blueprint)
|
||||
application.register_blueprint(shares_blueprint)
|
||||
application.register_blueprint(transaction_blueprint)
|
||||
application.register_blueprint(portfolio_blueprint)
|
||||
application.register_blueprint(users_blueprint)
|
||||
|
||||
# interface blueprint
|
||||
|
Reference in New Issue
Block a user