Smart curosr for Game Tables
This commit is contained in:
@@ -17,9 +17,10 @@ class SmartCursor:
|
|||||||
def __init__(self, cursor: Cursor = None, connection: Connectoin = None):
|
def __init__(self, cursor: Cursor = None, connection: Connectoin = None):
|
||||||
self.cursor = cursor
|
self.cursor = cursor
|
||||||
self.connection = connection
|
self.connection = connection
|
||||||
|
self.autoClose = False
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
if self.cursor != None:
|
if self.cursor == None:
|
||||||
self.cursor = self.connection.cursor()
|
self.cursor = self.connection.cursor()
|
||||||
self.autoClose = True
|
self.autoClose = True
|
||||||
return self.cursor
|
return self.cursor
|
||||||
|
|||||||
@@ -1,3 +1,7 @@
|
|||||||
|
from sqlite3 import Connection, Cursor
|
||||||
|
from database import SmartCursor
|
||||||
|
|
||||||
|
|
||||||
class GameTable:
|
class GameTable:
|
||||||
_game_table_id: int
|
_game_table_id: int
|
||||||
_game_table_name: str
|
_game_table_name: str
|
||||||
@@ -112,8 +116,15 @@ class GameTable:
|
|||||||
self._docker_id = docker_id
|
self._docker_id = docker_id
|
||||||
|
|
||||||
|
|
||||||
def read_by_id(game_table_id, cursor):
|
class GameService:
|
||||||
rows = cursor.execute(
|
connection: Connection
|
||||||
|
|
||||||
|
def __init__(self, connection: Connection):
|
||||||
|
self.connection = connection
|
||||||
|
|
||||||
|
def read_by_id(self, game_table_id, cursor=None):
|
||||||
|
with SmartCursor(cursor, self.connection) as smartCursor:
|
||||||
|
rows = smartCursor.execute(
|
||||||
"""SELECT game_table_id, game_table_name, game_table_link, active, docker_id
|
"""SELECT game_table_id, game_table_name, game_table_link, active, docker_id
|
||||||
FROM game_tables
|
FROM game_tables
|
||||||
WHERE game_table_id = ?""",
|
WHERE game_table_id = ?""",
|
||||||
@@ -125,20 +136,22 @@ def read_by_id(game_table_id, cursor):
|
|||||||
|
|
||||||
return GameTable(rows[0], rows[1], rows[2], rows[3], rows[4], True, True)
|
return GameTable(rows[0], rows[1], rows[2], rows[3], rows[4], True, True)
|
||||||
|
|
||||||
|
def read_all(self, cursor=None):
|
||||||
def read_all(cursor):
|
with SmartCursor(cursor, self.connection) as smartCursor:
|
||||||
rows = cursor.execute(
|
rows = smartCursor.execute(
|
||||||
"SELECT game_table_id, game_table_name, game_table_link, active, docker_id FROM game_tables"
|
"SELECT game_table_id, game_table_name, game_table_link, active, docker_id FROM game_tables"
|
||||||
).fetchall()
|
).fetchall()
|
||||||
return [
|
return [
|
||||||
GameTable(row[0], row[1], row[2], row[3], row[4], True, True) for row in rows
|
GameTable(row[0], row[1], row[2], row[3], row[4], True, True)
|
||||||
|
for row in rows
|
||||||
]
|
]
|
||||||
|
|
||||||
|
def read_active(self, cursor=None):
|
||||||
def read_active(cursor):
|
with SmartCursor(cursor, self.connection) as smartCursor:
|
||||||
rows = cursor.execute(
|
rows = smartCursor.execute(
|
||||||
"SELECT game_table_id, game_table_name, game_table_link, active, docker_id FROM game_tables WHERE active != 0"
|
"SELECT game_table_id, game_table_name, game_table_link, active, docker_id FROM game_tables WHERE active != 0"
|
||||||
).fetchall()
|
).fetchall()
|
||||||
return [
|
return [
|
||||||
GameTable(row[0], row[1], row[2], row[3], row[4], True, True) for row in rows
|
GameTable(row[0], row[1], row[2], row[3], row[4], True, True)
|
||||||
|
for row in rows
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -86,7 +86,7 @@ class KeyService:
|
|||||||
return [KeyTable(row[0], row[1]) for row in rows]
|
return [KeyTable(row[0], row[1]) for row in rows]
|
||||||
|
|
||||||
def get_free_keys(self, cursor=None) -> KeyTable:
|
def get_free_keys(self, cursor=None) -> KeyTable:
|
||||||
with SmartCursor(cursor, connection) as smartCursor:
|
with SmartCursor(cursor, self.connection) as smartCursor:
|
||||||
smartCursor.execute("""SELECT key, game_table_id
|
smartCursor.execute("""SELECT key, game_table_id
|
||||||
FROM game_keys
|
FROM game_keys
|
||||||
WHERE game_table_id IS NULL""")
|
WHERE game_table_id IS NULL""")
|
||||||
|
|||||||
29
src/main.py
29
src/main.py
@@ -1,6 +1,6 @@
|
|||||||
from flask import Flask, g, jsonify, request
|
from flask import Flask, g, jsonify, request
|
||||||
import sqlite3
|
import sqlite3
|
||||||
import game_tables
|
from game_tables import GameService, GameTable
|
||||||
from key_tables import KeyService, KeyTable
|
from key_tables import KeyService, KeyTable
|
||||||
import container_managment
|
import container_managment
|
||||||
|
|
||||||
@@ -36,31 +36,22 @@ def close_connection(exception):
|
|||||||
|
|
||||||
@app.route("/api/active_tables")
|
@app.route("/api/active_tables")
|
||||||
def get_active_tables():
|
def get_active_tables():
|
||||||
db = get_db()
|
gameService = GameService(get_db())
|
||||||
cursor = db.cursor()
|
tables = gameService.read_active()
|
||||||
tables = game_tables.read_active(cursor)
|
|
||||||
cursor.close()
|
|
||||||
|
|
||||||
return jsonify([table.toJson() for table in tables])
|
return jsonify([table.toJson() for table in tables])
|
||||||
|
|
||||||
|
|
||||||
@app.route("/api/tables")
|
@app.route("/api/tables")
|
||||||
def get_tables():
|
def get_tables():
|
||||||
db = get_db()
|
gameService = GameService(get_db())
|
||||||
cursor = db.cursor()
|
tables = gameService.read_all()
|
||||||
tables = game_tables.read_all(cursor)
|
|
||||||
cursor.close()
|
|
||||||
|
|
||||||
return jsonify([table.toJson() for table in tables])
|
return jsonify([table.toJson() for table in tables])
|
||||||
|
|
||||||
|
|
||||||
@app.route("/api/tables/<table_id>")
|
@app.route("/api/tables/<table_id>")
|
||||||
def get_table(table_id):
|
def get_table(table_id):
|
||||||
db = get_db()
|
gameService = GameService(get_db())
|
||||||
cursor = db.cursor()
|
table = gameService.read_by_id(table_id)
|
||||||
table = game_tables.read_by_id(table_id, cursor)
|
|
||||||
cursor.close()
|
|
||||||
|
|
||||||
return jsonify(table.toJson())
|
return jsonify(table.toJson())
|
||||||
|
|
||||||
|
|
||||||
@@ -69,9 +60,10 @@ def activate_table(table_id):
|
|||||||
db = get_db()
|
db = get_db()
|
||||||
cursor = db.cursor()
|
cursor = db.cursor()
|
||||||
keyService = KeyService(db)
|
keyService = KeyService(db)
|
||||||
|
gameService = GameService(db)
|
||||||
|
|
||||||
keys = keyService.get_free_keys(cursor)
|
keys = keyService.get_free_keys(cursor)
|
||||||
table = game_tables.read_by_id(table_id, cursor)
|
table = gameService.read_by_id(table_id, cursor)
|
||||||
|
|
||||||
if table == None:
|
if table == None:
|
||||||
return "No such table", 404
|
return "No such table", 404
|
||||||
@@ -98,8 +90,9 @@ def deactivate_table(table_id):
|
|||||||
db = get_db()
|
db = get_db()
|
||||||
cursor = db.cursor()
|
cursor = db.cursor()
|
||||||
keyService = KeyService(db)
|
keyService = KeyService(db)
|
||||||
|
gameService = GameService(db)
|
||||||
|
|
||||||
table = game_tables.read_by_id(table_id, cursor)
|
table = gameService.read_by_id(table_id, cursor)
|
||||||
key = keyService.get_key_for_table(table_id, cursor)
|
key = keyService.get_key_for_table(table_id, cursor)
|
||||||
|
|
||||||
if table == None:
|
if table == None:
|
||||||
|
|||||||
Reference in New Issue
Block a user