From 859935cc015ae7c2bebf860b69f525978b59963c Mon Sep 17 00:00:00 2001 From: H4CK3R-01 Date: Mon, 14 Mar 2022 23:40:36 +0100 Subject: [PATCH] Added portfolio endpoint --- docs/postman.json | 25 ++++++++++++++++++++- webservice/api_blueprint_portfolio.py | 31 +++++++++++++++++++++++++++ webservice/app.py | 2 ++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 webservice/api_blueprint_portfolio.py diff --git a/docs/postman.json b/docs/postman.json index 134841e..f8260ec 100644 --- a/docs/postman.json +++ b/docs/postman.json @@ -334,6 +334,29 @@ "response": [] } ] + }, + { + "name": "Portfolio", + "item": [ + { + "name": "/api/portfolio", + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "{{BASE_URL}}/api/portfolio", + "host": [ + "{{BASE_URL}}" + ], + "path": [ + "api", + "portfolio" + ] + } + }, + "response": [] + } + ] } ], "auth": { @@ -341,7 +364,7 @@ "bearer": [ { "key": "token", - "value": "access_token", + "value": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6IlVzZXJuYW1lIiwiZXhwIjoxNjQ3Mjk5MzIxfQ.5UoHi9Zu6p9szSVKK2_1Ln2uru4RVTGQl0MyHDB4sqg", "type": "string" } ] diff --git a/webservice/api_blueprint_portfolio.py b/webservice/api_blueprint_portfolio.py new file mode 100644 index 0000000..f8a715a --- /dev/null +++ b/webservice/api_blueprint_portfolio.py @@ -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}) diff --git a/webservice/app.py b/webservice/app.py index 9d63b26..80216f3 100644 --- a/webservice/app.py +++ b/webservice/app.py @@ -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