diff --git a/src/gismo/startup.py b/src/gismo/startup.py index 1e5e73e..223262a 100644 --- a/src/gismo/startup.py +++ b/src/gismo/startup.py @@ -17,6 +17,7 @@ from gismo_core.service.message_service import MessageService from gismo_data.abc.known_user_repository_abc import KnownUserRepositoryABC from gismo_data.abc.migration_abc import MigrationABC from gismo_data.abc.server_repository_abc import ServerRepositoryABC +from gismo_data.abc.user_joined_server_repository_abc import UserJoinedServerRepositoryABC from gismo_data.abc.user_repository_abc import UserRepositoryABC from gismo_data.db_context import DBContext from gismo_data.migration.initial_migration import InitialMigration @@ -28,6 +29,7 @@ from gismo_data.service.known_user_repository_service import \ from gismo_data.service.migration_service import MigrationService from gismo_data.service.server_repository_service import \ ServerRepositoryService +from gismo_data.service.user_joined_server_repository_service import UserJoinedServerRepositoryService from gismo_data.service.user_repository_service import UserRepositoryService from modules.base.base import Base from modules.boot_log.boot_log import BootLog @@ -76,6 +78,7 @@ class Startup(StartupABC): services.add_transient(UserRepositoryABC, UserRepositoryService) services.add_transient(ClientRepositoryABC, ClientRepositoryService) services.add_transient(KnownUserRepositoryABC, KnownUserRepositoryService) + services.add_transient(UserJoinedServerRepositoryABC, UserJoinedServerRepositoryService) # modules services.add_transient(ModuleABC, Database) diff --git a/src/gismo_data/abc/user_joined_server_repository_abc.py b/src/gismo_data/abc/user_joined_server_repository_abc.py new file mode 100644 index 0000000..49b8b7f --- /dev/null +++ b/src/gismo_data/abc/user_joined_server_repository_abc.py @@ -0,0 +1,35 @@ +from abc import ABC, abstractmethod +from typing import Optional + +from cpl_query.extension import List +from gismo_data.model.user_joined_server import UserJoinedServer + + +class UserJoinedServerRepositoryABC(ABC): + + @abstractmethod + def __init__(self): pass + + @abstractmethod + def get_user_joined_servers(self) -> List[UserJoinedServer]: pass + + @abstractmethod + def get_user_joined_server_by_id(self, id: int) -> UserJoinedServer: pass + + @abstractmethod + def get_user_joined_server_by_user_id(self, user_id: int) -> UserJoinedServer: pass + + @abstractmethod + def get_active_user_joined_server_by_user_id(self, user_id: int) -> UserJoinedServer: pass + + @abstractmethod + def find_active_user_joined_server_by_user_id(self, user_id: int) -> Optional[UserJoinedServer]: pass + + @abstractmethod + def add_user_joined_server(self, user_joined_server: UserJoinedServer): pass + + @abstractmethod + def update_user_joined_server(self, user_joined_server: UserJoinedServer): pass + + @abstractmethod + def delete_user_joined_server(self, user_joined_server: UserJoinedServer): pass diff --git a/src/gismo_data/model/user_joined_server.py b/src/gismo_data/model/user_joined_server.py index 4c87262..b31b527 100644 --- a/src/gismo_data/model/user_joined_server.py +++ b/src/gismo_data/model/user_joined_server.py @@ -63,22 +63,6 @@ class UserJoinedServer(TableABC): AND `LeavedOn` IS NULL; """) - @staticmethod - def get_select_by_server_id_string(id: int) -> str: - return str(f""" - SELECT * FROM `UserJoinedServers` - JOIN `Users` On `UserJoinedServers`.`UserId` = `Users`.`UserId` - WHERE `Users`.`ServerId` = {id}; - """) - - @staticmethod - def get_select_active_by_server_id_string(id: int) -> str: - return str(f""" - SELECT * FROM `UserJoinedServers` - JOIN `Users` On `UserJoinedServers`.`UserId` = `Users`.`UserId` - WHERE `Users`.`ServerId` = {id} - AND `UserJoinedServers`.`LeavedOn` IS NULL; - """) @property def insert_string(self) -> str: return str(f""" diff --git a/src/gismo_data/service/user_joined_server_repository_service.py b/src/gismo_data/service/user_joined_server_repository_service.py new file mode 100644 index 0000000..786ef57 --- /dev/null +++ b/src/gismo_data/service/user_joined_server_repository_service.py @@ -0,0 +1,111 @@ +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.user_joined_server_repository_abc import \ + UserJoinedServerRepositoryABC +from gismo_data.abc.user_repository_abc import UserRepositoryABC +from gismo_data.model.user import User +from gismo_data.model.user_joined_server import UserJoinedServer + + +class UserJoinedServerRepositoryService(UserJoinedServerRepositoryABC): + + def __init__(self, logger: LoggerABC, db_context: DatabaseContextABC, users: UserRepositoryABC): + self._logger = logger + self._context = db_context + + self._users = users + + UserJoinedServerRepositoryABC.__init__(self) + + def get_user_joined_servers(self) -> List[UserJoinedServer]: + joins = List(UserJoinedServer) + self._logger.trace(__name__, f'Send SQL command: {UserJoinedServer.get_select_all_string()}') + results = self._context.select(UserJoinedServer.get_select_all_string()) + for result in results: + self._logger.trace(__name__, f'Get user-joined-server with id {result[0]}') + joins.append(UserJoinedServer( + self._users.get_user_by_id(result[1]), + result[2], + result[3], + result[4], + result[5], + id=result[0] + )) + + return joins + + def get_user_joined_server_by_id(self, id: int) -> List[UserJoinedServer]: + joins = List(UserJoinedServer) + self._logger.trace(__name__, f'Send SQL command: {UserJoinedServer.get_select_by_id_string(id)}') + results = self._context.select(UserJoinedServer.get_select_by_id_string(id)) + for result in results: + joins.append(UserJoinedServer( + self._users.get_user_by_id(result[1]), + result[2], + result[3], + result[4], + result[5], + id=result[0] + )) + + return joins + + def get_user_joined_server_by_user_id(self, user_id: int) -> List[UserJoinedServer]: + joins = List(UserJoinedServer) + self._logger.trace(__name__, f'Send SQL command: {UserJoinedServer.get_select_by_user_id_string(user_id)}') + results = self._context.select(UserJoinedServer.get_select_by_user_id_string(user_id)) + for result in results: + joins.append(UserJoinedServer( + self._users.get_user_by_id(result[1]), + result[2], + result[3], + result[4], + result[5], + id=result[0] + )) + + return joins + + def get_active_user_joined_server_by_user_id(self, user_id: int) -> UserJoinedServer: + self._logger.trace(__name__, f'Send SQL command: {UserJoinedServer.get_select_by_user_id_string(user_id)}') + result = self._context.select(UserJoinedServer.get_select_active_by_user_id_string(user_id))[0] + return UserJoinedServer( + self._users.get_user_by_id(result[1]), + result[2], + result[3], + result[4], + result[5], + id=result[0] + ) + + def find_active_user_joined_server_by_user_id(self, user_id: int) -> Optional[UserJoinedServer]: + self._logger.trace(__name__, f'Send SQL command: {UserJoinedServer.get_select_by_user_id_string(user_id)}') + result = self._context.select(UserJoinedServer.get_select_active_by_user_id_string(user_id)) + if result is None or len(result) == 0: + return None + + result = result[0] + + return UserJoinedServer( + self._users.get_user_by_id(result[1]), + result[2], + result[3], + result[4], + result[5], + id=result[0] + ) + + def add_user_joined_server(self, user_joined_server: UserJoinedServer): + self._logger.trace(__name__, f'Send SQL command: {user_joined_server.insert_string}') + self._context.cursor.execute(user_joined_server.insert_string) + + def update_user_joined_server(self, user_joined_server: UserJoinedServer): + self._logger.trace(__name__, f'Send SQL command: {user_joined_server.udpate_string}') + self._context.cursor.execute(user_joined_server.udpate_string) + + def delete_user_joined_server(self, user_joined_server: UserJoinedServer): + self._logger.trace(__name__, f'Send SQL command: {user_joined_server.delete_string}') + self._context.cursor.execute(user_joined_server.delete_string) diff --git a/src/gismo_data/service/user_repository_service.py b/src/gismo_data/service/user_repository_service.py index 6854313..2644dfc 100644 --- a/src/gismo_data/service/user_repository_service.py +++ b/src/gismo_data/service/user_repository_service.py @@ -46,6 +46,9 @@ class UserRepositoryService(UserRepositoryABC): def get_user_by_discord_id(self, discord_id: int) -> 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))[0] + + result = result[0] + return User( result[1], result[2],