2023-06-13 18:51:20 +00:00
|
|
|
from abc import ABC, abstractmethod
|
2023-06-14 14:07:20 +00:00
|
|
|
from logging import Logger
|
2023-08-18 14:57:18 +00:00
|
|
|
|
2023-06-14 14:07:20 +00:00
|
|
|
from invokeai.app.services.board_image_record_storage import BoardImageRecordStorageBase
|
|
|
|
from invokeai.app.services.board_images import board_record_to_dto
|
2023-08-18 14:57:18 +00:00
|
|
|
from invokeai.app.services.board_record_storage import BoardChanges, BoardRecordStorageBase
|
|
|
|
from invokeai.app.services.image_record_storage import ImageRecordStorageBase, OffsetPaginatedResults
|
2023-06-14 18:20:23 +00:00
|
|
|
from invokeai.app.services.models.board_record import BoardDTO
|
2023-06-14 14:07:20 +00:00
|
|
|
from invokeai.app.services.urls import UrlServiceBase
|
|
|
|
|
|
|
|
|
|
|
|
class BoardServiceABC(ABC):
|
|
|
|
"""High-level service for board management."""
|
2023-06-13 18:51:20 +00:00
|
|
|
|
|
|
|
@abstractmethod
|
2023-06-14 14:07:20 +00:00
|
|
|
def create(
|
|
|
|
self,
|
|
|
|
board_name: str,
|
|
|
|
) -> BoardDTO:
|
|
|
|
"""Creates a board."""
|
2023-06-13 18:51:20 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
@abstractmethod
|
2023-06-14 14:07:20 +00:00
|
|
|
def get_dto(
|
2023-06-13 18:51:20 +00:00
|
|
|
self,
|
2023-06-14 14:07:20 +00:00
|
|
|
board_id: str,
|
|
|
|
) -> BoardDTO:
|
|
|
|
"""Gets a board."""
|
2023-06-13 18:51:20 +00:00
|
|
|
pass
|
|
|
|
|
2023-06-14 14:07:20 +00:00
|
|
|
@abstractmethod
|
|
|
|
def update(
|
|
|
|
self,
|
|
|
|
board_id: str,
|
|
|
|
changes: BoardChanges,
|
|
|
|
) -> BoardDTO:
|
|
|
|
"""Updates a board."""
|
2023-06-13 21:08:04 +00:00
|
|
|
pass
|
|
|
|
|
2023-06-14 14:07:20 +00:00
|
|
|
@abstractmethod
|
|
|
|
def delete(
|
|
|
|
self,
|
|
|
|
board_id: str,
|
|
|
|
) -> None:
|
|
|
|
"""Deletes a board."""
|
|
|
|
pass
|
|
|
|
|
|
|
|
@abstractmethod
|
2023-06-13 21:08:04 +00:00
|
|
|
def get_many(
|
|
|
|
self,
|
2023-06-14 14:07:20 +00:00
|
|
|
offset: int = 0,
|
|
|
|
limit: int = 10,
|
|
|
|
) -> OffsetPaginatedResults[BoardDTO]:
|
|
|
|
"""Gets many boards."""
|
2023-06-13 21:08:04 +00:00
|
|
|
pass
|
|
|
|
|
2023-06-20 12:51:05 +00:00
|
|
|
@abstractmethod
|
|
|
|
def get_all(
|
|
|
|
self,
|
|
|
|
) -> list[BoardDTO]:
|
|
|
|
"""Gets all boards."""
|
|
|
|
pass
|
|
|
|
|
2023-06-13 18:51:20 +00:00
|
|
|
|
2023-06-14 14:07:20 +00:00
|
|
|
class BoardServiceDependencies:
|
|
|
|
"""Service dependencies for the BoardService."""
|
2023-06-13 18:51:20 +00:00
|
|
|
|
2023-06-14 14:07:20 +00:00
|
|
|
board_image_records: BoardImageRecordStorageBase
|
|
|
|
board_records: BoardRecordStorageBase
|
|
|
|
image_records: ImageRecordStorageBase
|
|
|
|
urls: UrlServiceBase
|
|
|
|
logger: Logger
|
2023-06-13 18:51:20 +00:00
|
|
|
|
2023-06-14 14:07:20 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
board_image_record_storage: BoardImageRecordStorageBase,
|
|
|
|
image_record_storage: ImageRecordStorageBase,
|
|
|
|
board_record_storage: BoardRecordStorageBase,
|
|
|
|
url: UrlServiceBase,
|
|
|
|
logger: Logger,
|
|
|
|
):
|
|
|
|
self.board_image_records = board_image_record_storage
|
|
|
|
self.image_records = image_record_storage
|
|
|
|
self.board_records = board_record_storage
|
|
|
|
self.urls = url
|
|
|
|
self.logger = logger
|
2023-06-13 18:51:20 +00:00
|
|
|
|
|
|
|
|
2023-06-14 14:07:20 +00:00
|
|
|
class BoardService(BoardServiceABC):
|
|
|
|
_services: BoardServiceDependencies
|
|
|
|
|
|
|
|
def __init__(self, services: BoardServiceDependencies):
|
|
|
|
self._services = services
|
|
|
|
|
|
|
|
def create(
|
2023-06-13 18:51:20 +00:00
|
|
|
self,
|
|
|
|
board_name: str,
|
2023-06-14 14:07:20 +00:00
|
|
|
) -> BoardDTO:
|
|
|
|
board_record = self._services.board_records.save(board_name)
|
|
|
|
return board_record_to_dto(board_record, None, 0)
|
|
|
|
|
|
|
|
def get_dto(self, board_id: str) -> BoardDTO:
|
|
|
|
board_record = self._services.board_records.get(board_id)
|
2023-06-20 12:51:05 +00:00
|
|
|
cover_image = self._services.image_records.get_most_recent_image_for_board(board_record.board_id)
|
|
|
|
if cover_image:
|
2023-06-16 17:21:55 +00:00
|
|
|
cover_image_name = cover_image.image_name
|
|
|
|
else:
|
|
|
|
cover_image_name = None
|
2023-06-14 14:07:20 +00:00
|
|
|
image_count = self._services.board_image_records.get_image_count_for_board(board_id)
|
2023-06-16 17:21:55 +00:00
|
|
|
return board_record_to_dto(board_record, cover_image_name, image_count)
|
2023-06-13 19:56:09 +00:00
|
|
|
|
2023-06-14 14:07:20 +00:00
|
|
|
def update(
|
|
|
|
self,
|
|
|
|
board_id: str,
|
|
|
|
changes: BoardChanges,
|
|
|
|
) -> BoardDTO:
|
|
|
|
board_record = self._services.board_records.update(board_id, changes)
|
2023-06-20 12:51:05 +00:00
|
|
|
cover_image = self._services.image_records.get_most_recent_image_for_board(board_record.board_id)
|
|
|
|
if cover_image:
|
2023-06-16 17:21:55 +00:00
|
|
|
cover_image_name = cover_image.image_name
|
|
|
|
else:
|
|
|
|
cover_image_name = None
|
2023-06-20 12:51:05 +00:00
|
|
|
|
2023-06-14 14:07:20 +00:00
|
|
|
image_count = self._services.board_image_records.get_image_count_for_board(board_id)
|
2023-06-16 17:21:55 +00:00
|
|
|
return board_record_to_dto(board_record, cover_image_name, image_count)
|
2023-06-14 14:07:20 +00:00
|
|
|
|
|
|
|
def delete(self, board_id: str) -> None:
|
|
|
|
self._services.board_records.delete(board_id)
|
2023-06-13 21:08:04 +00:00
|
|
|
|
2023-06-14 14:07:20 +00:00
|
|
|
def get_many(self, offset: int = 0, limit: int = 10) -> OffsetPaginatedResults[BoardDTO]:
|
|
|
|
board_records = self._services.board_records.get_many(offset, limit)
|
|
|
|
board_dtos = []
|
|
|
|
for r in board_records.items:
|
2023-06-20 12:51:05 +00:00
|
|
|
cover_image = self._services.image_records.get_most_recent_image_for_board(r.board_id)
|
|
|
|
if cover_image:
|
2023-06-16 17:21:55 +00:00
|
|
|
cover_image_name = cover_image.image_name
|
2023-06-15 15:31:14 +00:00
|
|
|
else:
|
2023-06-16 17:21:55 +00:00
|
|
|
cover_image_name = None
|
2023-06-15 15:31:14 +00:00
|
|
|
|
2023-06-14 14:07:20 +00:00
|
|
|
image_count = self._services.board_image_records.get_image_count_for_board(r.board_id)
|
2023-06-16 17:21:55 +00:00
|
|
|
board_dtos.append(board_record_to_dto(r, cover_image_name, image_count))
|
2023-06-14 14:07:20 +00:00
|
|
|
|
|
|
|
return OffsetPaginatedResults[BoardDTO](items=board_dtos, offset=offset, limit=limit, total=len(board_dtos))
|
2023-06-20 12:51:05 +00:00
|
|
|
|
|
|
|
def get_all(self) -> list[BoardDTO]:
|
|
|
|
board_records = self._services.board_records.get_all()
|
|
|
|
board_dtos = []
|
|
|
|
for r in board_records:
|
|
|
|
cover_image = self._services.image_records.get_most_recent_image_for_board(r.board_id)
|
|
|
|
if cover_image:
|
|
|
|
cover_image_name = cover_image.image_name
|
|
|
|
else:
|
|
|
|
cover_image_name = None
|
|
|
|
|
|
|
|
image_count = self._services.board_image_records.get_image_count_for_board(r.board_id)
|
|
|
|
board_dtos.append(board_record_to_dto(r, cover_image_name, image_count))
|
|
|
|
|
|
|
|
return board_dtos
|