A-0.2 - Datenbankverbindung #35

Merged
edraft merged 13 commits from 0.2 into Alpha 2021-12-01 17:57:52 +01:00
4 changed files with 38 additions and 1 deletions
Showing only changes of commit 7bcd632b59 - Show all commits

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)
@ -38,6 +42,17 @@ class ServerRepositoryService(ServerRepositoryABC):
result[1], result[1],
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}')

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
@ -50,6 +51,19 @@ 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 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}')