2022-03-14 22:40:36 +00:00
|
|
|
import os
|
|
|
|
|
2022-03-17 08:26:25 +00:00
|
|
|
from apiflask import APIBlueprint
|
2022-03-14 22:40:36 +00:00
|
|
|
|
|
|
|
from db import db
|
2022-03-22 10:21:39 +00:00
|
|
|
from helper_functions import get_user_id_from_username, get_username_or_abort_401, make_response
|
2022-03-14 22:40:36 +00:00
|
|
|
from models import Transaction
|
2022-03-17 08:26:25 +00:00
|
|
|
from auth import auth
|
2022-03-14 22:40:36 +00:00
|
|
|
|
2022-03-17 08:26:25 +00:00
|
|
|
portfolio_blueprint = APIBlueprint('portfolio', __name__, url_prefix='/api')
|
2022-03-14 22:40:36 +00:00
|
|
|
__location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))
|
|
|
|
|
|
|
|
|
|
|
|
@portfolio_blueprint.route('/portfolio', methods=['GET'])
|
2022-03-17 08:26:25 +00:00
|
|
|
@portfolio_blueprint.output(200)
|
|
|
|
@portfolio_blueprint.auth_required(auth)
|
|
|
|
@portfolio_blueprint.doc(summary="Returns portfolio", description="Returns all shares of current user")
|
2022-03-14 22:40:36 +00:00
|
|
|
def get_portfolio():
|
2022-03-17 08:26:25 +00:00
|
|
|
username = get_username_or_abort_401()
|
2022-03-14 22:40:36 +00:00
|
|
|
|
|
|
|
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
|
2022-03-15 08:45:01 +00:00
|
|
|
return_portfolio[row.symbol]['last_transaction'] = row.time
|
2022-03-14 22:40:36 +00:00
|
|
|
else:
|
|
|
|
return_portfolio[row.symbol] = {"count": row.count, "last_transaction": row.time}
|
|
|
|
|
2022-03-22 10:21:39 +00:00
|
|
|
return make_response(return_portfolio, 200, "Successfully loaded symbols")
|