From 9d17070d0261ebfd1aaf6dc708c604e4d66105d8 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 8 Dec 2021 17:40:50 +0100 Subject: [PATCH] Fixed user repo error --- src/gismo_data/abc/user_repository_abc.py | 7 ++++-- src/gismo_data/model/user.py | 8 +++++++ .../service/user_repository_service.py | 24 ++++++++++++++----- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/gismo_data/abc/user_repository_abc.py b/src/gismo_data/abc/user_repository_abc.py index c198753..5a647f0 100644 --- a/src/gismo_data/abc/user_repository_abc.py +++ b/src/gismo_data/abc/user_repository_abc.py @@ -18,10 +18,13 @@ class UserRepositoryABC(ABC): def get_user_by_id(self, id: int) -> User: pass @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 - 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 def add_user(self, user: User): pass diff --git a/src/gismo_data/model/user.py b/src/gismo_data/model/user.py index f133f1a..5597c07 100644 --- a/src/gismo_data/model/user.py +++ b/src/gismo_data/model/user.py @@ -58,6 +58,14 @@ class User(TableABC): 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 def insert_string(self) -> str: return str(f""" diff --git a/src/gismo_data/service/user_repository_service.py b/src/gismo_data/service/user_repository_service.py index ba857be..7bb29ec 100644 --- a/src/gismo_data/service/user_repository_service.py +++ b/src/gismo_data/service/user_repository_service.py @@ -43,10 +43,22 @@ class UserRepositoryService(UserRepositoryABC): self._servers.get_server_by_id(result[3]), 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)}') - 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( result[1], @@ -55,9 +67,9 @@ class UserRepositoryService(UserRepositoryABC): id=result[0] ) - def find_user_by_discord_id(self, discord_id: int) -> Optional[User]: - 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)) + 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_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)) if result is None or len(result) == 0: return None