(api) change query param to include_archived

This commit is contained in:
maryhipp 2024-06-27 14:27:20 -04:00 committed by psychedelicious
parent 740bf80f3e
commit a63dbb2c2d
6 changed files with 25 additions and 18 deletions

View File

@ -118,13 +118,13 @@ async def list_boards(
all: Optional[bool] = Query(default=None, description="Whether to list all boards"), all: Optional[bool] = Query(default=None, description="Whether to list all boards"),
offset: Optional[int] = Query(default=None, description="The page offset"), offset: Optional[int] = Query(default=None, description="The page offset"),
limit: Optional[int] = Query(default=None, description="The number of boards per page"), limit: Optional[int] = Query(default=None, description="The number of boards per page"),
archived: bool = Query(default=False, description="Whether or not to include archived boards in list"), include_archived: bool = Query(default=False, description="Whether or not to include archived boards in list"),
) -> Union[OffsetPaginatedResults[BoardDTO], list[BoardDTO]]: ) -> Union[OffsetPaginatedResults[BoardDTO], list[BoardDTO]]:
"""Gets a list of boards""" """Gets a list of boards"""
if all: if all:
return ApiDependencies.invoker.services.boards.get_all(archived) return ApiDependencies.invoker.services.boards.get_all(include_archived)
elif offset is not None and limit is not None: elif offset is not None and limit is not None:
return ApiDependencies.invoker.services.boards.get_many(offset, limit, archived) return ApiDependencies.invoker.services.boards.get_many(offset, limit, include_archived)
else: else:
raise HTTPException( raise HTTPException(
status_code=400, status_code=400,

View File

@ -39,11 +39,13 @@ class BoardRecordStorageBase(ABC):
pass pass
@abstractmethod @abstractmethod
def get_many(self, offset: int = 0, limit: int = 10, archived: bool = False) -> OffsetPaginatedResults[BoardRecord]: def get_many(
self, offset: int = 0, limit: int = 10, include_archived: bool = False
) -> OffsetPaginatedResults[BoardRecord]:
"""Gets many board records.""" """Gets many board records."""
pass pass
@abstractmethod @abstractmethod
def get_all(self, archived: bool = False) -> list[BoardRecord]: def get_all(self, include_archived: bool = False) -> list[BoardRecord]:
"""Gets all board records.""" """Gets all board records."""
pass pass

View File

@ -22,7 +22,7 @@ class BoardRecord(BaseModelExcludeNull):
"""The updated timestamp of the image.""" """The updated timestamp of the image."""
cover_image_name: Optional[str] = Field(default=None, description="The name of the cover image of the board.") cover_image_name: Optional[str] = Field(default=None, description="The name of the cover image of the board.")
"""The name of the cover image of the board.""" """The name of the cover image of the board."""
archived: Optional[bool] = Field(default=None, description="Whether or not the board is archived.") archived: bool = Field(default=False, description="Whether or not the board is archived.")
"""Whether or not the board is archived.""" """Whether or not the board is archived."""

View File

@ -138,14 +138,15 @@ class SqliteBoardRecordStorage(BoardRecordStorageBase):
self._conn.commit() self._conn.commit()
except sqlite3.Error as e: except sqlite3.Error as e:
print(e)
self._conn.rollback() self._conn.rollback()
raise BoardRecordSaveException from e raise BoardRecordSaveException from e
finally: finally:
self._lock.release() self._lock.release()
return self.get(board_id) return self.get(board_id)
def get_many(self, offset: int = 0, limit: int = 10, archived: bool = False) -> OffsetPaginatedResults[BoardRecord]: def get_many(
self, offset: int = 0, limit: int = 10, include_archived: bool = False
) -> OffsetPaginatedResults[BoardRecord]:
try: try:
self._lock.acquire() self._lock.acquire()
@ -159,7 +160,7 @@ class SqliteBoardRecordStorage(BoardRecordStorageBase):
""" """
# Determine archived filter condition # Determine archived filter condition
if archived: if include_archived:
archived_filter = "" archived_filter = ""
else: else:
archived_filter = "WHERE archived = 0" archived_filter = "WHERE archived = 0"
@ -173,7 +174,7 @@ class SqliteBoardRecordStorage(BoardRecordStorageBase):
boards = [deserialize_board_record(dict(r)) for r in result] boards = [deserialize_board_record(dict(r)) for r in result]
# Determine count query # Determine count query
if archived: if include_archived:
count_query = """ count_query = """
SELECT COUNT(*) SELECT COUNT(*)
FROM boards; FROM boards;
@ -198,7 +199,7 @@ class SqliteBoardRecordStorage(BoardRecordStorageBase):
finally: finally:
self._lock.release() self._lock.release()
def get_all(self, archived: bool = False) -> list[BoardRecord]: def get_all(self, include_archived: bool = False) -> list[BoardRecord]:
try: try:
self._lock.acquire() self._lock.acquire()
@ -209,7 +210,7 @@ class SqliteBoardRecordStorage(BoardRecordStorageBase):
ORDER BY created_at DESC ORDER BY created_at DESC
""" """
if archived: if include_archived:
archived_filter = "" archived_filter = ""
else: else:
archived_filter = "WHERE archived = 0" archived_filter = "WHERE archived = 0"

View File

@ -43,11 +43,13 @@ class BoardServiceABC(ABC):
pass pass
@abstractmethod @abstractmethod
def get_many(self, offset: int = 0, limit: int = 10, archived: bool = False) -> OffsetPaginatedResults[BoardDTO]: def get_many(
self, offset: int = 0, limit: int = 10, include_archived: bool = False
) -> OffsetPaginatedResults[BoardDTO]:
"""Gets many boards.""" """Gets many boards."""
pass pass
@abstractmethod @abstractmethod
def get_all(self, archived: bool = False) -> list[BoardDTO]: def get_all(self, include_archived: bool = False) -> list[BoardDTO]:
"""Gets all boards.""" """Gets all boards."""
pass pass

View File

@ -48,8 +48,10 @@ class BoardService(BoardServiceABC):
def delete(self, board_id: str) -> None: def delete(self, board_id: str) -> None:
self.__invoker.services.board_records.delete(board_id) self.__invoker.services.board_records.delete(board_id)
def get_many(self, offset: int = 0, limit: int = 10, archived: bool = False) -> OffsetPaginatedResults[BoardDTO]: def get_many(
board_records = self.__invoker.services.board_records.get_many(offset, limit, archived) self, offset: int = 0, limit: int = 10, include_archived: bool = False
) -> OffsetPaginatedResults[BoardDTO]:
board_records = self.__invoker.services.board_records.get_many(offset, limit, include_archived)
board_dtos = [] board_dtos = []
for r in board_records.items: for r in board_records.items:
cover_image = self.__invoker.services.image_records.get_most_recent_image_for_board(r.board_id) cover_image = self.__invoker.services.image_records.get_most_recent_image_for_board(r.board_id)
@ -63,8 +65,8 @@ class BoardService(BoardServiceABC):
return OffsetPaginatedResults[BoardDTO](items=board_dtos, offset=offset, limit=limit, total=len(board_dtos)) return OffsetPaginatedResults[BoardDTO](items=board_dtos, offset=offset, limit=limit, total=len(board_dtos))
def get_all(self, archived: bool = False) -> list[BoardDTO]: def get_all(self, include_archived: bool = False) -> list[BoardDTO]:
board_records = self.__invoker.services.board_records.get_all(archived) board_records = self.__invoker.services.board_records.get_all(include_archived)
board_dtos = [] board_dtos = []
for r in board_records: for r in board_records:
cover_image = self.__invoker.services.image_records.get_most_recent_image_for_board(r.board_id) cover_image = self.__invoker.services.image_records.get_most_recent_image_for_board(r.board_id)