diff --git a/ara/domain/board/__init__.py b/ara/domain/board/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/ara/domain/board/board_domain.py b/ara/domain/board/board_domain.py new file mode 100644 index 00000000..0d42a58a --- /dev/null +++ b/ara/domain/board/board_domain.py @@ -0,0 +1,10 @@ +from ara.domain.board.type import BoardInfo +from ara.infra.board.board_infra import BoardInfra + + +class BoardDomain: + def __init__(self) -> None: + self.board_infra = BoardInfra() + + def get_all_boards(self) -> list[BoardInfo]: + return self.board_infra.get_all_boards() diff --git a/ara/domain/board/constants.py b/ara/domain/board/constants.py new file mode 100644 index 00000000..e743dc27 --- /dev/null +++ b/ara/domain/board/constants.py @@ -0,0 +1,13 @@ +from enum import IntEnum, IntFlag, auto + + +class BoardAccessPermissionType(IntEnum): + READ = 0 + WRITE = 1 + COMMENT = 2 + + +class NameType(IntFlag): + REGULAR = auto() + ANONYMOUS = auto() + REALNAME = auto() diff --git a/ara/domain/board/type.py b/ara/domain/board/type.py new file mode 100644 index 00000000..9a6ae77c --- /dev/null +++ b/ara/domain/board/type.py @@ -0,0 +1,45 @@ +from datetime import datetime + +from pydantic import BaseModel + +from ara.domain.board.constants import NameType + + +class BoardGroupInfo(BaseModel): + id: int + ko_name: str + en_name: str + slug: str + + +class TopicInfo(BaseModel): + slug: str + ko_name: str + en_name: str + + +class BoardInfo(BaseModel): + id: int + created_at: datetime + updated_at: datetime + deleted_at: datetime + slug: str + ko_name: str + en_name: str + # TODO(hyuk): 이거 group_has_access_permission 여기 잘보고 뭐 할거 판단 + read_access_mask: int + write_access_mask: int + comment_access_mask: int + is_readonly: bool + is_hidden: bool + name_type: NameType + is_school_communication: int + group: BoardGroupInfo + banner_image: str + ko_banner_description: str + en_banner_description: str + top_threshold: int + topics: list[TopicInfo] + + class Config: + arbitrary_types_allowed = True diff --git a/ara/infra/board/__init__.py b/ara/infra/board/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/ara/infra/board/board_infra.py b/ara/infra/board/board_infra.py new file mode 100644 index 00000000..3e9fbc5b --- /dev/null +++ b/ara/infra/board/board_infra.py @@ -0,0 +1,88 @@ +from apps.core.models.board import Board +from apps.core.models.board_group import BoardGroup +from ara.domain.board.type import BoardGroupInfo, BoardInfo, TopicInfo +from ara.infra.django_infra import AraDjangoInfra + + +class BoardGroupInfra(AraDjangoInfra[BoardGroup]): + def __init__(self) -> None: + super().__init__(Board) + + pass + + +class BoardInfra(AraDjangoInfra[Board]): + def __init__(self) -> None: + super().__init__(Board) + self.board_group_infra = BoardGroupInfra() + + def get_all_boards(self) -> list[BoardInfo]: + queryset = Board.objects.select_related("group").extra( + select={ + "topic_id": "topic.id", + "topic_slug": "topic.slug", + "topic_ko_name": "topic.ko_name", + "topic_en_name": "topic.en_name", + }, + tables=[ + "core_board` AS `b` LEFT OUTER JOIN `core_topic` AS `topic`" + " ON `b`.`id` = `topic`.`parent_board_id" + ], + where=["b.id = core_board.id"], + ) + + d: dict[BoardInfo, BoardInfo] = {} + for board_model in queryset: + board_info = self._to_board_without_topics(board_model) + if board_info.id in d.keys(): + d[board_info.id].topics.append( + TopicInfo( + slug=board_model.topic_slug, + ko_name=board_model.topic_ko_name, + en_name=board_model.topic_en_name, + ) + ) + else: + if board_model.topic_id is None: + d[board_info.id] = board_info + else: + board_info_with_topic = board_info + board_info_with_topic.topics.append( + TopicInfo( + slug=board_model.topic_slug, + ko_name=board_model.topic_ko_name, + en_name=board_model.topic_en_name, + ) + ) + d[board_info.id] = board_info_with_topic + + return [info for info in d.values()] + + def _to_board_without_topics(self, board: Board) -> BoardInfo: + return BoardInfo( + id=board.id, + created_at=board.created_at, + updated_at=board.updated_at, + deleted_at=board.deleted_at, + slug=board.slug, + ko_name=board.ko_name, + en_name=board.en_name, + read_access_mask=board.read_access_mask, + write_access_mask=board.write_access_mask, + comment_access_mask=board.comment_access_mask, + is_readonly=board.is_readonly, + is_hidden=board.is_hidden, + name_type=board.name_type, + is_school_communication=board.is_school_communication, + group=BoardGroupInfo( + id=board.group.id, + ko_name=board.group.ko_name, + en_name=board.group.en_name, + slug=board.group.slug, + ), + banner_image=board.banner_image.url, + ko_banner_description=board.ko_banner_description, + en_banner_description=board.en_banner_description, + top_threshold=board.top_threshold, + topics=[], + ) diff --git a/ara/service/board/__init__.py b/ara/service/board/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/ara/service/board/board_service.py b/ara/service/board/board_service.py new file mode 100644 index 00000000..98064084 --- /dev/null +++ b/ara/service/board/board_service.py @@ -0,0 +1,10 @@ +from ara.domain.board.board_domain import BoardDomain +from ara.domain.board.type import BoardInfo + + +class BoardService: + def __init__(self) -> None: + self.board_domain = BoardDomain() + + def get_all_boards(self) -> list[BoardInfo]: + return self.board_domain.get_all_boards()