Added find by dc id to repos

This commit is contained in:
Sven Heidemann 2021-11-30 15:55:17 +01:00
parent 20aedba23d
commit 7bcd632b59
4 changed files with 38 additions and 1 deletions

View File

@ -1,4 +1,5 @@
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from typing import Optional
from cpl_query.extension import List from cpl_query.extension import List
@ -19,6 +20,9 @@ class ServerRepositoryABC(ABC):
@abstractmethod @abstractmethod
def get_server_by_discord_id(self, discord_id: int) -> Server: pass def get_server_by_discord_id(self, discord_id: int) -> Server: pass
@abstractmethod
def find_server_by_discord_id(self, discord_id: int) -> Optional[Server]: pass
@abstractmethod @abstractmethod
def add_server(self, server: Server) -> int: pass def add_server(self, server: Server) -> int: pass

View File

@ -1,4 +1,5 @@
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from typing import Optional
from cpl_query.extension import List from cpl_query.extension import List
@ -19,6 +20,9 @@ class UserRepositoryABC(ABC):
@abstractmethod @abstractmethod
def get_user_by_discord_id(self, discord_id: int) -> User: pass def get_user_by_discord_id(self, discord_id: int) -> User: pass
@abstractmethod
def find_user_by_discord_id(self, discord_id: int) -> Optional[User]: pass
@abstractmethod @abstractmethod
def add_user(self, user: User) -> int: pass def add_user(self, user: User) -> int: pass

View File

@ -1,12 +1,16 @@
from typing import Optional
from cpl_core.database.context import DatabaseContextABC from cpl_core.database.context import DatabaseContextABC
from cpl_core.logging import LoggerABC
from cpl_query.extension import List from cpl_query.extension import List
from gismo_data.abc.server_repository_abc import ServerRepositoryABC from gismo_data.abc.server_repository_abc import ServerRepositoryABC
from gismo_data.model.server import Server from gismo_data.model.server import Server
class ServerRepositoryService(ServerRepositoryABC): class ServerRepositoryService(ServerRepositoryABC):
def __init__(self, db_context: DatabaseContextABC): def __init__(self, logger: LoggerABC, db_context: DatabaseContextABC):
self._logger = logger
self._context = db_context self._context = db_context
ServerRepositoryABC.__init__(self) ServerRepositoryABC.__init__(self)
@ -39,6 +43,17 @@ class ServerRepositoryService(ServerRepositoryABC):
id=result[0] id=result[0]
) )
def find_server_by_discord_id(self, discord_id: int) -> Optional[Server]:
self._logger.trace(__name__, f'Send SQL command: {Server.get_select_by_discord_id_string(discord_id)}')
result = self._context.select(Server.get_select_by_discord_id_string(discord_id))
if len(result) == 0:
return None
return Server(
result[1],
id=result[0]
)
def add_server(self, server: Server) -> int: def add_server(self, server: Server) -> int:
self._logger.trace(__name__, f'Send SQL command: {server.insert_string}') self._logger.trace(__name__, f'Send SQL command: {server.insert_string}')
self._context.cursor.execute(server.insert_string) self._context.cursor.execute(server.insert_string)

View File

@ -1,3 +1,4 @@
from typing import Optional
from cpl_core.database.context import DatabaseContextABC from cpl_core.database.context import DatabaseContextABC
from cpl_core.logging import LoggerABC from cpl_core.logging import LoggerABC
from cpl_query.extension import List from cpl_query.extension import List
@ -51,6 +52,19 @@ class UserRepositoryService(UserRepositoryABC):
id=result[0] id=result[0]
) )
def find_user_by_discord_id(self, discord_id: int) -> Optional[User]:
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))
if len(result) == 0:
return None
return User(
result[1],
result[2],
self._servers.get_server_by_id(result[3]),
id=result[0]
)
def add_user(self, user: User) -> int: def add_user(self, user: User) -> int:
self._logger.trace(__name__, f'Send SQL command: {user.insert_strin}') self._logger.trace(__name__, f'Send SQL command: {user.insert_strin}')
self._context.cursor.execute(user.insert_string) self._context.cursor.execute(user.insert_string)