Example #1
0
    def execute(
        self, dto: DeletePostDto
    ) -> Union[UseCaseSuccessOutput, UseCaseFailureOutput]:
        is_post_owner = self._is_post_owner(dto=dto)
        if not is_post_owner:
            return UseCaseFailureOutput(type=FailureType.INVALID_REQUEST_ERROR)

        post = self._board_repo.delete_post(dto=dto)
        if not post:
            return UseCaseFailureOutput(type=FailureType.SYSTEM_ERROR)
        return UseCaseSuccessOutput(value=post)
Example #2
0
    def execute(self, dto: GetPostDto):
        post = self._board_repo.get_post(post_id=dto.post_id)
        if not post:
            return UseCaseFailureOutput(FailureType.NOT_FOUND_ERROR)
        try:
            self._board_repo.add_read_count(post_id=dto.post_id)
        except Exception as e:
            logger.error(
                f"[GetPostUseCase][execute] post_id : {dto.post_id} error : {e}"
            )
            return UseCaseFailureOutput(FailureType.SYSTEM_ERROR)
        post = self._board_repo.get_post(post_id=dto.post_id)

        return UseCaseSuccessOutput(value=post)
Example #3
0
 def execute(
         self, dto: GetUserDto
 ) -> Union[UseCaseSuccessOutput, UseCaseFailureOutput]:
     user = self._user_repo.get_user(user_id=dto.user_id)
     if not user:
         return UseCaseFailureOutput(type=FailureType.NOT_FOUND_ERROR)
     return UseCaseSuccessOutput(value=user)
Example #4
0
 def transform(self, output: Union[UseCaseSuccessOutput, UseCaseFailureOutput]):
     if isinstance(output, UseCaseSuccessOutput):
         v = output.value
         try:
             schema = PostReportResponseSchema(
                 id=v.id,
                 post_id=v.post_id,
                 report_user_id=v.report_user_id,
                 status=v.status,
                 context=v.context,
                 confirm_admin_id=v.confirm_admin_id,
                 is_system_report=v.is_system_report,
                 created_at=v.created_at,
                 updated_at=v.updated_at,
             )
         except ValidationError as e:
             logger.error(f"[AuthenticationPresenter][transform] error : {e}")
             return failure_response(
                 UseCaseFailureOutput(
                     type=FailureType.SYSTEM_ERROR,
                     message="response schema validation error",
                 )
             )
         result = {
             "data": {"post_report": schema.dict()},
             "meta": {"cursor": output.meta},
         }
         return success_response(result=result)
     elif isinstance(output, UseCaseFailureOutput):
         return failure_response(output=output)
Example #5
0
def get_post_view(post_id):
    dto = GetPostRequest(post_id=post_id).validate_request_and_make_dto()
    if not dto:
        return failure_response(
            UseCaseFailureOutput(type=FailureType.INVALID_REQUEST_ERROR))

    return PostPresenter().transform(GetPostUseCase().execute(dto=dto))
Example #6
0
def create_post_report_view():
    dto = CreatePostReportRequest(
        **request.get_json()).validate_request_and_make_dto()
    if not dto:
        return failure_response(
            UseCaseFailureOutput(type=FailureType.INVALID_REQUEST_ERROR))

    return PostReportPresenter().transform(
        CreatePostReportUseCase().execute(dto=dto))
Example #7
0
def get_like_post_list_view():
    dto = GetLikePostListRequest(**request.get_json(),
                                 ).validate_request_and_make_dto()
    if not dto:
        return failure_response(
            UseCaseFailureOutput(type=FailureType.INVALID_REQUEST_ERROR))

    return PostListPresenter().transform(
        GetLikePostListUseCase().execute(dto=dto))
Example #8
0
def create_post_view():
    dto = CreatePostRequest(
        **request.form.to_dict(),
        files=request.files.getlist("files"),
    ).validate_request_and_make_dto()
    if not dto:
        return failure_response(
            UseCaseFailureOutput(type=FailureType.INVALID_REQUEST_ERROR))

    return PostPresenter().transform(CreatePostUseCase().execute(dto=dto))
