Set up database creation on start.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
34
src/main.py
34
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,
|
||||
}
|
||||
|
||||
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):
|
||||
@app.teardown_appcontext
|
||||
def close_connection(exception):
|
||||
db = getattr(g, "_database", None)
|
||||
if db is not None:
|
||||
db.close()
|
||||
|
||||
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__":
|
||||
app.run(debug=True)
|
||||
create_app.run(debug=True)
|
||||
|
||||
@@ -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/<table_id>")
|
||||
def get_table(table_id):
|
||||
gameService = GameService(get_db())
|
||||
table = gameService.read_by_id(table_id)
|
||||
return jsonify(table.toJson())
|
||||
|
||||
|
||||
@tables.route("/tables/<int:table_id>", methods=["POST"])
|
||||
def update_table(table_id):
|
||||
return
|
||||
|
||||
|
||||
@tables.route("tables/<table_id>", 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/<table_id>: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/<table_id>: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())
|
||||
|
||||
|
||||
Reference in New Issue
Block a user