read from the db
This commit is contained in:
54
src/main.py
54
src/main.py
@@ -1,23 +1,45 @@
|
||||
from flask import Flask, jsonify, request
|
||||
from flask import Flask, g, jsonify, request
|
||||
import sqlite3
|
||||
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
def convert(tupple):
|
||||
return {
|
||||
"id": tupple[0],
|
||||
"table_name": tupple[1],
|
||||
"table_link": tupple[2],
|
||||
"active": tupple[3] != 0
|
||||
}
|
||||
|
||||
def get_db():
|
||||
db = getattr(g, '_database', None)
|
||||
if db is None:
|
||||
db = sqlite3.connect("/data/tables.db")
|
||||
return db
|
||||
|
||||
@app.teardown_appcontext
|
||||
def close_connection(exception):
|
||||
db = getattr(g, '_database', None)
|
||||
if db is not None:
|
||||
db.close()
|
||||
|
||||
@app.route("/api/active_tables")
|
||||
def hello():
|
||||
return jsonify([{
|
||||
"id": 1,
|
||||
"table_name": "b",
|
||||
"table_link": "potato",
|
||||
"active": False
|
||||
}])
|
||||
def get_active_tables():
|
||||
db = get_db()
|
||||
c = db.execute('select game_table_id as id, game_table_name as table_name, game_table_link as table_link, active from game_tables where active')
|
||||
v = c.fetchall()
|
||||
c.close()
|
||||
|
||||
return jsonify([convert(x) for x in v])
|
||||
|
||||
@app.route("/api/tables")
|
||||
def potato():
|
||||
return jsonify([{
|
||||
"id": 1,
|
||||
"table_name": "Nuntis",
|
||||
"table_link": "vtt.cyberpunkrush.com/nuntis",
|
||||
"active": False
|
||||
}])
|
||||
def get_tables():
|
||||
db = get_db()
|
||||
c = db.execute('select game_table_id as id, game_table_name, game_table_link, active from game_tables')
|
||||
v = c.fetchall()
|
||||
c.close()
|
||||
|
||||
return jsonify([convert(x) for x in v])
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=True)
|
||||
|
||||
Reference in New Issue
Block a user