Set up database creation on start.

This commit is contained in:
iamBadgers
2026-04-09 23:17:42 -07:00
parent e475dbc36b
commit c63e9b19bd
4 changed files with 49 additions and 22 deletions

View File

@@ -9,6 +9,33 @@ def get_db():
return db return db
_game_table_create = """
CREATE TABLE IF NOT EXISTS "game_tables" (
"game_table_id" INTEGER NOT NULL UNIQUE,
"game_table_name" TEXT UNIQUE,
"game_table_link" TEXT UNIQUE,
"active" INTEGER DEFAULT 0,
"game_key" TEXT,
"docker_id" TEXT,
PRIMARY KEY("game_table_id")
);
"""
_key_table_create = """
CREATE TABLE IF NOT EXISTS "game_keys" (
"key" TEXT NOT NULL,
"game_table_id" INTEGER,
PRIMARY KEY("key")
);
"""
def init_db(connection):
with SmartCursor(connection=connection) as smart_cursor:
smart_cursor.execute(_game_table_create)
smart_cursor.execute(_key_table_create)
connection.commit()
class SmartCursor: class SmartCursor:
connection: Connection connection: Connection
curosr: Cursor curosr: Cursor

View File

@@ -2,6 +2,7 @@ from sqlite3 import Connection, Cursor
from database import SmartCursor from database import SmartCursor
import random import random
class GameTable: class GameTable:
_game_table_id: int _game_table_id: int
_game_table_name: str _game_table_name: str

View File

@@ -1,27 +1,13 @@
from flask import Flask, g, jsonify, request from flask import Flask, g, jsonify, request
import sqlite3 import sqlite3
import container_managment import container_managment
from database import SmartCursor from database import SmartCursor, get_db, init_db
import tables import tables
def create_app():
app = Flask(__name__) app = Flask(__name__)
def convertTable(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 @app.teardown_appcontext
def close_connection(exception): def close_connection(exception):
db = getattr(g, "_database", None) db = getattr(g, "_database", None)
@@ -30,5 +16,11 @@ def close_connection(exception):
app.register_blueprint(tables.tables, url_prefix="/api") app.register_blueprint(tables.tables, url_prefix="/api")
with app.app_context():
init_db(get_db())
return app
if __name__ == "__main__": if __name__ == "__main__":
app.run(debug=True) create_app.run(debug=True)

View File

@@ -7,6 +7,7 @@ from database import get_db, SmartCursor
tables = Blueprint("tables_api", __name__) tables = Blueprint("tables_api", __name__)
@tables.route("/tables", methods=["POST"]) @tables.route("/tables", methods=["POST"])
def create_table(): def create_table():
db = get_db() db = get_db()
@@ -22,26 +23,31 @@ def create_table():
db.commit() db.commit()
return jsonify({}), 200 return jsonify({}), 200
@tables.route("/tables/<table_id>") @tables.route("/tables/<table_id>")
def get_table(table_id): def get_table(table_id):
gameService = GameService(get_db()) gameService = GameService(get_db())
table = gameService.read_by_id(table_id) table = gameService.read_by_id(table_id)
return jsonify(table.toJson()) return jsonify(table.toJson())
@tables.route("/tables/<int:table_id>", methods=["POST"]) @tables.route("/tables/<int:table_id>", methods=["POST"])
def update_table(table_id): def update_table(table_id):
return return
@tables.route("tables/<table_id>", methods=["DELETE"]) @tables.route("tables/<table_id>", methods=["DELETE"])
def delete_table(table_id): def delete_table(table_id):
return return
@tables.route("/tables/active") @tables.route("/tables/active")
def list_active_tables(): def list_active_tables():
gameService = GameService(get_db()) gameService = GameService(get_db())
tables = gameService.read_active() tables = gameService.read_active()
return jsonify([table.toJson() for table in tables]) return jsonify([table.toJson() for table in tables])
@tables.route("/tables/all") @tables.route("/tables/all")
def list_all_tables(): def list_all_tables():
gameService = GameService(get_db()) gameService = GameService(get_db())
@@ -53,6 +59,7 @@ def list_all_tables():
def list_inactive_tables(): def list_inactive_tables():
return return
@tables.route("/tables/<table_id>:start", methods=["POST"]) @tables.route("/tables/<table_id>:start", methods=["POST"])
def activate_table(table_id): def activate_table(table_id):
db = get_db() db = get_db()
@@ -81,6 +88,7 @@ def activate_table(table_id):
db.commit() db.commit()
return jsonify(table.toJson()) return jsonify(table.toJson())
@tables.route("/tables/<table_id>:stop", methods=["POST"]) @tables.route("/tables/<table_id>:stop", methods=["POST"])
def deactivate_table(table_id): def deactivate_table(table_id):
db = get_db() db = get_db()
@@ -108,4 +116,3 @@ def deactivate_table(table_id):
cursor.close() cursor.close()
db.commit() db.commit()
return jsonify(table.toJson()) return jsonify(table.toJson())