From 4cda34e2c8ef843ca4a711c709a23eb43cf5665d Mon Sep 17 00:00:00 2001 From: H4CK3R-01 Date: Mon, 14 Mar 2022 18:13:16 +0100 Subject: [PATCH] Added transactions --- docs/postman.json | 52 +++++++++++++++++++++++- webservice/api_blueprint_transactions.py | 50 +++++++++++++++++++++++ webservice/app.py | 2 + 3 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 webservice/api_blueprint_transactions.py diff --git a/docs/postman.json b/docs/postman.json index 418f46c..dae03e7 100644 --- a/docs/postman.json +++ b/docs/postman.json @@ -284,6 +284,56 @@ "response": [] } ] + }, + { + "name": "Transactions", + "item": [ + { + "name": "/api/keywords", + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "{{BASE_URL}}/api/transactions", + "host": [ + "{{BASE_URL}}" + ], + "path": [ + "api", + "transactions" + ] + } + }, + "response": [] + }, + { + "name": "/api/keyword", + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "{\n \"symbol\": \"DTEGY\",\n \"time\": \"2021-03-14T18:08:44.625Z\",\n \"count\": 1,\n \"price\": 10.0\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "{{BASE_URL}}/api/transaction", + "host": [ + "{{BASE_URL}}" + ], + "path": [ + "api", + "transaction" + ] + } + }, + "response": [] + } + ] } ], "auth": { @@ -291,7 +341,7 @@ "bearer": [ { "key": "token", - "value": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6IlVzZXJuYW1lIiwiZXhwIjoxNjQ3Mjc1OTI2fQ.MhXTrfeLQmZ8dPzMOcNSTg1PUw4AU-aZ7h_zRmk8ibc", + "value": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6IlVzZXJuYW1lIiwiZXhwIjoxNjQ3MjgwMjkwfQ.SL_WpvJBA1XG_BVwD-zIS4-YnGvxbNqluy5fWjGp2DQ", "type": "string" } ] diff --git a/webservice/api_blueprint_transactions.py b/webservice/api_blueprint_transactions.py new file mode 100644 index 0000000..e47b33e --- /dev/null +++ b/webservice/api_blueprint_transactions.py @@ -0,0 +1,50 @@ +import os +import datetime + +from flask import Blueprint, jsonify, request + +from db import db +from helper_functions import get_username_from_token_data, extract_token_data, get_token, get_user_id_from_username +from models import Transaction + +transaction_blueprint = Blueprint('transaction', __name__, url_prefix='/api') +__location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__))) + + +@transaction_blueprint.route('/transaction', methods=['POST']) +def add_transaction(): + request_data = request.get_json() + symbol = request_data['symbol'] + time = datetime.datetime.strptime(request_data['time'], '%Y-%m-%dT%H:%M:%S.%fZ') + count = request_data['count'] + price = request_data['price'] + + # get username from jwt token + username = get_username_from_token_data(extract_token_data(get_token())) + + new_transcation = Transaction( + user_id=get_user_id_from_username(username), + symbol=symbol, + time=time, + count=count, + price=price + ) + db.session.add(new_transcation) + db.session.commit() + + return jsonify({"status": 200, "text": "Successfully added transaction", "data": new_transcation.as_dict()}) + + +@transaction_blueprint.route('/transactions', methods=['GET']) +def get_transaction(): + # get username from jwt token + username = get_username_from_token_data(extract_token_data(get_token())) + + return_transactions = [] + 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: + return_transactions.append(row.as_dict()) + + return jsonify({"status": 200, "text": "Successfully loaded transactions", "data": return_transactions}) diff --git a/webservice/app.py b/webservice/app.py index f8dac64..9d63b26 100644 --- a/webservice/app.py +++ b/webservice/app.py @@ -6,6 +6,7 @@ from blueprint_interface import interface_blueprint 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 def create_app(): @@ -25,6 +26,7 @@ def create_app(): # api blueprints application.register_blueprint(keyword_blueprint) application.register_blueprint(shares_blueprint) + application.register_blueprint(transaction_blueprint) application.register_blueprint(users_blueprint) # interface blueprint