Added database context
This commit is contained in:
parent
01cf17ef86
commit
14ecc46d92
@ -21,7 +21,7 @@ class Gismo(ApplicationABC):
|
||||
async def main(self):
|
||||
try:
|
||||
self._logger.trace(__name__, f'Try to start {BotService}')
|
||||
await self._bot.start_async()
|
||||
# await self._bot.start_async()
|
||||
except Exception as e:
|
||||
self._logger.error(__name__, 'Start failed', e)
|
||||
|
||||
|
@ -5,6 +5,16 @@
|
||||
"ConsoleLogLevel": "TRACE",
|
||||
"FileLogLevel": "TRACE"
|
||||
},
|
||||
"DatabaseSettings": {
|
||||
"Host": "localhost",
|
||||
"User": "sh_gismo",
|
||||
"Password": "c2hfZ2lzbW8=",
|
||||
"Database": "sh_gismo_dev",
|
||||
"Charset": "utf8mb4",
|
||||
"UseUnicode": "true",
|
||||
"Buffered": "true",
|
||||
"AuthPlugin": "mysql_native_password"
|
||||
},
|
||||
"Discord": {
|
||||
"Token": "OTExNTc0NDQyMzMxNzM0MDI2.YZjX2w.9yhsrTfrmkoDUqRJVzV5FVEwF3U"
|
||||
},
|
||||
|
@ -1,9 +1,10 @@
|
||||
from datetime import datetime
|
||||
import os
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
|
||||
from cpl_core.application import StartupABC
|
||||
from cpl_core.configuration import ConfigurationABC
|
||||
from cpl_core.database import DatabaseSettings
|
||||
from cpl_core.dependency_injection import (ServiceCollectionABC,
|
||||
ServiceProviderABC)
|
||||
from cpl_core.environment import ApplicationEnvironment
|
||||
@ -12,6 +13,11 @@ from gismo_core.abc.bot_service_abc import BotServiceABC
|
||||
from gismo_core.abc.message_service_abc import MessageServiceABC
|
||||
from gismo_core.service.bot_service import BotService
|
||||
from gismo_core.service.message_service import MessageService
|
||||
from gismo_data.abc.server_repository_abc import ServerRepositoryABC
|
||||
from gismo_data.abc.user_repository_abc import UserRepositoryABC
|
||||
from gismo_data.db_context import DBContext
|
||||
from gismo_data.service.server_repository_service import ServerRepositoryService
|
||||
from gismo_data.service.user_repository_service import UserRepositoryService
|
||||
from modules.boot_log.boot_log import BootLog
|
||||
from modules_core.abc.module_abc import ModuleABC
|
||||
from modules_core.abc.module_service_abc import ModuleServiceABC
|
||||
@ -42,10 +48,15 @@ class Startup(StartupABC):
|
||||
async def configure_services(self, services: ServiceCollectionABC, environment: ApplicationEnvironment) -> ServiceProviderABC:
|
||||
services.add_logging()
|
||||
|
||||
services.add_db_context(DBContext, self._config.get_configuration(DatabaseSettings))
|
||||
|
||||
services.add_singleton(ModuleServiceABC, ModuleService)
|
||||
services.add_singleton(BotServiceABC, BotService)
|
||||
services.add_transient(MessageServiceABC, MessageService)
|
||||
|
||||
services.add_transient(ServerRepositoryABC, ServerRepositoryService)
|
||||
services.add_transient(UserRepositoryABC, UserRepositoryService)
|
||||
|
||||
services.add_transient(ModuleABC, BootLog)
|
||||
|
||||
provider: ServiceProviderABC = services.build_service_provider()
|
||||
|
@ -1,25 +1 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
gismo sh-edraft Gismo
|
||||
~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
sh-edraft Dicord bot Gismo
|
||||
|
||||
:copyright: (c) 2021 - 2022 sh-edraft.de
|
||||
:license: MIT, see LICENSE for more details.
|
||||
|
||||
"""
|
||||
|
||||
__title__ = 'gismo_data'
|
||||
__author__ = 'Sven Heidemann'
|
||||
__license__ = 'MIT'
|
||||
__copyright__ = 'Copyright (c) 2021 - 2022 sh-edraft.de'
|
||||
__version__ = '0.2.0'
|
||||
|
||||
from collections import namedtuple
|
||||
|
||||
# imports:
|
||||
|
||||
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
|
||||
version_info = VersionInfo(major='0', minor='2', micro='0')
|
||||
# imports
|
||||
|
1
src/gismo_data/abc/__init__.py
Normal file
1
src/gismo_data/abc/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
# imports
|
20
src/gismo_data/abc/server_repository_abc.py
Normal file
20
src/gismo_data/abc/server_repository_abc.py
Normal file
@ -0,0 +1,20 @@
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
from cpl_query.extension import List
|
||||
|
||||
from gismo_data.model.server import Server
|
||||
|
||||
|
||||
class ServerRepositoryABC(ABC):
|
||||
|
||||
@abstractmethod
|
||||
def __init__(self): pass
|
||||
|
||||
@abstractmethod
|
||||
def get_servers(self) -> List[Server]: pass
|
||||
|
||||
@abstractmethod
|
||||
def get_server_by_id(self, id: int) -> Server: pass
|
||||
|
||||
@abstractmethod
|
||||
def get_server_by_discord_id(self, discord_id: int) -> Server: pass
|
20
src/gismo_data/abc/user_repository_abc.py
Normal file
20
src/gismo_data/abc/user_repository_abc.py
Normal file
@ -0,0 +1,20 @@
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
from cpl_query.extension import List
|
||||
|
||||
from gismo_data.model.user import User
|
||||
|
||||
|
||||
class UserRepositoryABC(ABC):
|
||||
|
||||
@abstractmethod
|
||||
def __init__(self): pass
|
||||
|
||||
@abstractmethod
|
||||
def get_users(self) -> List[User]: pass
|
||||
|
||||
@abstractmethod
|
||||
def get_user_by_id(self, id: int) -> User: pass
|
||||
|
||||
@abstractmethod
|
||||
def get_user_by_discord_id(self, discord_id: int) -> User: pass
|
32
src/gismo_data/db_context.py
Normal file
32
src/gismo_data/db_context.py
Normal file
@ -0,0 +1,32 @@
|
||||
from cpl_core.database import DatabaseSettings
|
||||
from cpl_core.database.context import DatabaseContext
|
||||
from cpl_core.logging import LoggerABC
|
||||
|
||||
|
||||
class DBContext(DatabaseContext):
|
||||
|
||||
def __init__(self, logger: LoggerABC):
|
||||
|
||||
self._logger = logger
|
||||
|
||||
DatabaseContext.__init__(self)
|
||||
|
||||
|
||||
def connect(self, database_settings: DatabaseSettings):
|
||||
try:
|
||||
self._logger.debug(__name__, "Connecting to database")
|
||||
super(DatabaseContext, self).connect(database_settings)
|
||||
self._logger.info(__name__, "Connected to database")
|
||||
except Exception as e:
|
||||
self._logger.fatal(__name__, "Connecting to database failed", e)
|
||||
|
||||
def save_changes(self):
|
||||
try:
|
||||
self._logger.trace(__name__, "Save changes")
|
||||
super(DatabaseContext, self).save_changes
|
||||
self._logger.debug(__name__, "Saved changes")
|
||||
except Exception as e:
|
||||
self._logger.error(__name__, "Saving changes failed", e)
|
||||
|
||||
def select(self, statement: str) -> list[tuple]:
|
||||
return super(DatabaseContext, self).select(statement)
|
1
src/gismo_data/model/__init__.py
Normal file
1
src/gismo_data/model/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
# imports
|
62
src/gismo_data/model/server.py
Normal file
62
src/gismo_data/model/server.py
Normal file
@ -0,0 +1,62 @@
|
||||
from typing import Optional
|
||||
from cpl_core.database import TableABC
|
||||
|
||||
|
||||
class Server(TableABC):
|
||||
|
||||
def __init__(self, dc_id: int, id=0):
|
||||
self._server_id = id
|
||||
self._discord_server_id = dc_id
|
||||
|
||||
@property
|
||||
def server_id(self) -> int:
|
||||
return self._server_id
|
||||
|
||||
@property
|
||||
def discord_server_id(self) -> int:
|
||||
return self._discord_server_id
|
||||
|
||||
@staticmethod
|
||||
def get_create_string() -> str:
|
||||
return str(f"""
|
||||
CREATE TABLE IF NOT EXISTS `Servers` (
|
||||
`ServerId` INT(30) NOT NULL AUTO_INCREMENT,
|
||||
`DiscordServerId` INT(30) NOT NULL,
|
||||
PRIMARY KEY(`ServerId`)
|
||||
);
|
||||
""")
|
||||
|
||||
@staticmethod
|
||||
def get_select_all_string() -> str:
|
||||
return str(f"""
|
||||
SELECT * FROM `Servers`;
|
||||
""")
|
||||
|
||||
@staticmethod
|
||||
def get_select_by_id_string(id: int) -> str:
|
||||
return str(f"""
|
||||
SELECT * FROM `Servers`
|
||||
WHERE `ServerId` = {id};
|
||||
""")
|
||||
|
||||
@staticmethod
|
||||
def get_select_by_discord_id_string(id: int) -> str:
|
||||
return str(f"""
|
||||
SELECT * FROM `Servers`
|
||||
WHERE `DiscordServerId` = {id};
|
||||
""")
|
||||
|
||||
@property
|
||||
def insert_string(self) -> str:
|
||||
return str(f"""
|
||||
""")
|
||||
|
||||
@property
|
||||
def udpate_string(self) -> str:
|
||||
return str(f"""
|
||||
""")
|
||||
|
||||
@property
|
||||
def delete_string(self) -> str:
|
||||
return str(f"""
|
||||
""")
|
80
src/gismo_data/model/user.py
Normal file
80
src/gismo_data/model/user.py
Normal file
@ -0,0 +1,80 @@
|
||||
from typing import Optional
|
||||
from cpl_core.database import TableABC
|
||||
|
||||
from gismo_data.model.server import Server
|
||||
|
||||
class User(TableABC):
|
||||
|
||||
def __init__(self, dc_id: int, xp: int, server: Optional[Server], id=0):
|
||||
self._user_id = id
|
||||
self._discord_id = dc_id
|
||||
self._xp = xp
|
||||
self._server = server
|
||||
|
||||
@property
|
||||
def user_id(self) -> int:
|
||||
return self._user_id
|
||||
|
||||
@property
|
||||
def discord_id(self) -> int:
|
||||
return self._discord_id
|
||||
|
||||
@property
|
||||
def xp(self) -> int:
|
||||
return self._xp
|
||||
|
||||
@xp.setter
|
||||
def xp(self, value: int):
|
||||
self._xp = value
|
||||
|
||||
@property
|
||||
def server(self) -> Optional[Server]:
|
||||
return self._server
|
||||
|
||||
@staticmethod
|
||||
def get_create_string() -> str:
|
||||
return str(f"""
|
||||
CREATE TABLE IF NOT EXISTS `Users` (
|
||||
`UserId` INT(30) NOT NULL AUTO_INCREMENT,
|
||||
`DiscordId` INT(30) NOT NULL,
|
||||
`XP` INT(30) NOT NULL DEFAULT 0,
|
||||
`ServerId` INT(30),
|
||||
FOREIGN KEY (`UserId`) REFERENCES Server(`ServerId`),
|
||||
PRIMARY KEY(`UserId`)
|
||||
);
|
||||
""")
|
||||
|
||||
@staticmethod
|
||||
def get_select_all_string() -> str:
|
||||
return str(f"""
|
||||
SELECT * FROM `Users`;
|
||||
""")
|
||||
|
||||
@staticmethod
|
||||
def get_select_by_id_string(id: int) -> str:
|
||||
return str(f"""
|
||||
SELECT * FROM `Users`
|
||||
WHERE `UserId` = {id};
|
||||
""")
|
||||
|
||||
@staticmethod
|
||||
def get_select_by_discord_id_string(id: int) -> str:
|
||||
return str(f"""
|
||||
SELECT * FROM `Users`
|
||||
WHERE `DiscordId` = {id};
|
||||
""")
|
||||
|
||||
@property
|
||||
def insert_string(self) -> str:
|
||||
return str(f"""
|
||||
""")
|
||||
|
||||
@property
|
||||
def udpate_string(self) -> str:
|
||||
return str(f"""
|
||||
""")
|
||||
|
||||
@property
|
||||
def delete_string(self) -> str:
|
||||
return str(f"""
|
||||
""")
|
1
src/gismo_data/service/__init__.py
Normal file
1
src/gismo_data/service/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
# imports
|
37
src/gismo_data/service/server_repository_service.py
Normal file
37
src/gismo_data/service/server_repository_service.py
Normal file
@ -0,0 +1,37 @@
|
||||
from cpl_core.database.context import DatabaseContextABC
|
||||
from cpl_query.extension import List
|
||||
from gismo_data.abc.server_repository_abc import ServerRepositoryABC
|
||||
from gismo_data.model.server import Server
|
||||
|
||||
|
||||
class ServerRepositoryService(ServerRepositoryABC):
|
||||
|
||||
def __init__(self, db_context: DatabaseContextABC):
|
||||
self._context = db_context
|
||||
|
||||
ServerRepositoryABC.__init__(self)
|
||||
|
||||
def get_servers(self) -> List[Server]:
|
||||
servers = List(Server)
|
||||
results = self._context.select(Server.get_select_all_string())
|
||||
for result in results:
|
||||
servers.append(Server(
|
||||
result[1],
|
||||
id=result[0]
|
||||
))
|
||||
|
||||
return servers
|
||||
|
||||
def get_server_by_id(self, id: int) -> Server:
|
||||
result = self._context.select(Server.get_select_by_id_string(id))
|
||||
return Server(
|
||||
result[1],
|
||||
id=result[0]
|
||||
)
|
||||
|
||||
def get_server_by_discord_id(self, discord_id: int) -> Server:
|
||||
result = self._context.select(Server.get_select_by_discord_id_string(discord_id))
|
||||
return Server(
|
||||
result[1],
|
||||
id=result[0]
|
||||
)
|
47
src/gismo_data/service/user_repository_service.py
Normal file
47
src/gismo_data/service/user_repository_service.py
Normal file
@ -0,0 +1,47 @@
|
||||
from cpl_core.database.context import DatabaseContextABC
|
||||
from cpl_query.extension import List
|
||||
from gismo_data.abc.server_repository_abc import ServerRepositoryABC
|
||||
|
||||
from gismo_data.abc.user_repository_abc import UserRepositoryABC
|
||||
from gismo_data.model.user import User
|
||||
|
||||
|
||||
class UserRepositoryService(UserRepositoryABC):
|
||||
|
||||
def __init__(self, db_context: DatabaseContextABC, servers: ServerRepositoryABC):
|
||||
self._context = db_context
|
||||
|
||||
self._servers = servers
|
||||
|
||||
UserRepositoryABC.__init__(self)
|
||||
|
||||
def get_users(self) -> List[User]:
|
||||
users = List(User)
|
||||
results = self._context.select(User.get_select_all_string())
|
||||
for result in results:
|
||||
users.append(User(
|
||||
result[1],
|
||||
result[2],
|
||||
self._servers.get_server_by_id(result[3]),
|
||||
id=result[0]
|
||||
))
|
||||
|
||||
return users
|
||||
|
||||
def get_user_by_id(self, id: int) -> User:
|
||||
result = self._context.select(User.get_select_by_id_string(id))
|
||||
return User(
|
||||
result[1],
|
||||
result[2],
|
||||
self._servers.get_server_by_id(result[3]),
|
||||
id=result[0]
|
||||
)
|
||||
|
||||
def get_user_by_discord_id(self, discord_id: int) -> User:
|
||||
result = self._context.select(User.get_select_by_discord_id_string(discord_id))
|
||||
return User(
|
||||
result[1],
|
||||
result[2],
|
||||
self._servers.get_server_by_id(result[3]),
|
||||
id=result[0]
|
||||
)
|
Reference in New Issue
Block a user