Skip to content
Merged
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
13 changes: 13 additions & 0 deletions app/like/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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:
Expand Down
12 changes: 12 additions & 0 deletions app/posts/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
)
13 changes: 13 additions & 0 deletions app/posts/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
]
Loading