diff --git a/src/gismo/startup.py b/src/gismo/startup.py index 3d2d05f..4ddd38d 100644 --- a/src/gismo/startup.py +++ b/src/gismo/startup.py @@ -9,6 +9,7 @@ from cpl_core.dependency_injection import (ServiceCollectionABC, ServiceProviderABC) from cpl_core.environment import ApplicationEnvironment from cpl_core.logging import LoggerABC + from gismo_core.abc.bot_service_abc import BotServiceABC from gismo_core.abc.message_service_abc import MessageServiceABC from gismo_core.service.bot_service import BotService @@ -16,7 +17,10 @@ from gismo_core.service.message_service import MessageService from gismo_data.abc.server_repository_abc import ServerRepositoryABC from gismo_data.abc.user_repository_abc import UserRepositoryABC from gismo_data.db_context import DBContext -from gismo_data.service.server_repository_service import ServerRepositoryService +from gismo_data.service.client_repository_service import ( + ClientRepositoryABC, ClientRepositoryService) +from gismo_data.service.server_repository_service import \ + ServerRepositoryService from gismo_data.service.user_repository_service import UserRepositoryService from modules.base.base import Base from modules.boot_log.boot_log import BootLog @@ -58,7 +62,8 @@ class Startup(StartupABC): services.add_transient(ServerRepositoryABC, ServerRepositoryService) services.add_transient(UserRepositoryABC, UserRepositoryService) - + services.add_transient(ClientRepositoryABC, ClientRepositoryService) + services.add_transient(ModuleABC, Database) services.add_transient(ModuleABC, Base) services.add_transient(ModuleABC, BootLog) diff --git a/src/gismo_data/abc/client_repository_abc.py b/src/gismo_data/abc/client_repository_abc.py new file mode 100644 index 0000000..b55b63e --- /dev/null +++ b/src/gismo_data/abc/client_repository_abc.py @@ -0,0 +1,32 @@ +from abc import ABC, abstractmethod +from typing import Optional + +from cpl_query.extension import List +from gismo_data.model.client import Client + + +class ClientRepositoryABC(ABC): + + @abstractmethod + def __init__(self): pass + + @abstractmethod + def get_clients(self) -> List[Client]: pass + + @abstractmethod + def get_client_by_id(self, id: int) -> Client: pass + + @abstractmethod + def get_client_by_discord_id(self, discord_id: int) -> Client: pass + + @abstractmethod + def find_client_by_discord_id(self, discord_id: int) -> Optional[Client]: pass + + @abstractmethod + def add_client(self, client: Client): pass + + @abstractmethod + def update_client(self, client: Client): pass + + @abstractmethod + def delete_client(self, client: Client): pass diff --git a/src/gismo_data/service/client_repository_service.py b/src/gismo_data/service/client_repository_service.py new file mode 100644 index 0000000..bd0f410 --- /dev/null +++ b/src/gismo_data/service/client_repository_service.py @@ -0,0 +1,97 @@ +from typing import Optional +from cpl_core.database.context import DatabaseContextABC +from cpl_core.logging import LoggerABC +from cpl_query.extension import List + +from gismo_data.abc.client_repository_abc import ClientRepositoryABC +from gismo_data.abc.server_repository_abc import ServerRepositoryABC +from gismo_data.model.client import Client + + +class ClientRepositoryService(ClientRepositoryABC): + + def __init__(self, logger: LoggerABC, db_context: DatabaseContextABC, servers: ServerRepositoryABC): + self._logger = logger + self._context = db_context + + self._servers = servers + + ClientRepositoryABC.__init__(self) + + def get_clients(self) -> List[Client]: + clients = List(Client) + self._logger.trace(__name__, f'Send SQL command: {Client.get_select_all_string()}') + results = self._context.select(Client.get_select_all_string()) + for result in results: + self._logger.trace(__name__, f'Get client with id {result[0]}') + clients.append(Client( + result[1], + result[2], + result[3], + result[4], + result[5], + result[6], + self._servers.get_server_by_id(result[7]), + id=result[0] + )) + + return clients + + def get_client_by_id(self, id: int) -> Client: + self._logger.trace(__name__, f'Send SQL command: {Client.get_select_by_id_string(id)}') + result = self._context.select(Client.get_select_by_id_string(id)) + return Client( + result[1], + result[2], + result[3], + result[4], + result[5], + result[6], + self._servers.get_server_by_id(result[7]), + id=result[0] + ) + + def get_client_by_discord_id(self, discord_id: int) -> Client: + self._logger.trace(__name__, f'Send SQL command: {Client.get_select_by_discord_id_string(discord_id)}') + result = self._context.select(Client.get_select_by_discord_id_string(discord_id))[0] + return Client( + result[1], + result[2], + result[3], + result[4], + result[5], + result[6], + self._servers.get_server_by_id(result[7]), + id=result[0] + ) + + def find_client_by_discord_id(self, discord_id: int) -> Optional[Client]: + self._logger.trace(__name__, f'Send SQL command: {Client.get_select_by_discord_id_string(discord_id)}') + result = self._context.select(Client.get_select_by_discord_id_string(discord_id)) + if result is None or len(result) == 0: + return None + + result = result[0] + + return Client( + result[1], + result[2], + result[3], + result[4], + result[5], + result[6], + self._servers.get_server_by_id(result[7]), + id=result[0] + ) + + def add_client(self, client: Client): + self._logger.trace(__name__, f'Send SQL command: {client.insert_string}') + self._context.cursor.execute(client.insert_string) + + def update_client(self, client: Client): + self._logger.trace(__name__, f'Send SQL command: {client.udpate_string}') + self._context.cursor.execute(client.udpate_string) + + def delete_client(self, client: Client): + self._logger.trace(__name__, f'Send SQL command: {client.delete_string}') + self._context.cursor.execute(client.delete_string)