summaryrefslogtreecommitdiff
path: root/app/controller.py
diff options
context:
space:
mode:
Diffstat (limited to 'app/controller.py')
-rw-r--r--app/controller.py36
1 files changed, 18 insertions, 18 deletions
diff --git a/app/controller.py b/app/controller.py
index 1c2c5bf..3bfce38 100644
--- a/app/controller.py
+++ b/app/controller.py
@@ -22,7 +22,6 @@ from app.utilities import (
class Courts:
-
def __init__(self):
with app.app_context():
courts = db.session.query(Court).all()
@@ -31,14 +30,17 @@ class Courts:
db.session.add(Court())
db.session.commit()
- def toggle(self, id: int) -> bool:
+ def get(self, id: int) -> Court:
court = db.session.get(Court, id)
if court is None:
- raise ValueError(f"Court {id} doesn't exist.")
- new_status = not court.status
- court.status = new_status
+ raise ValueError(f"Court not found: {id}")
+ return court
+
+ def toggle(self, id: int) -> Court:
+ court = self.get(id)
+ court.status = not court.status
db.session.commit()
- return new_status
+ return court
def active(self):
return db.session.query(Court).where(Court.status).all()
@@ -51,7 +53,6 @@ class Courts:
class Players:
-
def __init__(self) -> None:
with app.app_context():
if len(self.regulars()) == 0:
@@ -64,16 +65,14 @@ class Players:
db.session.add(Player(**args)) # type: ignore
db.session.commit()
- def get(self, id: int) -> bool:
+ def get(self, id: int) -> Player:
player = db.session.get(Player, id)
if player is None:
- raise ValueError(f"User {id} doesn't exist.")
- return player.status
+ raise ValueError(f"Player not found: {id}")
+ return player
def toggle(self, id: int) -> Player:
- player = db.session.get(Player, id)
- if player is None:
- raise ValueError(f"User {id} doesn't exist.")
+ player = self.get(id)
player.status = not player.status
db.session.commit()
return player
@@ -117,8 +116,6 @@ class Session:
levels: int = 10
teams: bool = False
proposal: list[DisplayGame] = []
- courts: Courts = Courts()
- players: Players = Players()
def _today(self):
games = (
@@ -128,12 +125,15 @@ class Session:
def __init__(self):
with app.app_context():
+ db.create_all()
+ self.courts = Courts()
+ self.players = Players()
if len(self._today()) == 0:
query = select(Player).where(Player.first_name == "---")
player = db.session.scalars(query).first()
if player is None:
- raise ValueError(f"Database isn't initialized.")
- player_data = {f"player_{i+1}": player.id for i in range(4)}
+ raise ValueError("Database isn't initialized.")
+ player_data = {f"player_{i + 1}": player.id for i in range(4)}
for court in self.courts.all():
args = {"round_id": 0, "court_id": court.id} | player_data
db.session.add(Game(**args))
@@ -143,7 +143,7 @@ class Session:
games = self._today()
round_id = max([game.round_id for game in games])
if round_id is None:
- raise ValueError(f"Database isn't initialized")
+ raise ValueError("Database isn't initialized")
return round_id
def current_round(self) -> list[DisplayGame]: