From c094a3efae3a05ff82e4fdd45cac4d7db5a356ff Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 18 Oct 2022 12:41:42 +0200 Subject: [PATCH] Improved get server logic #72 --- .../src/bot_api/service/discord_service.py | 51 ++++++++++--------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/kdb-bot/src/bot_api/service/discord_service.py b/kdb-bot/src/bot_api/service/discord_service.py index 7266e1bd4a..304e340a95 100644 --- a/kdb-bot/src/bot_api/service/discord_service.py +++ b/kdb-bot/src/bot_api/service/discord_service.py @@ -15,6 +15,7 @@ from bot_api.transformer.server_transformer import ServerTransformer from bot_data.abc.server_repository_abc import ServerRepositoryABC from bot_data.abc.user_repository_abc import UserRepositoryABC from bot_data.model.auth_role_enum import AuthRoleEnum +from bot_data.model.server import Server class DiscordService: @@ -31,14 +32,26 @@ class DiscordService: self._auth = auth self._users = users + def _to_dto(self, x: Server) -> Optional[ServerDTO]: + guild = self._bot.get_guild(x.discord_server_id) + if guild is None: + return ServerTransformer.to_dto( + x, + '', + 0, + None + ) + + return ServerTransformer.to_dto( + x, + guild.name, + guild.member_count, + guild.icon + ) + async def get_all_servers(self) -> List[ServerDTO]: - servers = self._servers.get_servers() - return servers.select(lambda x: ServerTransformer.to_dto( - x, - self._bot.get_guild(x.discord_server_id).name, - self._bot.get_guild(x.discord_server_id).member_count, - self._bot.get_guild(x.discord_server_id).icon - )) + servers = List(ServerDTO, self._servers.get_servers()) + return servers.select(self._to_dto).where(lambda x: x.name != '') async def get_all_servers_by_user(self) -> List[ServerDTO]: token = self._auth.get_decoded_token_from_request() @@ -53,12 +66,8 @@ class DiscordService: user_from_db = self._users.find_user_by_id(0 if user.user_id is None else user.user_id) servers = self._servers.get_servers().where(lambda x: user_from_db is not None and x.server_id == user_from_db.server.server_id) - return servers.select(lambda x: ServerTransformer.to_dto( - x, - self._bot.get_guild(x.discord_server_id).name, - self._bot.get_guild(x.discord_server_id).member_count, - self._bot.get_guild(x.discord_server_id).icon - )) + servers = List(ServerDTO, servers) + return servers.select(self._to_dto).where(lambda x: x.name != '') async def get_filtered_servers_async(self, criteria: ServerSelectCriteria) -> ServerFilteredResultDTO: token = self._auth.get_decoded_token_from_request() @@ -67,27 +76,21 @@ class DiscordService: role = AuthRoleEnum(token['role']) filtered_result = self._servers.get_filtered_servers(criteria) - servers = filtered_result.result + # filter out servers, where the user not exists if role != AuthRoleEnum.admin: user = await self._auth.find_auth_user_by_email_async(token['email']) user_from_db = self._users.find_user_by_id(0 if user.user_id is None else user.user_id) - servers = servers.where(lambda x: user_from_db is not None and x.server_id == user_from_db.server.server_id) + filtered_result.result = filtered_result.result.where(lambda x: user_from_db is not None and x.server_id == user_from_db.server.server_id) - result = servers.select( - lambda x: ServerTransformer.to_dto( - x, - self._bot.get_guild(x.discord_server_id).name, - self._bot.get_guild(x.discord_server_id).member_count, - self._bot.get_guild(x.discord_server_id).icon - ) - ) + servers: List = filtered_result.result.select(self._to_dto).where(lambda x: x.name != '') + result = List(ServerDTO, servers) if criteria.name is not None and criteria.name != '': result = result.where(lambda x: criteria.name.lower() in x.name.lower() or x.name.lower() == criteria.name.lower()) return ServerFilteredResultDTO( List(ServerDTO, result), - filtered_result.total_count + servers.count() ) async def get_server_by_id_async(self, id: int) -> ServerDTO: