Improved data repository
This commit is contained in:
parent
1d4a6c3e3b
commit
20aedba23d
@ -18,3 +18,12 @@ class ServerRepositoryABC(ABC):
|
|||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def get_server_by_discord_id(self, discord_id: int) -> Server: pass
|
def get_server_by_discord_id(self, discord_id: int) -> Server: pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def add_server(self, server: Server) -> int: pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def update_server(self, server: Server): pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def delete_server(self, server: Server): pass
|
@ -18,3 +18,12 @@ class UserRepositoryABC(ABC):
|
|||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def get_user_by_discord_id(self, discord_id: int) -> User: pass
|
def get_user_by_discord_id(self, discord_id: int) -> User: pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def add_user(self, user: User) -> int: pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def update_user(self, user: User): pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def delete_user(self, user: User): pass
|
||||||
|
@ -49,14 +49,27 @@ class Server(TableABC):
|
|||||||
@property
|
@property
|
||||||
def insert_string(self) -> str:
|
def insert_string(self) -> str:
|
||||||
return str(f"""
|
return str(f"""
|
||||||
|
INSERT INTO `Servers` (
|
||||||
|
`DiscordServerId`, `CreatedAt`, `LastModifiedAt`
|
||||||
|
) VALUES (
|
||||||
|
{self._discord_server_id},
|
||||||
|
'{self._created_at}',
|
||||||
|
'{self._modified_at}'
|
||||||
|
);
|
||||||
""")
|
""")
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def udpate_string(self) -> str:
|
def udpate_string(self) -> str:
|
||||||
return str(f"""
|
return str(f"""
|
||||||
|
UPDATE `Servers`
|
||||||
|
SET `DiscordServerId` = {self._discord_server_id},
|
||||||
|
`LastModifiedAt` = '{self._modified_at}'
|
||||||
|
WHERE `Id` = {self._id};
|
||||||
""")
|
""")
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def delete_string(self) -> str:
|
def delete_string(self) -> str:
|
||||||
return str(f"""
|
return str(f"""
|
||||||
|
DELETE FROM `Servers`
|
||||||
|
WHERE `Id` = {self._id};
|
||||||
""")
|
""")
|
||||||
|
@ -3,6 +3,7 @@ from cpl_core.database import TableABC
|
|||||||
|
|
||||||
from gismo_data.model.server import Server
|
from gismo_data.model.server import Server
|
||||||
|
|
||||||
|
|
||||||
class User(TableABC):
|
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], id=0):
|
||||||
@ -14,49 +15,51 @@ class User(TableABC):
|
|||||||
@property
|
@property
|
||||||
def user_id(self) -> int:
|
def user_id(self) -> int:
|
||||||
return self._user_id
|
return self._user_id
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def discord_id(self) -> int:
|
def discord_id(self) -> int:
|
||||||
return self._discord_id
|
return self._discord_id
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def xp(self) -> int:
|
def xp(self) -> int:
|
||||||
return self._xp
|
return self._xp
|
||||||
|
|
||||||
@xp.setter
|
@xp.setter
|
||||||
def xp(self, value: int):
|
def xp(self, value: int):
|
||||||
self._xp = value
|
self._xp = value
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def server(self) -> Optional[Server]:
|
def server(self) -> Optional[Server]:
|
||||||
return self._server
|
return self._server
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_create_string() -> str:
|
def get_create_string() -> str:
|
||||||
return str(f"""
|
return str(f"""
|
||||||
CREATE TABLE IF NOT EXISTS `Users` (
|
CREATE TABLE IF NOT EXISTS `Users` (
|
||||||
`UserId` INT(30) NOT NULL AUTO_INCREMENT,
|
`UserId` BIGINT NOT NULL AUTO_INCREMENT,
|
||||||
`DiscordId` INT(30) NOT NULL,
|
`DiscordId` BIGINT NOT NULL,
|
||||||
`XP` INT(30) NOT NULL DEFAULT 0,
|
`XP` BIGINT NOT NULL DEFAULT 0,
|
||||||
`ServerId` INT(30),
|
`ServerId` BIGINT,
|
||||||
|
`CreatedAt` DATETIME(6),
|
||||||
|
`LastModifiedAt` DATETIME(6),
|
||||||
FOREIGN KEY (`UserId`) REFERENCES Servers(`ServerId`),
|
FOREIGN KEY (`UserId`) REFERENCES Servers(`ServerId`),
|
||||||
PRIMARY KEY(`UserId`)
|
PRIMARY KEY(`UserId`)
|
||||||
);
|
);
|
||||||
""")
|
""")
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_select_all_string() -> str:
|
def get_select_all_string() -> str:
|
||||||
return str(f"""
|
return str(f"""
|
||||||
SELECT * FROM `Users`;
|
SELECT * FROM `Users`;
|
||||||
""")
|
""")
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_select_by_id_string(id: int) -> str:
|
def get_select_by_id_string(id: int) -> str:
|
||||||
return str(f"""
|
return str(f"""
|
||||||
SELECT * FROM `Users`
|
SELECT * FROM `Users`
|
||||||
WHERE `UserId` = {id};
|
WHERE `UserId` = {id};
|
||||||
""")
|
""")
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_select_by_discord_id_string(id: int) -> str:
|
def get_select_by_discord_id_string(id: int) -> str:
|
||||||
return str(f"""
|
return str(f"""
|
||||||
@ -67,14 +70,29 @@ class User(TableABC):
|
|||||||
@property
|
@property
|
||||||
def insert_string(self) -> str:
|
def insert_string(self) -> str:
|
||||||
return str(f"""
|
return str(f"""
|
||||||
|
INSERT INTO `Users` (
|
||||||
|
`DiscordId`, `XP`, `ServerId`, `CreatedAt`, `LastModifiedAt`
|
||||||
|
) VALUES (
|
||||||
|
{self._discord_id},
|
||||||
|
{self._xp},
|
||||||
|
{self._server.server_id},
|
||||||
|
'{self._created_at}',
|
||||||
|
'{self._modified_at}'
|
||||||
|
);
|
||||||
""")
|
""")
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def udpate_string(self) -> str:
|
def udpate_string(self) -> str:
|
||||||
return str(f"""
|
return str(f"""
|
||||||
|
UPDATE `Users`
|
||||||
|
SET `XP` = {self._xp},
|
||||||
|
`LastModifiedAt` = '{self._modified_at}'
|
||||||
|
WHERE `Id` = {self._id};
|
||||||
""")
|
""")
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def delete_string(self) -> str:
|
def delete_string(self) -> str:
|
||||||
return str(f"""
|
return str(f"""
|
||||||
|
DELETE FROM `Users`
|
||||||
|
WHERE `Id` = {self._id};
|
||||||
""")
|
""")
|
||||||
|
@ -13,6 +13,7 @@ class ServerRepositoryService(ServerRepositoryABC):
|
|||||||
|
|
||||||
def get_servers(self) -> List[Server]:
|
def get_servers(self) -> List[Server]:
|
||||||
servers = List(Server)
|
servers = List(Server)
|
||||||
|
self._logger.trace(__name__, f'Send SQL command: {Server.get_select_all_string()}')
|
||||||
results = self._context.select(Server.get_select_all_string())
|
results = self._context.select(Server.get_select_all_string())
|
||||||
for result in results:
|
for result in results:
|
||||||
servers.append(Server(
|
servers.append(Server(
|
||||||
@ -23,6 +24,7 @@ class ServerRepositoryService(ServerRepositoryABC):
|
|||||||
return servers
|
return servers
|
||||||
|
|
||||||
def get_server_by_id(self, id: int) -> Server:
|
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))
|
||||||
return Server(
|
return Server(
|
||||||
result[1],
|
result[1],
|
||||||
@ -30,8 +32,22 @@ class ServerRepositoryService(ServerRepositoryABC):
|
|||||||
)
|
)
|
||||||
|
|
||||||
def get_server_by_discord_id(self, discord_id: int) -> Server:
|
def get_server_by_discord_id(self, discord_id: int) -> Server:
|
||||||
|
self._logger.trace(__name__, f'Send SQL command: {Server.get_select_by_discord_id_string(discord_id)}')
|
||||||
result = self._context.select(Server.get_select_by_discord_id_string(discord_id))
|
result = self._context.select(Server.get_select_by_discord_id_string(discord_id))
|
||||||
return Server(
|
return Server(
|
||||||
result[1],
|
result[1],
|
||||||
id=result[0]
|
id=result[0]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def add_server(self, server: Server) -> int:
|
||||||
|
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}')
|
||||||
|
self._context.cursor.execute(server.udpate_string)
|
||||||
|
|
||||||
|
def delete_server(self, server: Server):
|
||||||
|
self._logger.trace(__name__, f'Send SQL command: {server.delete_string}')
|
||||||
|
self._context.cursor.execute(server.delete_string)
|
||||||
|
@ -1,14 +1,16 @@
|
|||||||
from cpl_core.database.context import DatabaseContextABC
|
from cpl_core.database.context import DatabaseContextABC
|
||||||
|
from cpl_core.logging import LoggerABC
|
||||||
from cpl_query.extension import List
|
from cpl_query.extension import List
|
||||||
from gismo_data.abc.server_repository_abc import ServerRepositoryABC
|
|
||||||
|
|
||||||
|
from gismo_data.abc.server_repository_abc import ServerRepositoryABC
|
||||||
from gismo_data.abc.user_repository_abc import UserRepositoryABC
|
from gismo_data.abc.user_repository_abc import UserRepositoryABC
|
||||||
from gismo_data.model.user import User
|
from gismo_data.model.user import User
|
||||||
|
|
||||||
|
|
||||||
class UserRepositoryService(UserRepositoryABC):
|
class UserRepositoryService(UserRepositoryABC):
|
||||||
|
|
||||||
def __init__(self, db_context: DatabaseContextABC, servers: ServerRepositoryABC):
|
def __init__(self, logger: LoggerABC, db_context: DatabaseContextABC, servers: ServerRepositoryABC):
|
||||||
|
self._logger = logger
|
||||||
self._context = db_context
|
self._context = db_context
|
||||||
|
|
||||||
self._servers = servers
|
self._servers = servers
|
||||||
@ -17,6 +19,7 @@ class UserRepositoryService(UserRepositoryABC):
|
|||||||
|
|
||||||
def get_users(self) -> List[User]:
|
def get_users(self) -> List[User]:
|
||||||
users = List(User)
|
users = List(User)
|
||||||
|
self._logger.trace(__name__, f'Send SQL command: {User.get_select_all_string()}')
|
||||||
results = self._context.select(User.get_select_all_string())
|
results = self._context.select(User.get_select_all_string())
|
||||||
for result in results:
|
for result in results:
|
||||||
users.append(User(
|
users.append(User(
|
||||||
@ -29,6 +32,7 @@ class UserRepositoryService(UserRepositoryABC):
|
|||||||
return users
|
return users
|
||||||
|
|
||||||
def get_user_by_id(self, id: int) -> User:
|
def get_user_by_id(self, id: int) -> User:
|
||||||
|
self._logger.trace(__name__, f'Send SQL command: {User.get_select_by_id_string(id)}')
|
||||||
result = self._context.select(User.get_select_by_id_string(id))
|
result = self._context.select(User.get_select_by_id_string(id))
|
||||||
return User(
|
return User(
|
||||||
result[1],
|
result[1],
|
||||||
@ -38,6 +42,7 @@ class UserRepositoryService(UserRepositoryABC):
|
|||||||
)
|
)
|
||||||
|
|
||||||
def get_user_by_discord_id(self, discord_id: int) -> User:
|
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))
|
||||||
return User(
|
return User(
|
||||||
result[1],
|
result[1],
|
||||||
@ -45,3 +50,16 @@ class UserRepositoryService(UserRepositoryABC):
|
|||||||
self._servers.get_server_by_id(result[3]),
|
self._servers.get_server_by_id(result[3]),
|
||||||
id=result[0]
|
id=result[0]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def add_user(self, user: User) -> int:
|
||||||
|
self._logger.trace(__name__, f'Send SQL command: {user.insert_strin}')
|
||||||
|
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}')
|
||||||
|
self._context.cursor.execute(user.udpate_string)
|
||||||
|
|
||||||
|
def delete_user(self, user: User):
|
||||||
|
self._logger.trace(__name__, f'Send SQL command: {user.delete_string}')
|
||||||
|
self._context.cursor.execute(user.delete_string)
|
||||||
|
Reference in New Issue
Block a user