Added transactions
This commit is contained in:
50
webservice/api_blueprint_transactions.py
Normal file
50
webservice/api_blueprint_transactions.py
Normal file
@@ -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})
|
@@ -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
|
||||
|
Reference in New Issue
Block a user