diff --git a/app/like/repository.py b/app/like/repository.py index 2bc3a04b..dab958f8 100644 --- a/app/like/repository.py +++ b/app/like/repository.py @@ -3,6 +3,7 @@ from app.database.deps import SessionDep from app.like.tables import comment_like_table, post_like_table +from app.users.models import User from app.utils.dependency import dependency @@ -51,6 +52,18 @@ async def remove_all_by_user(self, *, user_id: int): delete(post_like_table).where(post_like_table.c.user_id == user_id) ) + async def find_users_by_post_id(self, *, post_id: int) -> list[User]: + result = await self.session.scalars( + select(User) + .join(post_like_table, User.id == post_like_table.c.user_id) + .where( + post_like_table.c.post_id == post_id, + User.deleted_at.is_(None), + ) + .order_by(User.id) + ) + return list(result.all()) + @dependency class CommentLikeRepository: diff --git a/app/posts/router.py b/app/posts/router.py index 2315bf3d..5e955bd4 100644 --- a/app/posts/router.py +++ b/app/posts/router.py @@ -10,6 +10,7 @@ from app.core.router import create_router from app.storage.schemas import ImageUrl from app.storage.service import ImageMetadata, create_presigned_upload_url +from app.users.schemas import UserRead from .schemas import ( PostCompactRead, @@ -158,3 +159,14 @@ async def unlike_post( post_id=post_id, current_user=current_user, ) + + +@router.get("/{post_id}/liked-users", status_code=status.HTTP_200_OK) +async def read_post_liked_users( + post_service: PostService, + post_id: int, + current_user: CurrentUser, +) -> list[UserRead]: + return await post_service.get_post_liked_users( + post_id=post_id, current_user=current_user + ) diff --git a/app/posts/service.py b/app/posts/service.py index 3539db49..392bc94e 100644 --- a/app/posts/service.py +++ b/app/posts/service.py @@ -12,6 +12,7 @@ from app.storage.deps import S3ClientDep from app.storage.service import get_image_metadata from app.users.models import User +from app.users.schemas import UserRead from app.utils.dependency import dependency from .models import Post, PostImage @@ -280,3 +281,15 @@ async def unlike_post(self, *, post_id: int, current_user: User): user_id=current_user.id, post_id=post_id, ) + + async def get_post_liked_users( + self, *, post_id: int, current_user: User + ) -> list[UserRead]: + post = await self.post_repository.find_by_id(post_id=post_id) + if not post: + raise HTTPException(status_code=404, detail="게시물을 찾을 수 없습니다.") + + users = await self.post_like_repository.find_users_by_post_id(post_id=post_id) + return [ + UserRead.from_user(user, current_user_id=current_user.id) for user in users + ]