Added logic to load clients on_ready

This commit is contained in:
2021-12-01 20:49:58 +01:00
parent d27964372d
commit 026dfe0dfb
4 changed files with 82 additions and 9 deletions

View File

@@ -22,6 +22,9 @@ class ClientRepositoryABC(ABC):
@abstractmethod
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
@abstractmethod
def add_client(self, client: Client): pass

View File

@@ -37,7 +37,7 @@ class Client(TableABC):
@property
def discord_id(self) -> int:
return self._discord_id
return self._discord_client_id
@property
def sent_message_count(self) -> int:
@@ -78,7 +78,7 @@ class Client(TableABC):
`CreatedAt` DATETIME(6),
`LastModifiedAt` DATETIME(6),
FOREIGN KEY (`ServerId`) REFERENCES Servers(`ServerId`),
PRIMARY KEY(`UserId`)
PRIMARY KEY(`ClientId`)
);
""")
@@ -102,10 +102,18 @@ class Client(TableABC):
WHERE `DiscordClientId` = {id};
""")
@staticmethod
def get_select_by_server_id_string(id: int) -> str:
return str(f"""
SELECT * FROM `Clients`
WHERE `ServerId` = {id};
""")
@property
def insert_string(self) -> str:
return str(f"""
INSERT INTO `Clients` (
`DiscordClientId`,
`SentMessageCount`,
`ReceivedMessageCount`,
`DeletedMessageCount`,
@@ -115,12 +123,13 @@ class Client(TableABC):
`CreatedAt`,
`LastModifiedAt`
) VALUES (
{self._discord_client_id},
{self._sent_message_count},
{self._received_message_count},
{self._deleted_message_count},
{self._received_message_count},
{self._moved_users_count},
{self._server.id},
{self._server.server_id},
'{self._created_at}',
'{self._modified_at}'
);

View File

@@ -83,6 +83,25 @@ class ClientRepositoryService(ClientRepositoryABC):
self._servers.get_server_by_id(result[7]),
id=result[0]
)
def find_client_by_server_id(self, discord_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}')