diff --git a/src/gismo_data/migration/migration_0_3_1.py b/src/gismo_data/migration/migration_0_3_1.py new file mode 100644 index 0000000..6102720 --- /dev/null +++ b/src/gismo_data/migration/migration_0_3_1.py @@ -0,0 +1,35 @@ +from cpl_core.logging import LoggerABC + +from gismo_data.abc.migration_abc import MigrationABC +from gismo_data.db_context import DBContext + +class Migration_0_3_1(MigrationABC): + + def __init__(self, logger: LoggerABC, db: DBContext): + self._logger = logger + self._db = db + self._cursor = db.cursor + + def upgrade(self): + self._logger.debug(__name__, 'Running upgrade') + + self._cursor.execute( + str(f""" + CREATE TABLE IF NOT EXISTS `UserJoinedVoiceChannel` ( + `JoinId` BIGINT NOT NULL AUTO_INCREMENT, + `UserId` BIGINT NOT NULL, + `DiscordChannelId` BIGINT NOT NULL, + `JoinedOn` DATETIME(6) NOT NULL, + `LeavedOn` DATETIME(6), + `CreatedAt` DATETIME(6), + `LastModifiedAt` DATETIME(6), + FOREIGN KEY (`UserId`) REFERENCES Users(`UserId`), + PRIMARY KEY(`JoinId`) + ); + """) + ) + + def downgrade(self): + self._logger.debug(__name__, 'Running downgrade') + self._cursor.execute('DROP TABLE `UserJoinedVoiceChannel`;') + diff --git a/src/gismo_data/model/user_joined_voice_channel.py b/src/gismo_data/model/user_joined_voice_channel.py new file mode 100644 index 0000000..63604e0 --- /dev/null +++ b/src/gismo_data/model/user_joined_voice_channel.py @@ -0,0 +1,119 @@ +from datetime import datetime + +from cpl_core.database import TableABC + +from gismo_data.model.user import User + + +class UserJoinedVoiceChannel(TableABC): + + def __init__(self, user: User, dc_channel_id: int, joined_on: datetime, leaved_on: datetime = None, created_at: datetime = None, modified_at: datetime = None, id=0): + self._join_id = id + self._dc_channel_id = dc_channel_id + self._user = user + self._joined_on = joined_on + self._leaved_on = leaved_on + + TableABC.__init__(self) + self._created_at = created_at if created_at is not None else self._created_at + self._modified_at = modified_at if modified_at is not None else self._modified_at + + @property + def join_id(self) -> int: + return self._join_id + + @property + def dc_channel_id(self) -> int: + return self._dc_channel_id + + @property + def user(self) -> User: + return self._user + + @property + def joined_on(self) -> datetime: + return self._joined_on + + @joined_on.setter + def joined_on(self, value: datetime): + self._modified_at = datetime.now() + self.joined_on = value + + @property + def leaved_on(self) -> datetime: + return self._leaved_on + + @leaved_on.setter + def leaved_on(self, value: datetime): + self._modified_at = datetime.now() + self._leaved_on = value + + @staticmethod + def get_select_all_string() -> str: + return str(f""" + SELECT * FROM `UserJoinedVoiceChannel`; + """) + + @staticmethod + def get_select_by_id_string(id: int) -> str: + return str(f""" + SELECT * FROM `UserJoinedVoiceChannel` + WHERE `JoinId` = {id}; + """) + + @staticmethod + def get_select_by_user_id_string(id: int) -> str: + return str(f""" + SELECT * FROM `UserJoinedVoiceChannel` + WHERE `UserId` = {id}; + """) + + @staticmethod + def get_select_active_by_user_id_string(id: int) -> str: + return str(f""" + SELECT * FROM `UserJoinedVoiceChannel` + WHERE `UserId` = {id} + AND `LeavedOn` IS NULL; + """) + + @property + def insert_string(self) -> str: + if self._leaved_on is not None: + return str(f""" + INSERT INTO `UserJoinedVoiceChannel` ( + `UserId`, `JoinedOn`, `LeavedOn`, `CreatedAt`, `LastModifiedAt` + ) VALUES ( + {self._user.user_id}, + '{self._joined_on}', + '{self._leaved_on}', + '{self._created_at}', + '{self._modified_at}' + ); + """) + else: + return str(f""" + INSERT INTO `UserJoinedVoiceChannel` ( + `UserId`, `JoinedOn`, `CreatedAt`, `LastModifiedAt` + ) VALUES ( + {self._user.user_id}, + '{self._joined_on}', + '{self._created_at}', + '{self._modified_at}' + ); + """) + + @property + def udpate_string(self) -> str: + return str(f""" + UPDATE `UserJoinedVoiceChannel` + SET `LeavedOn` = '{self._leaved_on}', + `LastModifiedAt` = '{self._modified_at}' + WHERE `UserId` = {self._user.user_id}; + """) + + @property + def delete_string(self) -> str: + return str(f""" + DELETE FROM `UserJoinedVoiceChannel` + WHERE `Id` = {self._join_id}; + """)