Add Teams (#45)
Co-authored-by: David Rodenkirchen <drodenkirchen@linetco.com> Reviewed-on: #45
This commit was merged in pull request #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)
|
||||
|
||||
@@ -0,0 +1,134 @@
|
||||
from string import ascii_letters, digits
|
||||
from typing import Optional
|
||||
|
||||
from src.ezgg_lan_manager.services.DatabaseService import DatabaseService, DuplicationError
|
||||
from src.ezgg_lan_manager.types.Team import TeamStatus, Team
|
||||
from src.ezgg_lan_manager.types.User import User
|
||||
|
||||
|
||||
class NameNotAllowedError(Exception):
|
||||
def __init__(self, disallowed_char: str) -> None:
|
||||
self.disallowed_char = disallowed_char
|
||||
|
||||
|
||||
class AlreadyMemberError(Exception):
|
||||
def __init__(self) -> None:
|
||||
pass
|
||||
|
||||
|
||||
class NotMemberError(Exception):
|
||||
def __init__(self) -> None:
|
||||
pass
|
||||
|
||||
|
||||
class TeamLeadRemovalError(Exception):
|
||||
def __init__(self) -> None:
|
||||
pass
|
||||
|
||||
|
||||
class TeamNameTooLongError(Exception):
|
||||
def __init__(self) -> None:
|
||||
pass
|
||||
|
||||
|
||||
class TeamNameAlreadyTaken(Exception):
|
||||
def __init__(self) -> None:
|
||||
pass
|
||||
|
||||
|
||||
class TeamAbbrInvalidError(Exception):
|
||||
def __init__(self) -> None:
|
||||
pass
|
||||
|
||||
|
||||
class TeamService:
|
||||
ALLOWED_TEAM_NAME_SYMBOLS = ascii_letters + digits + "!#$%&*+,-./:;<=>?[]^_{|}~ "
|
||||
MAX_TEAM_NAME_LENGTH = 24
|
||||
MAX_TEAM_ABBR_LENGTH = 8
|
||||
|
||||
def __init__(self, db_service: DatabaseService) -> None:
|
||||
self._db_service = db_service
|
||||
|
||||
async def get_all_teams(self) -> list[Team]:
|
||||
return await self._db_service.get_teams()
|
||||
|
||||
async def get_team_by_id(self, team_id: int) -> Optional[Team]:
|
||||
return await self._db_service.get_team_by_id(team_id)
|
||||
|
||||
async def get_teams_for_user_by_id(self, user_id: int) -> list[Team]:
|
||||
all_teams = await self.get_all_teams()
|
||||
user_teams = []
|
||||
for team in all_teams:
|
||||
if user_id in [u.user_id for u in team.members.keys()]:
|
||||
user_teams.append(team)
|
||||
return user_teams
|
||||
|
||||
async def create_team(self, team_name: str, team_abbr: str, join_password: str, leader: User) -> Team:
|
||||
disallowed_char = self._check_for_disallowed_char(team_name)
|
||||
if disallowed_char:
|
||||
raise NameNotAllowedError(disallowed_char)
|
||||
disallowed_char = self._check_for_disallowed_char(team_abbr)
|
||||
if disallowed_char:
|
||||
raise NameNotAllowedError(disallowed_char)
|
||||
|
||||
if not team_name or len(team_name) > self.MAX_TEAM_NAME_LENGTH:
|
||||
raise TeamNameTooLongError()
|
||||
|
||||
if not team_abbr or len(team_abbr) > self.MAX_TEAM_ABBR_LENGTH:
|
||||
raise TeamAbbrInvalidError()
|
||||
|
||||
try:
|
||||
created_team = await self._db_service.create_team(team_name, team_abbr, join_password, leader)
|
||||
except DuplicationError:
|
||||
raise TeamNameAlreadyTaken
|
||||
return created_team
|
||||
|
||||
async def update_team(self, team: Team) -> Team:
|
||||
"""
|
||||
Updates the team EXCLUDING adding and removing members. This is to be done via add_member_to_team and remove_member_from_team
|
||||
:param team: New instance of Team that is to be updated
|
||||
:return: The modified Team instance
|
||||
"""
|
||||
disallowed_char = self._check_for_disallowed_char(team.name)
|
||||
if disallowed_char:
|
||||
raise NameNotAllowedError(disallowed_char)
|
||||
disallowed_char = self._check_for_disallowed_char(team.abbreviation)
|
||||
if disallowed_char:
|
||||
raise NameNotAllowedError(disallowed_char)
|
||||
|
||||
if not team.name or len(team.name) > self.MAX_TEAM_NAME_LENGTH:
|
||||
raise TeamNameTooLongError()
|
||||
|
||||
if not team.abbreviation or len(team.abbreviation) > self.MAX_TEAM_ABBR_LENGTH:
|
||||
raise TeamAbbrInvalidError()
|
||||
|
||||
return await self._db_service.update_team(team)
|
||||
|
||||
async def add_member_to_team(self, team: Team, user: User, status: TeamStatus = TeamStatus.MEMBER) -> Team:
|
||||
if user in team.members:
|
||||
raise AlreadyMemberError()
|
||||
|
||||
await self._db_service.add_member_to_team(team, user, status)
|
||||
return await self.get_team_by_id(team.id)
|
||||
|
||||
async def remove_member_from_team(self, team: Team, user: User) -> Team:
|
||||
if user not in team.members:
|
||||
raise NotMemberError()
|
||||
|
||||
if team.members[user] == TeamStatus.LEADER:
|
||||
raise TeamLeadRemovalError()
|
||||
|
||||
await self._db_service.remove_user_from_team(team, user)
|
||||
return await self.get_team_by_id(team.id)
|
||||
|
||||
async def is_join_password_valid(self, team_id: int, join_password: str) -> bool:
|
||||
team = await self.get_team_by_id(team_id)
|
||||
if not team:
|
||||
return False
|
||||
return team.join_password == join_password
|
||||
|
||||
def _check_for_disallowed_char(self, name: str) -> Optional[str]:
|
||||
for c in name:
|
||||
if c not in self.ALLOWED_TEAM_NAME_SYMBOLS:
|
||||
return c
|
||||
return None
|
||||
Reference in New Issue
Block a user