Added stats table #46

This commit is contained in:
Sven Heidemann 2022-11-09 18:36:48 +01:00
parent fa7e41469b
commit a43676e9bf
4 changed files with 112 additions and 33 deletions

View File

@ -0,0 +1,108 @@
from datetime import datetime
from typing import Optional
from cpl_core.database import TableABC
from bot_data.model.server import Server
class Statistic(TableABC):
def __init__(self, name: str, description: str, code: str, server: Server, created_at: datetime=None, modified_at: datetime=None, id=0):
self._id = id
self._name = name
self._description = description
self._code = code
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
@property
def description(self) -> str:
return self._description
@description.setter
def description(self, value: str):
self._description = value
@property
def code(self) -> str:
return self._code
@code.setter
def code(self, value: str):
self._code = value
@property
def server(self) -> Server:
return self._server
@staticmethod
def get_select_all_string() -> str:
return str(f"""
SELECT * FROM `Statistics`;
""")
@staticmethod
def get_select_by_id_string(id: int) -> str:
return str(f"""
SELECT * FROM `Statistics`
WHERE `Id` = {id};
""")
@staticmethod
def get_select_by_name_string(name: str, s_id: int) -> str:
return str(f"""
SELECT * FROM `Statistics`
WHERE `Name` = {name};
""")
@staticmethod
def get_select_by_server_string(s_id: int) -> str:
return str(f"""
SELECT * FROM `Statistics`
WHERE `ServerId` = {s_id};
""")
@property
def insert_string(self) -> str:
return str(f"""
INSERT INTO `Statistics` (
`Name`, `Description`, `Code`, `ServerId`, `CreatedAt`, `LastModifiedAt`
) VALUES (
'{self._name}',
'{self._description}',
'{self._code}',
{self._server.server_id},
'{self._created_at}',
'{self._modified_at}'
);
""")
@property
def udpate_string(self) -> str:
return str(f"""
UPDATE `Statistics`
SET `Name` = {self._name},
`Description` = '{self._description}'
`Code` = '{self._code}'
`LastModifiedAt` = '{self._modified_at}'
WHERE `Id` = {self._id};
""")
@property
def delete_string(self) -> str:
return str(f"""
DELETE FROM `Statistics`
WHERE `Id` = {self._id};
""")

View File

@ -13,8 +13,8 @@ from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC
from bot_core.abc.message_service_abc import MessageServiceABC from bot_core.abc.message_service_abc import MessageServiceABC
from bot_core.logging.command_logger import CommandLogger from bot_core.logging.command_logger import CommandLogger
from bot_data.abc.server_repository_abc import ServerRepositoryABC from bot_data.abc.server_repository_abc import ServerRepositoryABC
from bot_data.model.statistic import Statistic
from modules.permission.abc.permission_service_abc import PermissionServiceABC from modules.permission.abc.permission_service_abc import PermissionServiceABC
from modules.stats.model.statistic import Statistic
from modules.stats.service.statistic_service import StatisticService from modules.stats.service.statistic_service import StatisticService
from modules.stats.ui.add_statistic_form import AddStatisticForm from modules.stats.ui.add_statistic_form import AddStatisticForm

View File

@ -1,29 +0,0 @@
from typing import Callable
class Statistic:
def __init__(self, name: str, description: str, code: str):
self._name = name
self._description = description
self._code = code
@property
def name(self) -> str:
return self._name
@property
def description(self) -> str:
return self._description
@description.setter
def description(self, value: str):
self._description = value
@property
def code(self) -> str:
return self._code
@code.setter
def code(self, value: str):
self._code = value

View File

@ -5,7 +5,7 @@ from discord import ui, TextStyle
from bot_core.abc.message_service_abc import MessageServiceABC from bot_core.abc.message_service_abc import MessageServiceABC
from bot_core.logging.command_logger import CommandLogger from bot_core.logging.command_logger import CommandLogger
from modules.stats.model.statistic import Statistic from modules.stats.model.statisticmodel import StatisticModel
class AddStatisticForm(ui.Modal): class AddStatisticForm(ui.Modal):
@ -15,7 +15,7 @@ class AddStatisticForm(ui.Modal):
def __init__( def __init__(
self, self,
stats: List[Statistic], stats: List[StatisticModel],
name: str, name: str,
message_service: MessageServiceABC, message_service: MessageServiceABC,
logger: CommandLogger, logger: CommandLogger,
@ -41,7 +41,7 @@ class AddStatisticForm(ui.Modal):
statistic = self._stats.where(lambda s: s.name == self._name).single_or_default() statistic = self._stats.where(lambda s: s.name == self._name).single_or_default()
try: try:
if statistic is None: if statistic is None:
self._stats.append(Statistic(self._name, self.description.value, self.code.value)) self._stats.append(StatisticModel(self._name, self.description.value, self.code.value))
await self._message_service.send_interaction_msg(interaction, self._t.transform('modules.stats.add.success')) await self._message_service.send_interaction_msg(interaction, self._t.transform('modules.stats.add.success'))
return return