TelegramAktienBot/api/api_blueprint_portfolio.py

34 lines
1.2 KiB
Python
Raw Normal View History

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
2022-03-27 16:19:51 +00:00
from schema import PortfolioResponseSchema
2022-03-14 22:40:36 +00:00
from db import db
2022-03-27 15:23:33 +00:00
from helper_functions import make_response, get_email_or_abort_401
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-27 15:23:33 +00:00
@portfolio_blueprint.output(PortfolioResponseSchema(many=True), 200)
2022-03-17 08:26:25 +00:00
@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-27 15:23:33 +00:00
email = get_email_or_abort_401()
2022-03-14 22:40:36 +00:00
2022-03-27 15:23:33 +00:00
return_portfolio = []
transactions = db.session.execute("SELECT symbol, SUM(count), SUM(price), MAX(time) FROM `transactions` WHERE email = '" + email + "' GROUP BY symbol;").all()
2022-03-14 22:40:36 +00:00
if transactions is not None:
for row in transactions:
2022-03-27 15:23:33 +00:00
return_portfolio.append({
"symbol": row[0],
"count": row[1],
# "price": row[2],
"last_transaction": row[3]
})
2022-03-14 22:40:36 +00:00
2022-03-22 10:21:39 +00:00
return make_response(return_portfolio, 200, "Successfully loaded symbols")