Added UserJoinedServer model

This commit is contained in:
Sven Heidemann 2021-12-02 18:07:43 +01:00
parent c949efea50
commit 1d09ad80bf
2 changed files with 134 additions and 10 deletions

View File

@ -7,11 +7,10 @@ from gismo_data.model.server import Server
class User(TableABC):
def __init__(self, dc_id: int, xp: int, server: Optional[Server], created_at: datetime = None, modified_at: datetime = None, id=0):
def __init__(self, dc_id: int, xp: int, created_at: datetime = None, modified_at: datetime = None, id=0):
self._user_id = id
self._discord_id = dc_id
self._xp = xp
self._server = server
TableABC.__init__(self)
self._created_at = created_at if created_at is not None else self._created_at
@ -34,10 +33,6 @@ class User(TableABC):
self._modified_at = datetime.now().isoformat()
self._xp = value
@property
def server(self) -> Optional[Server]:
return self._server
@staticmethod
def get_create_string() -> str:
return str(f"""
@ -45,10 +40,8 @@ class User(TableABC):
`UserId` BIGINT NOT NULL AUTO_INCREMENT,
`DiscordId` BIGINT NOT NULL,
`XP` BIGINT NOT NULL DEFAULT 0,
`ServerId` BIGINT,
`CreatedAt` DATETIME(6),
`LastModifiedAt` DATETIME(6),
FOREIGN KEY (`ServerId`) REFERENCES Servers(`ServerId`),
PRIMARY KEY(`UserId`)
);
""")
@ -77,11 +70,10 @@ class User(TableABC):
def insert_string(self) -> str:
return str(f"""
INSERT INTO `Users` (
`DiscordId`, `XP`, `ServerId`, `CreatedAt`, `LastModifiedAt`
`DiscordId`, `XP`, `CreatedAt`, `LastModifiedAt`
) VALUES (
{self._discord_id},
{self._xp},
{self._server.server_id},
'{self._created_at}',
'{self._modified_at}'
);

View File

@ -0,0 +1,132 @@
from datetime import datetime
from typing import Optional
from cpl_core.database import TableABC
from gismo_data.model.user import User
from gismo_data.model.server import Server
class UserJoinedServer(TableABC):
def __init__(self, user: User, server: Server, joined_on: datetime, leaved_on: datetime=None, created_at: datetime=None, modified_at: datetime=None, id=0):
self._join_id = id
self._user = user
self._server = server
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 user(self) -> User:
return self._user
@property
def server(self) -> Server:
return self._server
@property
def joined_on(self) -> datetime:
return self._joined_on
@property
def leaved_on(self) -> datetime:
return self._leaved_on
@staticmethod
def get_create_string() -> str:
return str(f"""
CREATE TABLE IF NOT EXISTS `UserJoinedServers` (
`JoinId` BIGINT NOT NULL AUTO_INCREMENT,
`UserId` BIGINT NOT NULL,
`ServerId` BIGINT NOT NULL,
`JoinedOn` DATETIME(6) NOT NULL,
`LeavedOn` DATETIME(6),
`CreatedAt` DATETIME(6),
`LastModifiedAt` DATETIME(6),
FOREIGN KEY (`UserId`) REFERENCES Users(`UserId`),
FOREIGN KEY (`ServerId`) REFERENCES Servers(`ServerId`),
PRIMARY KEY(`ServerId`)
);
""")
@staticmethod
def get_select_all_string() -> str:
return str(f"""
SELECT * FROM `UserJoinedServers`;
""")
@staticmethod
def get_select_by_id_string(id: int) -> str:
return str(f"""
SELECT * FROM `UserJoinedServers`
WHERE `JoinId` = {id};
""")
@staticmethod
def get_select_by_user_id_string(id: int) -> str:
return str(f"""
SELECT * FROM `UserJoinedServers`
WHERE `UserId` = {id};
""")
@staticmethod
def get_select_active_by_user_id_string(id: int) -> str:
return str(f"""
SELECT * FROM `UserJoinedServers`
WHERE `UserId` = {id}
AND `LeavedOn` IS NULL;
""")
@staticmethod
def get_select_by_server_id_string(id: int) -> str:
return str(f"""
SELECT * FROM `UserJoinedServers`
WHERE `ServerId` = {id};
""")
@staticmethod
def get_select_active_by_server_id_string(id: int) -> str:
return str(f"""
SELECT * FROM `UserJoinedServers`
WHERE `ServerId` = {id}
AND `LeavedOn` IS NULL;
""")
@property
def insert_string(self) -> str:
return str(f"""
INSERT INTO `UserJoinedServers` (
`UserId`, `ServerId`, `JoinedOn`, `LeavedOn`, `CreatedAt`, `LastModifiedAt`
) VALUES (
{self._user.user_id},
{self._server.server_id},
{self._joined_on},
{self._leaved_on},
'{self._created_at}',
'{self._modified_at}'
);
""")
@property
def udpate_string(self) -> str:
return str(f"""
UPDATE `UserJoinedServers`
SET `LeavedOn` = {self._leaved_on},
`LastModifiedAt` = '{self._modified_at}'
WHERE `UserId` = {self._user.user_id}
AND `ServerId` = {self._server.server_id};
""")
@property
def delete_string(self) -> str:
return str(f"""
DELETE FROM `UserJoinedServers`
WHERE `Id` = {self._join_id};
""")