A-0.3 - Basismodul #36

Merged
edraft merged 48 commits from 0.3 into Alpha 2021-12-27 18:17:42 +01:00
3 changed files with 31 additions and 8 deletions
Showing only changes of commit 9d17070d02 - Show all commits

View File

@ -18,10 +18,13 @@ class UserRepositoryABC(ABC):
def get_user_by_id(self, id: int) -> User: pass def get_user_by_id(self, id: int) -> User: pass
@abstractmethod @abstractmethod
def get_user_by_discord_id(self, discord_id: int) -> User: pass def get_users_by_discord_id(self, discord_id: int) -> List[User]: pass
@abstractmethod @abstractmethod
def find_user_by_discord_id(self, discord_id: int) -> Optional[User]: pass def get_user_by_discord_id_and_server_id(self, discord_id: int, server_id: int) -> User: pass
@abstractmethod
def find_user_by_discord_id_and_server_id(self, discord_id: int, server_id: int) -> Optional[User]: pass
@abstractmethod @abstractmethod
def add_user(self, user: User): pass def add_user(self, user: User): pass

View File

@ -58,6 +58,14 @@ class User(TableABC):
WHERE `DiscordId` = {id}; WHERE `DiscordId` = {id};
""") """)
@staticmethod
def get_select_by_discord_id_and_server_id_string(dc_id: int, s_id: int) -> str:
return str(f"""
SELECT * FROM `Users`
WHERE `DiscordId` = {dc_id}
AND `ServerId` = {s_id};
""")
@property @property
def insert_string(self) -> str: def insert_string(self) -> str:
return str(f""" return str(f"""

View File

@ -43,10 +43,22 @@ class UserRepositoryService(UserRepositoryABC):
self._servers.get_server_by_id(result[3]), self._servers.get_server_by_id(result[3]),
id=result[0] id=result[0]
) )
def get_user_by_discord_id(self, discord_id: int) -> User: def get_users_by_discord_id(self, discord_id: int) -> List[User]:
users = List(User)
self._logger.trace(__name__, f'Send SQL command: {User.get_select_by_discord_id_string(discord_id)}') self._logger.trace(__name__, f'Send SQL command: {User.get_select_by_discord_id_string(discord_id)}')
result = self._context.select(User.get_select_by_discord_id_string(discord_id))[0] results = self._context.select(User.get_select_by_discord_id_string(discord_id))
for result in results:
users.append(User(
result[1],
result[2],
self._servers.get_server_by_id(result[3]),
id=result[0]
))
def get_user_by_discord_id_and_server_id(self, discord_id: int, server_id: int) -> User:
self._logger.trace(__name__, f'Send SQL command: {User.get_select_by_discord_id_and_server_id_string(discord_id, server_id)}')
result = self._context.select(User.get_select_by_discord_id_and_server_id_string(discord_id, server_id))[0]
return User( return User(
result[1], result[1],
@ -55,9 +67,9 @@ class UserRepositoryService(UserRepositoryABC):
id=result[0] id=result[0]
) )
def find_user_by_discord_id(self, discord_id: int) -> Optional[User]: def find_user_by_discord_id_and_server_id(self, discord_id: int, server_id: int) -> Optional[User]:
self._logger.trace(__name__, f'Send SQL command: {User.get_select_by_discord_id_string(discord_id)}') self._logger.trace(__name__, f'Send SQL command: {User.get_select_by_discord_id_and_server_id_string(discord_id, server_id)}')
result = self._context.select(User.get_select_by_discord_id_string(discord_id)) result = self._context.select(User.get_select_by_discord_id_and_server_id_string(discord_id, server_id))
if result is None or len(result) == 0: if result is None or len(result) == 0:
return None return None