Added get filtered servers #72
This commit is contained in:
@@ -3,6 +3,8 @@ from typing import Optional
|
||||
|
||||
from cpl_query.extension import List
|
||||
|
||||
from bot_api.filter.discord.server_select_criteria import ServerSelectCriteria
|
||||
from bot_data.filtered_result import FilteredResult
|
||||
from bot_data.model.server import Server
|
||||
|
||||
|
||||
@@ -13,6 +15,9 @@ class ServerRepositoryABC(ABC):
|
||||
|
||||
@abstractmethod
|
||||
def get_servers(self) -> List[Server]: pass
|
||||
|
||||
@abstractmethod
|
||||
def get_filtered_servers(self, criteria: ServerSelectCriteria) -> FilteredResult: pass
|
||||
|
||||
@abstractmethod
|
||||
def get_server_by_id(self, id: int) -> Server: pass
|
||||
|
@@ -3,8 +3,10 @@ from typing import Optional
|
||||
from cpl_core.database.context import DatabaseContextABC
|
||||
from cpl_query.extension import List
|
||||
|
||||
from bot_api.filter.discord.server_select_criteria import ServerSelectCriteria
|
||||
from bot_core.logging.database_logger import DatabaseLogger
|
||||
from bot_data.abc.server_repository_abc import ServerRepositoryABC
|
||||
from bot_data.filtered_result import FilteredResult
|
||||
from bot_data.model.server import Server
|
||||
|
||||
|
||||
@@ -28,6 +30,29 @@ class ServerRepositoryService(ServerRepositoryABC):
|
||||
|
||||
return servers
|
||||
|
||||
def get_filtered_servers(self, criteria: ServerSelectCriteria) -> FilteredResult:
|
||||
servers = self.get_servers()
|
||||
self._logger.trace(__name__, f'Send SQL command: {Server.get_select_all_string()}')
|
||||
query = servers
|
||||
|
||||
if criteria.name is not None and criteria.name != '':
|
||||
query = query.where(lambda x: criteria.name in x.first_name or x.first_name == criteria.name)
|
||||
|
||||
# sort
|
||||
if criteria.sort_column is not None and criteria.sort_column != '' and criteria.sort_direction is not None and criteria.sort_direction:
|
||||
crit_sort_direction = criteria.sort_direction.lower()
|
||||
if crit_sort_direction == "desc" or crit_sort_direction == "descending":
|
||||
query = query.order_by_descending(lambda x: getattr(x, criteria.sort_column))
|
||||
else:
|
||||
query = query.order_by(lambda x: getattr(x, criteria.sort_column))
|
||||
|
||||
result = FilteredResult()
|
||||
result.total_count = query.count()
|
||||
skip = criteria.page_size * criteria.page_index
|
||||
result.result = query.skip(skip).take(criteria.page_size)
|
||||
|
||||
return result
|
||||
|
||||
def get_server_by_id(self, server_id: int) -> Server:
|
||||
self._logger.trace(__name__, f'Send SQL command: {Server.get_select_by_id_string(server_id)}')
|
||||
result = self._context.select(Server.get_select_by_id_string(server_id))[0]
|
||||
|
Reference in New Issue
Block a user