From a43676e9bfe691051a6b0ccc082e3baabd881f8d Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 9 Nov 2022 18:36:48 +0100 Subject: [PATCH] Added stats table #46 --- kdb-bot/src/bot_data/model/statistic.py | 108 ++++++++++++++++++ .../src/modules/stats/command/stats_group.py | 2 +- kdb-bot/src/modules/stats/model/statistic.py | 29 ----- .../modules/stats/ui/add_statistic_form.py | 6 +- 4 files changed, 112 insertions(+), 33 deletions(-) create mode 100644 kdb-bot/src/bot_data/model/statistic.py delete mode 100644 kdb-bot/src/modules/stats/model/statistic.py diff --git a/kdb-bot/src/bot_data/model/statistic.py b/kdb-bot/src/bot_data/model/statistic.py new file mode 100644 index 00000000..1c34a264 --- /dev/null +++ b/kdb-bot/src/bot_data/model/statistic.py @@ -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}; + """) diff --git a/kdb-bot/src/modules/stats/command/stats_group.py b/kdb-bot/src/modules/stats/command/stats_group.py index 67c8469a..0d61f249 100644 --- a/kdb-bot/src/modules/stats/command/stats_group.py +++ b/kdb-bot/src/modules/stats/command/stats_group.py @@ -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.logging.command_logger import CommandLogger 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.stats.model.statistic import Statistic from modules.stats.service.statistic_service import StatisticService from modules.stats.ui.add_statistic_form import AddStatisticForm diff --git a/kdb-bot/src/modules/stats/model/statistic.py b/kdb-bot/src/modules/stats/model/statistic.py deleted file mode 100644 index 1ab0afe4..00000000 --- a/kdb-bot/src/modules/stats/model/statistic.py +++ /dev/null @@ -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 diff --git a/kdb-bot/src/modules/stats/ui/add_statistic_form.py b/kdb-bot/src/modules/stats/ui/add_statistic_form.py index fe053241..073fc9d4 100644 --- a/kdb-bot/src/modules/stats/ui/add_statistic_form.py +++ b/kdb-bot/src/modules/stats/ui/add_statistic_form.py @@ -5,7 +5,7 @@ from discord import ui, TextStyle from bot_core.abc.message_service_abc import MessageServiceABC 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): @@ -15,7 +15,7 @@ class AddStatisticForm(ui.Modal): def __init__( self, - stats: List[Statistic], + stats: List[StatisticModel], name: str, message_service: MessageServiceABC, logger: CommandLogger, @@ -41,7 +41,7 @@ class AddStatisticForm(ui.Modal): statistic = self._stats.where(lambda s: s.name == self._name).single_or_default() try: 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')) return