diff --git a/app/api.py b/app/api.py index b7f6260..6f106d5 100644 --- a/app/api.py +++ b/app/api.py @@ -4,6 +4,7 @@ from app.auth.deps import get_current_user from app.auth.router import router as auth_router from app.core.router import create_router +from app.posts.public_router import router as posts_public_router from app.posts.router import router as posts_router from app.tags.router import router as tags_router from app.users.router import router as users_router @@ -12,6 +13,7 @@ public_router = create_router(prefix="/public") public_router.include_router(auth_router, prefix="/auth", tags=["Auth"]) +public_router.include_router(posts_public_router, prefix="/posts", tags=["Posts"]) private_router = create_router( prefix="/private", diff --git a/app/posts/public_router.py b/app/posts/public_router.py new file mode 100644 index 0000000..35c7ee2 --- /dev/null +++ b/app/posts/public_router.py @@ -0,0 +1,14 @@ +from app.core.router import create_router + +from .schemas import PostRead +from .service import PostService + +router = create_router() + + +@router.get("/{post_id}") +async def read_post_public( + post_id: int, + post_service: PostService, +) -> PostRead: + return await post_service.get_post_by_id_simple(post_id=post_id) diff --git a/app/posts/schemas.py b/app/posts/schemas.py index e91d54f..8188a61 100644 --- a/app/posts/schemas.py +++ b/app/posts/schemas.py @@ -90,7 +90,7 @@ def from_post( like_count: int, comment_count: int, is_liked: bool = False, - current_user_id: int, + current_user_id: int | None = None, ): return cls( id=post.id, diff --git a/app/posts/service.py b/app/posts/service.py index 28d5a6f..16c3aba 100644 --- a/app/posts/service.py +++ b/app/posts/service.py @@ -26,13 +26,32 @@ class PostService: post_like_repository: PostLikeRepository comment_repository: CommentRepository + async def get_post_by_id_simple(self, *, post_id: int) -> PostRead: + """Public용 - 좋아요 정보 없이 기본 정보만 조회""" + post = await self.post_repository.find_by_id(post_id=post_id) + + if not post: + raise HTTPException(status_code=404, detail="게시물을 찾을 수 없습니다.") + + like_count, comment_count = await asyncio.gather( + self.post_like_repository.count_by_id(post_id=post_id), + self.comment_repository.count_by_post_id(post_id=post_id), + ) + + return PostRead.from_post( + post, + like_count=like_count, + comment_count=comment_count, + ) + async def get_post_by_id(self, *, post_id: int, current_user: User) -> PostRead: + """로그인 사용자용 - 좋아요 정보 포함 조회""" post = await self.post_repository.find_by_id(post_id=post_id) if not post: raise HTTPException(status_code=404, detail="게시물을 찾을 수 없습니다.") - [like_count, comment_count, is_liked] = await asyncio.gather( + like_count, comment_count, is_liked = await asyncio.gather( self.post_like_repository.count_by_id(post_id=post_id), self.comment_repository.count_by_post_id(post_id=post_id), self.post_like_repository.is_liked( diff --git a/app/users/schemas.py b/app/users/schemas.py index 3c43a12..52b5d99 100644 --- a/app/users/schemas.py +++ b/app/users/schemas.py @@ -34,7 +34,7 @@ class UserRead(APISchema): is_me: bool @classmethod - def from_user(cls, user: User, *, current_user_id: int): + def from_user(cls, user: User, *, current_user_id: int | None = None): return cls( handle=user.handle, nickname=user.nickname, @@ -43,7 +43,7 @@ def from_user(cls, user: User, *, current_user_id: int): if user.profile_image is not None else None ), - is_me=current_user_id == user.id, + is_me=current_user_id == user.id if current_user_id is not None else False, )