0.3-#70 - Im Web Interface Server auswählen - (#72) #73
@ -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:
|
||||
|
Loading…
Reference in New Issue
Block a user