Added find function to client repo

This commit is contained in:
Sven Heidemann 2021-12-08 07:14:50 +01:00
parent 9bae6c3c13
commit f03ee54703
3 changed files with 31 additions and 1 deletions

View File

@ -23,7 +23,10 @@ class ClientRepositoryABC(ABC):
def find_client_by_discord_id(self, discord_id: int) -> Optional[Client]: pass def find_client_by_discord_id(self, discord_id: int) -> Optional[Client]: pass
@abstractmethod @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 @abstractmethod
def add_client(self, client: Client): pass def add_client(self, client: Client): pass

View File

@ -90,6 +90,14 @@ class Client(TableABC):
WHERE `ServerId` = {id}; 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 @property
def insert_string(self) -> str: def insert_string(self) -> str:
return str(f""" return str(f"""

View File

@ -102,6 +102,25 @@ class ClientRepositoryService(ClientRepositoryABC):
self._servers.get_server_by_id(result[7]), self._servers.get_server_by_id(result[7]),
id=result[0] 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): def add_client(self, client: Client):
self._logger.trace(__name__, f'Send SQL command: {client.insert_string}') self._logger.trace(__name__, f'Send SQL command: {client.insert_string}')