From f03ee54703520cea349e2811cbf9cb29f31a25bc Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 8 Dec 2021 07:14:50 +0100 Subject: [PATCH] Added find function to client repo --- src/gismo_data/abc/client_repository_abc.py | 5 ++++- src/gismo_data/model/client.py | 8 ++++++++ .../service/client_repository_service.py | 19 +++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/gismo_data/abc/client_repository_abc.py b/src/gismo_data/abc/client_repository_abc.py index 6b1dcdb..2c84839 100644 --- a/src/gismo_data/abc/client_repository_abc.py +++ b/src/gismo_data/abc/client_repository_abc.py @@ -23,7 +23,10 @@ class ClientRepositoryABC(ABC): def find_client_by_discord_id(self, discord_id: int) -> Optional[Client]: pass @abstractmethod - def find_client_by_server_id(self, discord_id: int) -> Optional[Client]: pass + def find_client_by_server_id(self, server_id: int) -> Optional[Client]: pass + + @abstractmethod + def find_client_by_discord_id_and_server_id(self, discord_id: int, server_id: int) -> Optional[Client]: pass @abstractmethod def add_client(self, client: Client): pass diff --git a/src/gismo_data/model/client.py b/src/gismo_data/model/client.py index 61e6a6c..dd918a6 100644 --- a/src/gismo_data/model/client.py +++ b/src/gismo_data/model/client.py @@ -90,6 +90,14 @@ class Client(TableABC): WHERE `ServerId` = {id}; """) + @staticmethod + def get_select_by_discord_id_and_server_id_string(id: int, server_id: int) -> str: + return str(f""" + SELECT * FROM `Clients` + WHERE `DiscordClientId` = {id} + AND `ServerId` = {server_id}; + """) + @property def insert_string(self) -> str: return str(f""" diff --git a/src/gismo_data/service/client_repository_service.py b/src/gismo_data/service/client_repository_service.py index 8f0f5a1..1397ec9 100644 --- a/src/gismo_data/service/client_repository_service.py +++ b/src/gismo_data/service/client_repository_service.py @@ -102,6 +102,25 @@ class ClientRepositoryService(ClientRepositoryABC): self._servers.get_server_by_id(result[7]), id=result[0] ) + + def find_client_by_discord_id_and_server_id(self, discord_id: int, server_id: int) -> Optional[Client]: + self._logger.trace(__name__, f'Send SQL command: {Client.get_select_by_server_id_string(discord_id)}') + result = self._context.select(Client.get_select_by_server_id_string(discord_id)) + if result is None or len(result) == 0: + return None + + result = result[0] + + return Client( + result[1], + result[2], + result[3], + result[4], + result[5], + result[6], + self._servers.get_server_by_id(result[7]), + id=result[0] + ) def add_client(self, client: Client): self._logger.trace(__name__, f'Send SQL command: {client.insert_string}')