Improved database connection
This commit is contained in:
@@ -24,7 +24,7 @@ class ServerRepositoryABC(ABC):
|
||||
def find_server_by_discord_id(self, discord_id: int) -> Optional[Server]: pass
|
||||
|
||||
@abstractmethod
|
||||
def add_server(self, server: Server) -> int: pass
|
||||
def add_server(self, server: Server): pass
|
||||
|
||||
@abstractmethod
|
||||
def update_server(self, server: Server): pass
|
||||
|
@@ -24,7 +24,7 @@ class UserRepositoryABC(ABC):
|
||||
def find_user_by_discord_id(self, discord_id: int) -> Optional[User]: pass
|
||||
|
||||
@abstractmethod
|
||||
def add_user(self, user: User) -> int: pass
|
||||
def add_user(self, user: User): pass
|
||||
|
||||
@abstractmethod
|
||||
def update_user(self, user: User): pass
|
||||
|
@@ -29,10 +29,10 @@ class DBContext(DatabaseContext):
|
||||
def save_changes(self):
|
||||
try:
|
||||
self._logger.trace(__name__, "Save changes")
|
||||
super(DatabaseContext, self).save_changes
|
||||
super(DBContext, 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)
|
||||
return super(DBContext, self).select(statement)
|
@@ -1,13 +1,19 @@
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
|
||||
from cpl_core.database import TableABC
|
||||
|
||||
|
||||
class Server(TableABC):
|
||||
|
||||
def __init__(self, dc_id: int, id=0):
|
||||
def __init__(self, dc_id: int, created_at: datetime=None, modified_at: datetime=None, id=0):
|
||||
self._server_id = id
|
||||
self._discord_server_id = dc_id
|
||||
|
||||
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 server_id(self) -> int:
|
||||
return self._server_id
|
||||
@@ -20,8 +26,10 @@ class Server(TableABC):
|
||||
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,
|
||||
`ServerId` BIGINT NOT NULL AUTO_INCREMENT,
|
||||
`DiscordServerId` BIGINT NOT NULL,
|
||||
`CreatedAt` DATETIME(6),
|
||||
`LastModifiedAt` DATETIME(6),
|
||||
PRIMARY KEY(`ServerId`)
|
||||
);
|
||||
""")
|
||||
|
@@ -1,3 +1,4 @@
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
from cpl_core.database import TableABC
|
||||
|
||||
@@ -6,11 +7,15 @@ from gismo_data.model.server import Server
|
||||
|
||||
class User(TableABC):
|
||||
|
||||
def __init__(self, dc_id: int, xp: int, server: Optional[Server], id=0):
|
||||
def __init__(self, dc_id: int, xp: int, server: Optional[Server], 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
|
||||
self._modified_at = modified_at if modified_at is not None else self._modified_at
|
||||
|
||||
@property
|
||||
def user_id(self) -> int:
|
||||
@@ -26,6 +31,7 @@ class User(TableABC):
|
||||
|
||||
@xp.setter
|
||||
def xp(self, value: int):
|
||||
self._modified_at = datetime.now().isoformat()
|
||||
self._xp = value
|
||||
|
||||
@property
|
||||
@@ -42,7 +48,7 @@ class User(TableABC):
|
||||
`ServerId` BIGINT,
|
||||
`CreatedAt` DATETIME(6),
|
||||
`LastModifiedAt` DATETIME(6),
|
||||
FOREIGN KEY (`UserId`) REFERENCES Servers(`ServerId`),
|
||||
FOREIGN KEY (`ServerId`) REFERENCES Servers(`ServerId`),
|
||||
PRIMARY KEY(`UserId`)
|
||||
);
|
||||
""")
|
||||
|
@@ -29,7 +29,7 @@ class ServerRepositoryService(ServerRepositoryABC):
|
||||
|
||||
def get_server_by_id(self, id: int) -> Server:
|
||||
self._logger.trace(__name__, f'Send SQL command: {Server.get_select_by_id_string(id)}')
|
||||
result = self._context.select(Server.get_select_by_id_string(id))
|
||||
result = self._context.select(Server.get_select_by_id_string(id))[0]
|
||||
return Server(
|
||||
result[1],
|
||||
id=result[0]
|
||||
@@ -49,15 +49,18 @@ class ServerRepositoryService(ServerRepositoryABC):
|
||||
if result is None or len(result) == 0:
|
||||
return None
|
||||
|
||||
result = result[0]
|
||||
|
||||
return Server(
|
||||
result[1],
|
||||
result[2],
|
||||
result[3],
|
||||
id=result[0]
|
||||
)
|
||||
|
||||
def add_server(self, server: Server) -> int:
|
||||
def add_server(self, server: Server):
|
||||
self._logger.trace(__name__, f'Send SQL command: {server.insert_string}')
|
||||
self._context.cursor.execute(server.insert_string)
|
||||
return int(self._context.select("SELECT LAST_INSERT_ID();")[0])
|
||||
|
||||
def update_server(self, server: Server):
|
||||
self._logger.trace(__name__, f'Send SQL command: {server.udpate_string}')
|
||||
|
@@ -23,6 +23,7 @@ class UserRepositoryService(UserRepositoryABC):
|
||||
self._logger.trace(__name__, f'Send SQL command: {User.get_select_all_string()}')
|
||||
results = self._context.select(User.get_select_all_string())
|
||||
for result in results:
|
||||
self._logger.trace(__name__, f'Get user with id {result[0]}')
|
||||
users.append(User(
|
||||
result[1],
|
||||
result[2],
|
||||
@@ -44,7 +45,7 @@ class UserRepositoryService(UserRepositoryABC):
|
||||
|
||||
def get_user_by_discord_id(self, discord_id: int) -> User:
|
||||
self._logger.trace(__name__, f'Send SQL command: {User.get_select_by_discord_id_string(discord_id)}')
|
||||
result = self._context.select(User.get_select_by_discord_id_string(discord_id))
|
||||
result = self._context.select(User.get_select_by_discord_id_string(discord_id))[0]
|
||||
return User(
|
||||
result[1],
|
||||
result[2],
|
||||
@@ -58,17 +59,20 @@ class UserRepositoryService(UserRepositoryABC):
|
||||
if result is None or len(result) == 0:
|
||||
return None
|
||||
|
||||
result = result[0]
|
||||
|
||||
return User(
|
||||
result[1],
|
||||
result[2],
|
||||
result[3],
|
||||
result[4],
|
||||
self._servers.get_server_by_id(result[3]),
|
||||
id=result[0]
|
||||
)
|
||||
|
||||
def add_user(self, user: User) -> int:
|
||||
self._logger.trace(__name__, f'Send SQL command: {user.insert_strin}')
|
||||
def add_user(self, user: User):
|
||||
self._logger.trace(__name__, f'Send SQL command: {user.insert_string}')
|
||||
self._context.cursor.execute(user.insert_string)
|
||||
return int(self._context.select("SELECT LAST_INSERT_ID();")[0])
|
||||
|
||||
def update_user(self, user: User):
|
||||
self._logger.trace(__name__, f'Send SQL command: {user.udpate_string}')
|
||||
|
Reference in New Issue
Block a user