Add Teams (#45)
Co-authored-by: David Rodenkirchen <drodenkirchen@linetco.com> Reviewed-on: #45
This commit is contained in:
@@ -14,6 +14,7 @@ from src.ezgg_lan_manager.types.ConfigurationTypes import DatabaseConfiguration
|
||||
from src.ezgg_lan_manager.types.News import News
|
||||
from src.ezgg_lan_manager.types.Participant import Participant
|
||||
from src.ezgg_lan_manager.types.Seat import Seat
|
||||
from src.ezgg_lan_manager.types.Team import TeamStatus, Team
|
||||
from src.ezgg_lan_manager.types.Ticket import Ticket
|
||||
from src.ezgg_lan_manager.types.Tournament import Tournament
|
||||
from src.ezgg_lan_manager.types.TournamentBase import GameTitle, TournamentFormat, TournamentStatus, ParticipantType
|
||||
@@ -967,3 +968,208 @@ class DatabaseService:
|
||||
return await self.remove_participant_from_tournament(participant, tournament)
|
||||
except Exception as e:
|
||||
logger.warning(f"Error removing participant from tournament: {e}")
|
||||
|
||||
async def get_teams(self) -> list[Team]:
|
||||
async with self._connection_pool.acquire() as conn:
|
||||
async with conn.cursor(aiomysql.Cursor) as cursor:
|
||||
query = """
|
||||
SELECT
|
||||
t.id AS team_id,
|
||||
t.name AS team_name,
|
||||
t.abbreviation AS team_abbr,
|
||||
t.join_password,
|
||||
t.created_at AS team_created_at,
|
||||
|
||||
tm.status AS team_status,
|
||||
tm.joined_at AS member_joined_at,
|
||||
|
||||
u.*
|
||||
|
||||
FROM teams t
|
||||
|
||||
LEFT JOIN team_members tm
|
||||
ON t.id = tm.team_id
|
||||
|
||||
LEFT JOIN users u
|
||||
ON tm.user_id = u.user_id
|
||||
|
||||
ORDER BY
|
||||
t.id,
|
||||
CASE tm.status
|
||||
WHEN 'LEADER' THEN 1
|
||||
WHEN 'OFFICER' THEN 2
|
||||
WHEN 'MEMBER' THEN 3
|
||||
ELSE 4
|
||||
END,
|
||||
u.user_name;
|
||||
"""
|
||||
try:
|
||||
await cursor.execute(query)
|
||||
await conn.commit()
|
||||
except aiomysql.InterfaceError:
|
||||
pool_init_result = await self.init_db_pool()
|
||||
if not pool_init_result:
|
||||
raise NoDatabaseConnectionError
|
||||
return await self.get_teams()
|
||||
except Exception as e:
|
||||
logger.warning(f"Error getting teams: {e}")
|
||||
return []
|
||||
|
||||
current_team: Optional[Team] = None
|
||||
all_teams = []
|
||||
|
||||
for row in await cursor.fetchall():
|
||||
if row[5] is None: # Teams without single member are ignored
|
||||
continue
|
||||
if current_team is None:
|
||||
user = self._map_db_result_to_user(row[7:])
|
||||
current_team = Team(id=row[0], name=row[1], abbreviation=row[2], join_password=row[3], members={user: TeamStatus.from_str(row[5])})
|
||||
elif current_team.id == row[0]: # Still same team
|
||||
current_team.members[self._map_db_result_to_user(row[7:])] = TeamStatus.from_str(row[5])
|
||||
else:
|
||||
all_teams.append(current_team)
|
||||
user = self._map_db_result_to_user(row[7:])
|
||||
current_team = Team(id=row[0], name=row[1], abbreviation=row[2], join_password=row[3], members={user: TeamStatus.from_str(row[5])})
|
||||
|
||||
all_teams.append(current_team)
|
||||
|
||||
return all_teams
|
||||
|
||||
async def get_team_by_id(self, team_id: int) -> Optional[Team]:
|
||||
async with self._connection_pool.acquire() as conn:
|
||||
async with conn.cursor(aiomysql.Cursor) as cursor:
|
||||
query = """
|
||||
SELECT
|
||||
t.id AS team_id,
|
||||
t.name AS team_name,
|
||||
t.abbreviation AS team_abbr,
|
||||
t.join_password,
|
||||
t.created_at AS team_created_at,
|
||||
|
||||
tm.status AS team_status,
|
||||
tm.joined_at AS member_joined_at,
|
||||
|
||||
u.*
|
||||
|
||||
FROM teams t
|
||||
|
||||
LEFT JOIN team_members tm
|
||||
ON t.id = tm.team_id
|
||||
|
||||
LEFT JOIN users u
|
||||
ON tm.user_id = u.user_id
|
||||
|
||||
WHERE t.id = %s
|
||||
|
||||
ORDER BY
|
||||
t.id,
|
||||
CASE tm.status
|
||||
WHEN 'LEADER' THEN 1
|
||||
WHEN 'OFFICER' THEN 2
|
||||
WHEN 'MEMBER' THEN 3
|
||||
ELSE 4
|
||||
END,
|
||||
u.user_name;
|
||||
"""
|
||||
try:
|
||||
await cursor.execute(query, (team_id, ))
|
||||
await conn.commit()
|
||||
except aiomysql.InterfaceError:
|
||||
pool_init_result = await self.init_db_pool()
|
||||
if not pool_init_result:
|
||||
raise NoDatabaseConnectionError
|
||||
return await self.get_team_by_id(team_id)
|
||||
except Exception as e:
|
||||
logger.warning(f"Error getting team: {e}")
|
||||
return None
|
||||
|
||||
team: Optional[Team] = None
|
||||
|
||||
for row in await cursor.fetchall():
|
||||
if team is None:
|
||||
user = self._map_db_result_to_user(row[7:])
|
||||
team = Team(id=row[0], name=row[1], abbreviation=row[2], join_password=row[3], members={user: TeamStatus.from_str(row[5])})
|
||||
elif team.id == row[0]:
|
||||
team.members[self._map_db_result_to_user(row[7:])] = TeamStatus.from_str(row[5])
|
||||
|
||||
return team
|
||||
|
||||
async def create_team(self, team_name: str, team_abbr: str, join_password: str, leader: User) -> Team:
|
||||
async with self._connection_pool.acquire() as conn:
|
||||
async with conn.cursor(aiomysql.Cursor) as cursor:
|
||||
try:
|
||||
await cursor.execute(
|
||||
"INSERT INTO teams (name, abbreviation, join_password) "
|
||||
"VALUES (%s, %s, %s)", (team_name, team_abbr, join_password)
|
||||
)
|
||||
await conn.commit()
|
||||
team_id = cursor.lastrowid
|
||||
await cursor.execute(
|
||||
"INSERT INTO team_members (team_id, user_id, status) VALUES (%s, %s, %s)",
|
||||
(team_id, leader.user_id, TeamStatus.LEADER.name)
|
||||
)
|
||||
await conn.commit()
|
||||
return await self.get_team_by_id(team_id)
|
||||
|
||||
except aiomysql.InterfaceError:
|
||||
pool_init_result = await self.init_db_pool()
|
||||
if not pool_init_result:
|
||||
raise NoDatabaseConnectionError
|
||||
return await self.create_team(team_name, team_abbr, join_password)
|
||||
except aiomysql.IntegrityError as e:
|
||||
logger.warning(f"Aborted duplication entry: {e}")
|
||||
raise DuplicationError
|
||||
|
||||
async def update_team(self, team: Team) -> Team:
|
||||
async with self._connection_pool.acquire() as conn:
|
||||
async with conn.cursor(aiomysql.Cursor) as cursor:
|
||||
try:
|
||||
await cursor.execute(
|
||||
"UPDATE teams SET name = %s, abbreviation = %s, join_password = %s WHERE (id = %s)",
|
||||
(team.name, team.abbreviation, team.join_password, team.id)
|
||||
)
|
||||
await conn.commit()
|
||||
return await self.get_team_by_id(team.id)
|
||||
except aiomysql.InterfaceError:
|
||||
pool_init_result = await self.init_db_pool()
|
||||
if not pool_init_result:
|
||||
raise NoDatabaseConnectionError
|
||||
return await self.update_team(team)
|
||||
except aiomysql.IntegrityError as e:
|
||||
logger.warning(f"Aborted duplication entry: {e}")
|
||||
raise DuplicationError
|
||||
|
||||
|
||||
async def add_member_to_team(self, team: Team, user: User, status: TeamStatus = TeamStatus.MEMBER) -> None:
|
||||
async with self._connection_pool.acquire() as conn:
|
||||
async with conn.cursor(aiomysql.Cursor) as cursor:
|
||||
try:
|
||||
await cursor.execute(
|
||||
"INSERT INTO team_members (team_id, user_id, status) VALUES (%s, %s, %s)",
|
||||
(team.id, user.user_id, status.name)
|
||||
)
|
||||
await conn.commit()
|
||||
except aiomysql.InterfaceError:
|
||||
pool_init_result = await self.init_db_pool()
|
||||
if not pool_init_result:
|
||||
raise NoDatabaseConnectionError
|
||||
return await self.add_member_to_team(team, user, status)
|
||||
except aiomysql.IntegrityError as e:
|
||||
logger.warning(f"Failed to add member {user.user_name} to team {team.name}: {e}")
|
||||
raise DuplicationError
|
||||
|
||||
|
||||
async def remove_user_from_team(self, team: Team, user: User) -> None:
|
||||
async with self._connection_pool.acquire() as conn:
|
||||
async with conn.cursor(aiomysql.Cursor) as cursor:
|
||||
try:
|
||||
await cursor.execute(
|
||||
"DELETE FROM team_members WHERE team_id = %s AND user_id = %s",
|
||||
(team.id, user.user_id)
|
||||
)
|
||||
await conn.commit()
|
||||
except aiomysql.InterfaceError:
|
||||
pool_init_result = await self.init_db_pool()
|
||||
if not pool_init_result:
|
||||
raise NoDatabaseConnectionError
|
||||
return await self.remove_user_from_team(team, user)
|
||||
|
||||
Reference in New Issue
Block a user