32 lines
1.3 KiB
Python
32 lines
1.3 KiB
Python
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})
|