Improved data repository
This commit is contained in:
@@ -13,6 +13,7 @@ class ServerRepositoryService(ServerRepositoryABC):
|
||||
|
||||
def get_servers(self) -> 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())
|
||||
for result in results:
|
||||
servers.append(Server(
|
||||
@@ -23,6 +24,7 @@ class ServerRepositoryService(ServerRepositoryABC):
|
||||
return servers
|
||||
|
||||
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))
|
||||
return Server(
|
||||
result[1],
|
||||
@@ -30,8 +32,22 @@ class ServerRepositoryService(ServerRepositoryABC):
|
||||
)
|
||||
|
||||
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))
|
||||
return Server(
|
||||
result[1],
|
||||
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.logging import LoggerABC
|
||||
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.model.user import User
|
||||
|
||||
|
||||
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._servers = servers
|
||||
@@ -17,6 +19,7 @@ class UserRepositoryService(UserRepositoryABC):
|
||||
|
||||
def get_users(self) -> 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())
|
||||
for result in results:
|
||||
users.append(User(
|
||||
@@ -29,6 +32,7 @@ class UserRepositoryService(UserRepositoryABC):
|
||||
return users
|
||||
|
||||
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))
|
||||
return User(
|
||||
result[1],
|
||||
@@ -38,6 +42,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))
|
||||
return User(
|
||||
result[1],
|
||||
@@ -45,3 +50,16 @@ class UserRepositoryService(UserRepositoryABC):
|
||||
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}')
|
||||
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