Added level model #25

This commit is contained in:
Sven Heidemann 2022-11-06 14:35:28 +01:00
parent e1dbab3f4f
commit 5a3eb57c0b
2 changed files with 118 additions and 0 deletions

View File

@ -20,6 +20,8 @@ class LevelMigration(MigrationABC):
CREATE TABLE IF NOT EXISTS `Levels` ( CREATE TABLE IF NOT EXISTS `Levels` (
`Id` BIGINT NOT NULL AUTO_INCREMENT, `Id` BIGINT NOT NULL AUTO_INCREMENT,
`Name` VARCHAR(255) NOT NULL, `Name` VARCHAR(255) NOT NULL,
`Color` VARCHAR(7) NOT NULL,
`MinXp` BIGINT NOT NULL,
`PermissionInt` BIGINT NOT NULL, `PermissionInt` BIGINT NOT NULL,
`ServerId` BIGINT, `ServerId` BIGINT,
PRIMARY KEY(`Id`), PRIMARY KEY(`Id`),

View File

@ -0,0 +1,116 @@
from datetime import datetime
from typing import Optional
from cpl_core.database import TableABC
from bot_data.model.server import Server
class Level(TableABC):
def __init__(self, name: str, color: str, min_xp: int, permissions: int, server: Optional[Server], created_at: datetime = None, modified_at: datetime = None, id=0):
self._id = id
self._name = name
self._color = color
self._min_xp = min_xp
self._permissions = permissions
self._server = server
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 id(self) -> int:
return self._id
@property
def name(self) -> str:
return self._name
@name.setter
def name(self, value: str):
self._name = value
@property
def color(self) -> str:
return self._color
@color.setter
def color(self, value: str):
self._color = value
@property
def min_xp(self) -> int:
return self._min_xp
@min_xp.setter
def min_xp(self, value: int):
self._min_xp = value
@property
def permissions(self) -> int:
return self._permissions
@permissions.setter
def permissions(self, value: int):
self._permissions = value
@property
def server(self) -> Optional[Server]:
return self._server
@staticmethod
def get_select_all_string() -> str:
return str(f"""
SELECT * FROM `Levels`;
""")
@staticmethod
def get_select_by_id_string(id: int) -> str:
return str(f"""
SELECT * FROM `Levels`
WHERE `Id` = {id};
""")
@staticmethod
def get_select_by_server_id_string(dc_id: int, s_id: int) -> str:
return str(f"""
SELECT * FROM `Levels`
WHERE `ServerId` = {s_id};
""")
@property
def insert_string(self) -> str:
return str(f"""
INSERT INTO `Levels` (
`Name`, `Color`, `MinXp`, `PermissionInt`, `ServerId`, `CreatedAt`, `LastModifiedAt`
) VALUES (
`{self._name}`,
`{self._color}`,
{self._min_xp},
{self._permissions},
{self._server.server_id},
'{self._created_at}',
'{self._modified_at}'
);
""")
@property
def udpate_string(self) -> str:
return str(f"""
UPDATE `Levels`
SET `Name` = {self._name},
`Name` = {self._name},
`Color` = {self._color},
`MinXp` = {self._min_xp},
`PermissionInt` = {self._permissions},
`LastModifiedAt` = '{self._modified_at}'
WHERE `Id` = {self._id};
""")
@property
def delete_string(self) -> str:
return str(f"""
DELETE FROM `Levels`
WHERE `Id` = {self._id};
""")