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`, `DiscordChannelId`, `JoinedOn`, `LeavedOn`, `CreatedAt`, `LastModifiedAt` ) VALUES ( {self._user.user_id}, {self._dc_channel_id}, '{self._joined_on}', '{self._leaved_on}', '{self._created_at}', '{self._modified_at}' ); """) else: return str(f""" INSERT INTO `UserJoinedVoiceChannel` ( `UserId`, `DiscordChannelId`, `JoinedOn`, `CreatedAt`, `LastModifiedAt` ) VALUES ( {self._user.user_id}, {self._dc_channel_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 `JoinId` = {self._join_id}; """) @property def delete_string(self) -> str: return str(f""" DELETE FROM `UserJoinedVoiceChannel` WHERE `JoinId` = {self._join_id}; """)