From c63e9b19bd9e20109be8838bbde9f0389d3054b5 Mon Sep 17 00:00:00 2001 From: iamBadgers Date: Thu, 9 Apr 2026 23:17:42 -0700 Subject: [PATCH] Set up database creation on start. --- src/database.py | 27 +++++++++++++++++++++++++++ src/game_tables.py | 1 + src/main.py | 34 +++++++++++++--------------------- src/tables.py | 9 ++++++++- 4 files changed, 49 insertions(+), 22 deletions(-) diff --git a/src/database.py b/src/database.py index bc2aa76..9dcabbf 100644 --- a/src/database.py +++ b/src/database.py @@ -9,6 +9,33 @@ def get_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: connection: Connection curosr: Cursor diff --git a/src/game_tables.py b/src/game_tables.py index a1feaca..637d286 100644 --- a/src/game_tables.py +++ b/src/game_tables.py @@ -2,6 +2,7 @@ from sqlite3 import Connection, Cursor from database import SmartCursor import random + class GameTable: _game_table_id: int _game_table_name: str diff --git a/src/main.py b/src/main.py index 14844c6..c43d52d 100644 --- a/src/main.py +++ b/src/main.py @@ -1,34 +1,26 @@ from flask import Flask, g, jsonify, request import sqlite3 import container_managment -from database import SmartCursor +from database import SmartCursor, get_db, init_db import tables -app = Flask(__name__) +def create_app(): + app = Flask(__name__) -def convertTable(tupple): - return { - "id": tupple[0], - "table_name": tupple[1], - "table_link": tupple[2], - "active": tupple[3] != 0, - } + @app.teardown_appcontext + def close_connection(exception): + db = getattr(g, "_database", None) + if db is not None: + db.close() -def get_db(): - db = getattr(g, "_database", None) - if db is None: - db = sqlite3.connect("/data/tables.db") - return db + app.register_blueprint(tables.tables, url_prefix="/api") + with app.app_context(): + init_db(get_db()) -@app.teardown_appcontext -def close_connection(exception): - db = getattr(g, "_database", None) - if db is not None: - db.close() + return app -app.register_blueprint(tables.tables, url_prefix="/api") if __name__ == "__main__": - app.run(debug=True) + create_app.run(debug=True) diff --git a/src/tables.py b/src/tables.py index 882a731..4c9db78 100644 --- a/src/tables.py +++ b/src/tables.py @@ -7,6 +7,7 @@ from database import get_db, SmartCursor tables = Blueprint("tables_api", __name__) + @tables.route("/tables", methods=["POST"]) def create_table(): db = get_db() @@ -22,26 +23,31 @@ def create_table(): db.commit() return jsonify({}), 200 + @tables.route("/tables/") def get_table(table_id): gameService = GameService(get_db()) table = gameService.read_by_id(table_id) return jsonify(table.toJson()) + @tables.route("/tables/", methods=["POST"]) def update_table(table_id): return + @tables.route("tables/", methods=["DELETE"]) def delete_table(table_id): return + @tables.route("/tables/active") def list_active_tables(): gameService = GameService(get_db()) tables = gameService.read_active() return jsonify([table.toJson() for table in tables]) + @tables.route("/tables/all") def list_all_tables(): gameService = GameService(get_db()) @@ -53,6 +59,7 @@ def list_all_tables(): def list_inactive_tables(): return + @tables.route("/tables/:start", methods=["POST"]) def activate_table(table_id): db = get_db() @@ -81,6 +88,7 @@ def activate_table(table_id): db.commit() return jsonify(table.toJson()) + @tables.route("/tables/:stop", methods=["POST"]) def deactivate_table(table_id): db = get_db() @@ -108,4 +116,3 @@ def deactivate_table(table_id): cursor.close() db.commit() return jsonify(table.toJson()) -