Improved get server logic #72
This commit is contained in:
parent
2a97438417
commit
c094a3efae
@ -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.server_repository_abc import ServerRepositoryABC
|
||||||
from bot_data.abc.user_repository_abc import UserRepositoryABC
|
from bot_data.abc.user_repository_abc import UserRepositoryABC
|
||||||
from bot_data.model.auth_role_enum import AuthRoleEnum
|
from bot_data.model.auth_role_enum import AuthRoleEnum
|
||||||
|
from bot_data.model.server import Server
|
||||||
|
|
||||||
|
|
||||||
class DiscordService:
|
class DiscordService:
|
||||||
@ -31,14 +32,26 @@ class DiscordService:
|
|||||||
self._auth = auth
|
self._auth = auth
|
||||||
self._users = users
|
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]:
|
async def get_all_servers(self) -> List[ServerDTO]:
|
||||||
servers = self._servers.get_servers()
|
servers = List(ServerDTO, self._servers.get_servers())
|
||||||
return servers.select(lambda x: ServerTransformer.to_dto(
|
return servers.select(self._to_dto).where(lambda x: x.name != '')
|
||||||
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
|
|
||||||
))
|
|
||||||
|
|
||||||
async def get_all_servers_by_user(self) -> List[ServerDTO]:
|
async def get_all_servers_by_user(self) -> List[ServerDTO]:
|
||||||
token = self._auth.get_decoded_token_from_request()
|
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)
|
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)
|
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(
|
servers = List(ServerDTO, servers)
|
||||||
x,
|
return servers.select(self._to_dto).where(lambda x: x.name != '')
|
||||||
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
|
|
||||||
))
|
|
||||||
|
|
||||||
async def get_filtered_servers_async(self, criteria: ServerSelectCriteria) -> ServerFilteredResultDTO:
|
async def get_filtered_servers_async(self, criteria: ServerSelectCriteria) -> ServerFilteredResultDTO:
|
||||||
token = self._auth.get_decoded_token_from_request()
|
token = self._auth.get_decoded_token_from_request()
|
||||||
@ -67,27 +76,21 @@ class DiscordService:
|
|||||||
|
|
||||||
role = AuthRoleEnum(token['role'])
|
role = AuthRoleEnum(token['role'])
|
||||||
filtered_result = self._servers.get_filtered_servers(criteria)
|
filtered_result = self._servers.get_filtered_servers(criteria)
|
||||||
servers = filtered_result.result
|
# filter out servers, where the user not exists
|
||||||
if role != AuthRoleEnum.admin:
|
if role != AuthRoleEnum.admin:
|
||||||
user = await self._auth.find_auth_user_by_email_async(token['email'])
|
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)
|
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(
|
servers: List = filtered_result.result.select(self._to_dto).where(lambda x: x.name != '')
|
||||||
lambda x: ServerTransformer.to_dto(
|
result = List(ServerDTO, servers)
|
||||||
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
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
if criteria.name is not None and criteria.name != '':
|
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())
|
result = result.where(lambda x: criteria.name.lower() in x.name.lower() or x.name.lower() == criteria.name.lower())
|
||||||
|
|
||||||
return ServerFilteredResultDTO(
|
return ServerFilteredResultDTO(
|
||||||
List(ServerDTO, result),
|
List(ServerDTO, result),
|
||||||
filtered_result.total_count
|
servers.count()
|
||||||
)
|
)
|
||||||
|
|
||||||
async def get_server_by_id_async(self, id: int) -> ServerDTO:
|
async def get_server_by_id_async(self, id: int) -> ServerDTO:
|
||||||
|
Loading…
Reference in New Issue
Block a user