Add dao's

This commit is contained in:
iamBadgers
2026-03-22 20:26:46 -07:00
parent de7c604f91
commit 64c1f52cdf
4 changed files with 152 additions and 22 deletions

9
src/database.py Normal file
View 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
View 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
View File

View File

@@ -1,74 +1,111 @@
from flask import Flask, g, jsonify, request
import sqlite3
import game_tables
app = Flask(__name__)
def convert(tupple):
def convertTable(tupple):
return {
"id": tupple[0],
"table_name": tupple[1],
"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():
db = getattr(g, '_database', None)
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)
db = getattr(g, "_database", None)
if db is not None:
db.close()
@app.route("/api/active_tables")
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()
cursor = db.cursor()
tables = game_tables.read_active(cursor)
cursor.close()
return jsonify([table.toJson() for table in tables])
return jsonify([convert(x) for x in v])
@app.route("/api/tables")
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()
cursor = db.cursor()
tables = game_tables.read_all(cursor)
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")
def activate_table(table_id):
db = get_db()
c = db.execute('select * from game_keys where game_table_id is null')
v = c.fetchall()
c = db.execute("select * from game_keys where game_table_id is null")
keys = c.fetchall()
c.close()
if (len(v) == 0):
if len(keys) == 0:
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')
v = c.fetchall()
key = convertKey(keys[0])
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()
if (len(v) == 0):
if len(tables) == 0:
return "No such table", 404
table = convert(v[0])
if (table["active"]):
table = convertTable(tables[0])
if table["active"]:
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")
def deactivate_table(table_id):
return
if __name__ == '__main__':
if __name__ == "__main__":
app.run(debug=True)