Added transactions
This commit is contained in:
parent
e82b614265
commit
4cda34e2c8
@ -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"
|
||||
}
|
||||
]
|
||||
|
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
|
||||
|
Loading…
Reference in New Issue
Block a user