Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
Expand Down
14 changes: 14 additions & 0 deletions app/posts/public_router.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion app/posts/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
21 changes: 20 additions & 1 deletion app/posts/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions app/users/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
)


Expand Down
Loading