Skip to content

Commit 3a7f253

Browse files
authored
Merge pull request #154 from ShowbilityProject/feat/post-likes-users
Feat/post likes users
2 parents 1793501 + f86b6b8 commit 3a7f253

3 files changed

Lines changed: 38 additions & 0 deletions

File tree

app/like/repository.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from app.database.deps import SessionDep
55
from app.like.tables import comment_like_table, post_like_table
6+
from app.users.models import User
67
from app.utils.dependency import dependency
78

89

@@ -51,6 +52,18 @@ async def remove_all_by_user(self, *, user_id: int):
5152
delete(post_like_table).where(post_like_table.c.user_id == user_id)
5253
)
5354

55+
async def find_users_by_post_id(self, *, post_id: int) -> list[User]:
56+
result = await self.session.scalars(
57+
select(User)
58+
.join(post_like_table, User.id == post_like_table.c.user_id)
59+
.where(
60+
post_like_table.c.post_id == post_id,
61+
User.deleted_at.is_(None),
62+
)
63+
.order_by(User.id)
64+
)
65+
return list(result.all())
66+
5467

5568
@dependency
5669
class CommentLikeRepository:

app/posts/router.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from app.core.router import create_router
1111
from app.storage.schemas import ImageUrl
1212
from app.storage.service import ImageMetadata, create_presigned_upload_url
13+
from app.users.schemas import UserRead
1314

1415
from .schemas import (
1516
PostCompactRead,
@@ -158,3 +159,14 @@ async def unlike_post(
158159
post_id=post_id,
159160
current_user=current_user,
160161
)
162+
163+
164+
@router.get("/{post_id}/liked-users", status_code=status.HTTP_200_OK)
165+
async def read_post_liked_users(
166+
post_service: PostService,
167+
post_id: int,
168+
current_user: CurrentUser,
169+
) -> list[UserRead]:
170+
return await post_service.get_post_liked_users(
171+
post_id=post_id, current_user=current_user
172+
)

app/posts/service.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from app.storage.deps import S3ClientDep
1313
from app.storage.service import get_image_metadata
1414
from app.users.models import User
15+
from app.users.schemas import UserRead
1516
from app.utils.dependency import dependency
1617

1718
from .models import Post, PostImage
@@ -280,3 +281,15 @@ async def unlike_post(self, *, post_id: int, current_user: User):
280281
user_id=current_user.id,
281282
post_id=post_id,
282283
)
284+
285+
async def get_post_liked_users(
286+
self, *, post_id: int, current_user: User
287+
) -> list[UserRead]:
288+
post = await self.post_repository.find_by_id(post_id=post_id)
289+
if not post:
290+
raise HTTPException(status_code=404, detail="게시물을 찾을 수 없습니다.")
291+
292+
users = await self.post_like_repository.find_users_by_post_id(post_id=post_id)
293+
return [
294+
UserRead.from_user(user, current_user_id=current_user.id) for user in users
295+
]

0 commit comments

Comments
 (0)