Fixed user repo error

This commit is contained in:
Sven Heidemann 2021-12-08 17:40:50 +01:00
parent f417eca644
commit 9d17070d02
3 changed files with 31 additions and 8 deletions

View File

@ -18,10 +18,13 @@ class UserRepositoryABC(ABC):
def get_user_by_id(self, id: int) -> User: pass def get_user_by_id(self, id: int) -> User: pass
@abstractmethod @abstractmethod
def get_user_by_discord_id(self, discord_id: int) -> User: pass def get_users_by_discord_id(self, discord_id: int) -> List[User]: pass
@abstractmethod @abstractmethod
def find_user_by_discord_id(self, discord_id: int) -> Optional[User]: pass def get_user_by_discord_id_and_server_id(self, discord_id: int, server_id: int) -> User: pass
@abstractmethod
def find_user_by_discord_id_and_server_id(self, discord_id: int, server_id: int) -> Optional[User]: pass
@abstractmethod @abstractmethod
def add_user(self, user: User): pass def add_user(self, user: User): pass

View File

@ -58,6 +58,14 @@ class User(TableABC):
WHERE `DiscordId` = {id}; WHERE `DiscordId` = {id};
""") """)
@staticmethod
def get_select_by_discord_id_and_server_id_string(dc_id: int, s_id: int) -> str:
return str(f"""
SELECT * FROM `Users`
WHERE `DiscordId` = {dc_id}
AND `ServerId` = {s_id};
""")
@property @property
def insert_string(self) -> str: def insert_string(self) -> str:
return str(f""" return str(f"""

View File

@ -43,10 +43,22 @@ class UserRepositoryService(UserRepositoryABC):
self._servers.get_server_by_id(result[3]), self._servers.get_server_by_id(result[3]),
id=result[0] id=result[0]
) )
def get_user_by_discord_id(self, discord_id: int) -> User: def get_users_by_discord_id(self, discord_id: int) -> List[User]:
users = List(User)
self._logger.trace(__name__, f'Send SQL command: {User.get_select_by_discord_id_string(discord_id)}') self._logger.trace(__name__, f'Send SQL command: {User.get_select_by_discord_id_string(discord_id)}')
result = self._context.select(User.get_select_by_discord_id_string(discord_id))[0] results = self._context.select(User.get_select_by_discord_id_string(discord_id))
for result in results:
users.append(User(
result[1],
result[2],
self._servers.get_server_by_id(result[3]),
id=result[0]
))
def get_user_by_discord_id_and_server_id(self, discord_id: int, server_id: int) -> User:
self._logger.trace(__name__, f'Send SQL command: {User.get_select_by_discord_id_and_server_id_string(discord_id, server_id)}')
result = self._context.select(User.get_select_by_discord_id_and_server_id_string(discord_id, server_id))[0]
return User( return User(
result[1], result[1],
@ -55,9 +67,9 @@ class UserRepositoryService(UserRepositoryABC):
id=result[0] id=result[0]
) )
def find_user_by_discord_id(self, discord_id: int) -> Optional[User]: def find_user_by_discord_id_and_server_id(self, discord_id: int, server_id: int) -> Optional[User]:
self._logger.trace(__name__, f'Send SQL command: {User.get_select_by_discord_id_string(discord_id)}') self._logger.trace(__name__, f'Send SQL command: {User.get_select_by_discord_id_and_server_id_string(discord_id, server_id)}')
result = self._context.select(User.get_select_by_discord_id_string(discord_id)) result = self._context.select(User.get_select_by_discord_id_and_server_id_string(discord_id, server_id))
if result is None or len(result) == 0: if result is None or len(result) == 0:
return None return None