Example #9
0
    def execute(self, dto: CreatePostReportDto):
        is_post_exist = self._is_post_exist(post_id=dto.post_id)
        if not is_post_exist:
            return UseCaseFailureOutput(type=FailureType.NOT_FOUND_ERROR)

        post_report = self.report_repo.get_post_report(
            report_user_id=dto.report_user_id, post_id=dto.post_id)
        if post_report:
            return UseCaseFailureOutput(type=FailureType.ALREADY_EXIST)

        post_report = self.report_repo.create_post_report(dto=dto)
        if not post_report:
            return UseCaseFailureOutput(type=FailureType.SYSTEM_ERROR)

        post_report_count = self._add_report_count(post_id=dto.post_id)

        if post_report_count >= PostReportEnum.REPORT_MAX_COUNT.value:
            self._block_post(post_id=dto.post_id)

        return UseCaseSuccessOutput(value=post_report)
Example #10
0
    def execute(self, dto: GetLikePostListDto):
        user = self._get_user(dto.user_id)
        if not user:
            return UseCaseFailureOutput(type=FailureType.NOT_FOUND_ERROR)

        like_post_list = self._board_repo.get_like_post_list(
            user_id=dto.user_id, previous_post_id=dto.previous_post_id)

        return UseCaseSuccessOutput(
            value=like_post_list,
            meta=self._make_cursor(last_post_id=like_post_list[-1].
                                   id if like_post_list else None),
        )
Example #11
0
    def execute(
        self, dto: UpdatePostDto
    ) -> Union[UseCaseSuccessOutput, UseCaseFailureOutput]:
        is_post_owner = self._is_post_owner(dto=dto)
        if not is_post_owner:
            return UseCaseFailureOutput(type=FailureType.INVALID_REQUEST_ERROR)

        post = self._board_repo.update_post(dto=dto)
        if not post:
            return UseCaseFailureOutput(type=FailureType.SYSTEM_ERROR)

        attachments = self._board_repo.get_attachments(post_id=post.id)
        if attachments:
            self._board_repo.delete_attachments(post_id=post.id)
        attachments = []
        if dto.file_type == AttachmentEnum.PICTURE.value:
            attachments = self._upload_pictures(dto=dto, post_id=post.id)
            if attachments == False:
                return UseCaseFailureOutput(type=FailureType.SYSTEM_ERROR)
        post.attachments = attachments

        return UseCaseSuccessOutput(value=post)
Example #12
0
    def execute(
        self, dto: UpdateUserDto
    ) -> Union[UseCaseSuccessOutput, UseCaseFailureOutput]:
        user = self._user_repo.get_user(user_id=dto.user_id)
        if not user:
            return UseCaseFailureOutput(type=FailureType.NOT_FOUND_ERROR)

        user = self._user_repo.update_user(
            user_id=dto.user_id,
            nickname=dto.nickname,
            status=dto.status,
            region_id=dto.region_id,
        )
        if not user:
            return UseCaseFailureOutput(type=FailureType.SYSTEM_ERROR)

        if dto.files:
            attachment = self._upload_picture(dto=dto,
                                              user_profile_id=user.profile_id)
            if not attachment:
                return UseCaseFailureOutput(FailureType.SYSTEM_ERROR)

        return UseCaseSuccessOutput(value=user)
Example #13
0
    def execute(
        self, dto: GetPostListDto
    ) -> Union[UseCaseSuccessOutput, UseCaseFailureOutput]:
        is_region_group_exist = self._get_region_group(id=dto.region_group_id)
        if not is_region_group_exist:
            return UseCaseFailureOutput(FailureType.NOT_FOUND_ERROR)

        post_list = self._board_repo.get_post_list(
            region_group_id=dto.region_group_id,
            previous_post_id=dto.previous_post_id,
            title=dto.title,
            category_ids=dto.category_ids,
            status=dto.status,
        )

        return UseCaseSuccessOutput(
            value=post_list,
            meta=self._make_cursor(
                last_post_id=post_list[-1].id if post_list else None),
        )
Example #14
0
 def transform(self, output: Union[UseCaseSuccessOutput,
                                   UseCaseFailureOutput]):
     if isinstance(output, UseCaseSuccessOutput):
         value = output.value
         try:
             schema = PostResponseSchema(post=value)
         except ValidationError as e:
             logger.error(f"[PostPresenter][transform] error : {e}")
             return failure_response(
                 UseCaseFailureOutput(
                     type=FailureType.SYSTEM_ERROR,
                     message="response schema validation error",
                 ))
         result = {
             "data": schema.dict(),
             "meta": output.meta,
         }
         return success_response(result=result)
     elif isinstance(output, UseCaseFailureOutput):
         return failure_response(output=output)
 def transform(
     self,
     response: UseCaseSuccessOutput,
 ):
     try:
         schema = AuthenticationResponseSchema(type=response.type)
     except ValidationError as e:
         logger.error(f"[AuthenticationPresenter][transform] error : {e}")
         return failure_response(
             UseCaseFailureOutput(
                 type=FailureType.SYSTEM_ERROR,
                 message="response schema validation error",
             ))
     result = {
         "data": {
             "result": schema.dict()
         },
         "meta": {
             "cursor": response.meta
         },
     }
     return success_response(result=result)
