Add dao's
This commit is contained in:
9
src/database.py
Normal file
9
src/database.py
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
from flask import g
|
||||||
|
import sqlite
|
||||||
|
|
||||||
|
|
||||||
|
def get_db():
|
||||||
|
db = getattr(g, "_database", None)
|
||||||
|
if db is None:
|
||||||
|
db = sqlite3.connect("/data/tables.db")
|
||||||
|
return db
|
||||||
84
src/game_tables.py
Normal file
84
src/game_tables.py
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
class GameTable:
|
||||||
|
game_table_id: int
|
||||||
|
game_table_name: str
|
||||||
|
game_table_link: str
|
||||||
|
active: bool
|
||||||
|
created: bool
|
||||||
|
updated: bool
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
game_table_id: int,
|
||||||
|
game_table_name: str,
|
||||||
|
game_table_link: str,
|
||||||
|
active: bool,
|
||||||
|
created: bool = False,
|
||||||
|
updated: bool = False,
|
||||||
|
):
|
||||||
|
self.game_table_id = game_table_id
|
||||||
|
self.game_table_name = game_table_name
|
||||||
|
self.game_table_link = game_table_link
|
||||||
|
self.active = active
|
||||||
|
self.created = created
|
||||||
|
self.updated = updated
|
||||||
|
|
||||||
|
def toJson(self):
|
||||||
|
return {
|
||||||
|
"id": self.game_table_id,
|
||||||
|
"table_name": self.game_table_name,
|
||||||
|
"table_link": self.game_table_link,
|
||||||
|
"active": self.active,
|
||||||
|
}
|
||||||
|
|
||||||
|
def commit(self, cursor):
|
||||||
|
if not self.created:
|
||||||
|
cursor.execute(
|
||||||
|
"""INSERT INTO game_tables
|
||||||
|
(game_table_id, game_table_name, game_table_link, active)
|
||||||
|
VALUES (?, ?, ?, ?)""",
|
||||||
|
(
|
||||||
|
self.game_table_id,
|
||||||
|
self.game_table_name,
|
||||||
|
self.game_table_link,
|
||||||
|
self.active,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
self.created = True
|
||||||
|
return
|
||||||
|
if not self.updated:
|
||||||
|
cursor.execute(
|
||||||
|
"""UPDATE game_tables
|
||||||
|
SET game_table_name = ?, game_table_link = ?, active = ?
|
||||||
|
WHERE game_table_id = ?""",
|
||||||
|
(
|
||||||
|
self.game_table_name,
|
||||||
|
self.game_table_link,
|
||||||
|
self.active,
|
||||||
|
self.game_table_id,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
self.updated = False
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
def read_by_id(game_table_id, cursor):
|
||||||
|
rows = cursor.execute(
|
||||||
|
"SELECT game_table_id, game_table_name, game_table_link, active FROM game_tables WHERE game_table_id = ?",
|
||||||
|
(game_table_id,),
|
||||||
|
).fetchone()
|
||||||
|
|
||||||
|
return GameTable(rows[0], rows[1], rows[2], rows[3], True, True)
|
||||||
|
|
||||||
|
|
||||||
|
def read_all(cursor):
|
||||||
|
rows = cursor.execute(
|
||||||
|
"SELECT game_table_id, game_table_name, game_table_link, active FROM game_tables"
|
||||||
|
).fetchall()
|
||||||
|
return [GameTable(row[0], row[1], row[2], row[3], True, True) for row in rows]
|
||||||
|
|
||||||
|
|
||||||
|
def read_active(cursor):
|
||||||
|
rows = cursor.execute(
|
||||||
|
"SELECT game_table_id, game_table_name, game_table_link, active FROM game_tables WHERE active != 0"
|
||||||
|
).fetchall()
|
||||||
|
return [GameTable(row[0], row[1], row[2], row[3], True, True) for row in rows]
|
||||||
0
src/key_tables.py
Normal file
0
src/key_tables.py
Normal file
81
src/main.py
81
src/main.py
@@ -1,74 +1,111 @@
|
|||||||
from flask import Flask, g, jsonify, request
|
from flask import Flask, g, jsonify, request
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
import game_tables
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
def convert(tupple):
|
|
||||||
|
def convertTable(tupple):
|
||||||
return {
|
return {
|
||||||
"id": tupple[0],
|
"id": tupple[0],
|
||||||
"table_name": tupple[1],
|
"table_name": tupple[1],
|
||||||
"table_link": tupple[2],
|
"table_link": tupple[2],
|
||||||
"active": tupple[3] != 0
|
"active": tupple[3] != 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def convertKey(tupple):
|
||||||
|
return {"key": tupple[0], "table_id": tupple[1]}
|
||||||
|
|
||||||
|
|
||||||
def get_db():
|
def get_db():
|
||||||
db = getattr(g, '_database', None)
|
db = getattr(g, "_database", None)
|
||||||
if db is None:
|
if db is None:
|
||||||
db = sqlite3.connect("/data/tables.db")
|
db = sqlite3.connect("/data/tables.db")
|
||||||
return db
|
return db
|
||||||
|
|
||||||
|
|
||||||
@app.teardown_appcontext
|
@app.teardown_appcontext
|
||||||
def close_connection(exception):
|
def close_connection(exception):
|
||||||
db = getattr(g, '_database', None)
|
db = getattr(g, "_database", None)
|
||||||
if db is not None:
|
if db is not None:
|
||||||
db.close()
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
@app.route("/api/active_tables")
|
@app.route("/api/active_tables")
|
||||||
def get_active_tables():
|
def get_active_tables():
|
||||||
db = get_db()
|
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')
|
cursor = db.cursor()
|
||||||
v = c.fetchall()
|
tables = game_tables.read_active(cursor)
|
||||||
c.close()
|
cursor.close()
|
||||||
|
|
||||||
|
return jsonify([table.toJson() for table in tables])
|
||||||
|
|
||||||
return jsonify([convert(x) for x in v])
|
|
||||||
|
|
||||||
@app.route("/api/tables")
|
@app.route("/api/tables")
|
||||||
def get_tables():
|
def get_tables():
|
||||||
db = get_db()
|
db = get_db()
|
||||||
c = db.execute('select game_table_id as id, game_table_name, game_table_link, active from game_tables')
|
cursor = db.cursor()
|
||||||
v = c.fetchall()
|
tables = game_tables.read_all(cursor)
|
||||||
c.close()
|
cursor.close();
|
||||||
|
|
||||||
|
return jsonify([table.toJson() for table in tables])
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/api/tables/<table_id>")
|
||||||
|
def get_table(table_id):
|
||||||
|
db = get_db()
|
||||||
|
cursor = db.cursor()
|
||||||
|
table = game_tables.read_by_id(table_id, cursor)
|
||||||
|
cursor.close()
|
||||||
|
|
||||||
|
return jsonify(table.toJson())
|
||||||
|
|
||||||
return jsonify([convert(x) for x in v])
|
|
||||||
|
|
||||||
@app.route("/api/tables/<table_id>:start")
|
@app.route("/api/tables/<table_id>:start")
|
||||||
def activate_table(table_id):
|
def activate_table(table_id):
|
||||||
db = get_db()
|
db = get_db()
|
||||||
c = db.execute('select * from game_keys where game_table_id is null')
|
c = db.execute("select * from game_keys where game_table_id is null")
|
||||||
v = c.fetchall()
|
keys = c.fetchall()
|
||||||
c.close()
|
c.close()
|
||||||
|
|
||||||
if (len(v) == 0):
|
if len(keys) == 0:
|
||||||
return "No more unused keys", 400
|
return "No more unused keys", 400
|
||||||
|
|
||||||
c = db.execute('select game_table_id as id, game_table_name, game_table_link, active from game_tables')
|
key = convertKey(keys[0])
|
||||||
v = c.fetchall()
|
|
||||||
|
c = db.execute(
|
||||||
|
"select game_table_id as id, game_table_name, game_table_link, active from game_tables where game_table_id = ?",
|
||||||
|
(table_id,),
|
||||||
|
)
|
||||||
|
tables = c.fetchall()
|
||||||
c.close()
|
c.close()
|
||||||
|
|
||||||
if (len(v) == 0):
|
if len(tables) == 0:
|
||||||
return "No such table", 404
|
return "No such table", 404
|
||||||
|
|
||||||
table = convert(v[0])
|
table = convertTable(tables[0])
|
||||||
if (table["active"]):
|
if table["active"]:
|
||||||
return "Table already active", 400
|
return "Table already active", 400
|
||||||
|
|
||||||
|
c = db.cursor()
|
||||||
|
c.execute(
|
||||||
|
"UPDATE game_tables SET active = 1 WHERE game_table_id = ?", (table["id"],)
|
||||||
|
)
|
||||||
|
c.execute(
|
||||||
|
"UPDATE game_keys SET game_table_id = ? WHERE key = ?",
|
||||||
|
(table["id"], key["key"]),
|
||||||
|
)
|
||||||
|
c.close()
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
return jsonify(table)
|
||||||
|
|
||||||
return jsonify(v)
|
|
||||||
|
|
||||||
@app.route("/api/tables/<table_id>:stop")
|
@app.route("/api/tables/<table_id>:stop")
|
||||||
def deactivate_table(table_id):
|
def deactivate_table(table_id):
|
||||||
return
|
return
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
|
if __name__ == "__main__":
|
||||||
app.run(debug=True)
|
app.run(debug=True)
|
||||||
|
|||||||
Reference in New Issue
Block a user