Skip to content

Commit

Permalink
πŸŒ’πŸ«₯ ↝ Set up endpoint for creating users from frontend POST request
Browse files Browse the repository at this point in the history
  • Loading branch information
Gizmotronn committed Jan 5, 2023
1 parent fd0c790 commit 9bd1990
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 12 deletions.
2 changes: 2 additions & 0 deletions backend/.flaskenv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FLASK_APP=app
FLASK_DEBUG=True
2 changes: 2 additions & 0 deletions backend/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ requests = "*"
matplotlib = "*"
lightkurve = "*"
thirdweb-sdk = "*"
python-dotenv = "*"
psycopg2-binary = "*"

[dev-packages]

Expand Down
95 changes: 90 additions & 5 deletions backend/Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 35 additions & 7 deletions backend/app.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,49 @@
from flask import Flask, jsonify, request, make_response
import lightkurve as lk
import os
from flask import Flask, request#, jsonify, make_response
from dotenv import load_dotenv
import psycopg2
#import lightkurve as lk
#from thirdweb import ThirdwebSDK

load_dotenv()
app = Flask(__name__)
url = os.getenv("DATABASE_URL")
connection = psycopg2.connect(url)
#proposalsSDK = ThirdwebSDK('goerli')

# PostgreSQL queries
CREATE_USERS_TABLE = (
'CREATE TABLE IF NOT EXISTS users (id SERIAL PRIMARY KEY, address TEXT);'
)
CREATE_PLANETS_TABLE = (
"""CREATE TABLE IF NOT EXISTS planets (user_id INTEGER, temperature REAL, date TIMESTAMP, FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE);"""
)
INSERT_USER_RETURN_ID = 'INSERT INTO users (address) VALUES (%s) RETURNING id;'
INSERT_PLANET = (
'INSERT INTO planets (user_id, temperature, date) VALUES (%s, %s, %s);'
)

@app.route('/')
def index():
return "Hello World"

@app.route('/createPlanet', methods=["GET", "POST"])
def createPlanet():
return "Hi"
# User Management
@app.post('/api/user')
def addUser():
data = request.get_json()
address = data['address']

# Connect to the database
with connection:
with connection.cursor() as cursor:
cursor.execute(CREATE_USERS_TABLE)
cursor.execute(INSERT_USER_RETURN_ID, (address,))
user_id = cursor.fetchone()[0]

return {'id': user_id, 'message': f"User {address} created"}, 201

#@app.route('/proposals')
#def getProposals():
#contract = proposalsSDK.get_contract('0xCcaA1ABA77Bae6296D386C2F130c46FEc3E5A004')
#proposals = contract.call("getProposals")

#return proposals"""
#return proposals"""

1 comment on commit 9bd1990

@Gizmotronn
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.