Example #16
0
    def execute(self, dto: LikePostDto):
        user = self._get_user(dto.user_id)
        if not user:
            return UseCaseFailureOutput(type=FailureType.NOT_FOUND_ERROR)

        is_post_exist = self._is_post_exist(post_id=dto.post_id)
        if not is_post_exist:
            return UseCaseFailureOutput(type=FailureType.NOT_FOUND_ERROR)

        post_like_state = self._board_repo.get_post_like_state(
            user_id=dto.user_id, post_id=dto.post_id)
        if not post_like_state:
            post_like_state = self._board_repo.create_post_like_state(
                user_id=dto.user_id, post_id=dto.post_id)
            if not post_like_state:
                return UseCaseFailureOutput(FailureType.SYSTEM_ERROR)

            post_like_count = self._board_repo.update_post_like_count(
                post_id=dto.post_id, count=PostLikeCountEnum.UP.value)
            if not post_like_count:
                return UseCaseFailureOutput(FailureType.SYSTEM_ERROR)
        else:
            state = post_like_state.state
            to_be_adopted_post_like_state = self._get_to_be_adopted_post_like_state(
                state=state)

            post_like_state = self._board_repo.update_post_like_state(
                user_id=dto.user_id,
                post_id=dto.post_id,
                state=to_be_adopted_post_like_state,
            )
            if not post_like_state:
                return UseCaseFailureOutput(FailureType.SYSTEM_ERROR)

            post_like_count = self._update_post_like_count(state=state,
                                                           dto=dto)
            if not post_like_count:
                return UseCaseFailureOutput(FailureType.SYSTEM_ERROR)

            post_like_state = self._board_repo.get_post_like_state(
                user_id=dto.user_id, post_id=dto.post_id)

        post = self._board_repo.get_post(post_id=dto.post_id)
        post.post_like_state = post_like_state.state

        return UseCaseSuccessOutput(value=post)
Example #17
0
    def execute(
        self, dto: CreatePostDto
    ) -> Union[UseCaseSuccessOutput, UseCaseFailureOutput]:
        user = self._get_user(dto.user_id)
        if not user:
            return UseCaseFailureOutput(type=FailureType.NOT_FOUND_ERROR)

        post = self._board_repo.create_post(dto=dto)
        if not post:
            return UseCaseFailureOutput(type=FailureType.SYSTEM_ERROR)

        attachments = []
        if dto.file_type == AttachmentEnum.PICTURE.value:
            attachments = self._upload_pictures(dto=dto, post_id=post.id)
            if attachments == False:
                return UseCaseFailureOutput(type=FailureType.SYSTEM_ERROR)

        post_like_count = self._board_repo.create_post_like_count(
            post_id=post.id)
        if not post_like_count:
            return UseCaseFailureOutput(type=FailureType.SYSTEM_ERROR)

        post.post_like_count = post_like_count.count
        post.attachments = attachments

        # 키워드 노티 전송 대상 조회
        target_user_group: List[dict] = self._get_target_user_for_notification(
        )

        # todo. 내부함수로 리팩토링
        if target_user_group:
            notification_list = list()
            for target_user in target_user_group:
                if (post.title.find(target_user["keyword_1"]) >= 0
                        or post.title.find(target_user["keyword_2"]) >= 0
                        or post.title.find(target_user["keyword_3"]) >= 0):
                    message_dto = MessageDto(
                        token=target_user.get("token"),
                        post_id=post.id,
                        user_id=target_user.get("user_id"),
                        category=CategoryEnum.KEYWORD.value,
                        type=TypeEnum.ALL.value,
                        title=TitleEnum.KEYWORD.value,
                        body=BodyEnum.KEYWORD.value,
                    )
                    notification_message = NotificationMessageConverter.to_dict(
                        message_dto)

                    notification_history = NotificationHistoryDto(
                        user_id=target_user.get("user_id"),
                        status=StatusEnum.PENDING.value,
                        type=TypeEnum.ALL.value,
                        category=CategoryEnum.KEYWORD.value,
                        message=notification_message,
                    )

                    notification_list.append(notification_history)

            if notification_list:
                self._create_notification_history(notification_list)

        return UseCaseSuccessOutput(value=post)