Skip to content

Commit 343b423

Browse files
committed
Feat: 게시글 단건 조회 시 좋아요/북마크 여부 추가
1 parent 2d737e7 commit 343b423

File tree

5 files changed

+41
-8
lines changed

5 files changed

+41
-8
lines changed

src/main/java/com/back/domain/board/comment/controller/CommentControllerDocs.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,8 @@ ResponseEntity<RsData<Void>> deleteComment(
539539
"parentId": 25,
540540
"author": {
541541
"id": 7,
542-
"nickname": "이몽룡"
542+
"nickname": "이몽룡",
543+
"profileImageUrl": null
543544
},
544545
"content": "저도 동의합니다!",
545546
"createdAt": "2025-09-22T13:30:00",

src/main/java/com/back/domain/board/post/controller/PostController.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,12 @@ public ResponseEntity<RsData<PageResponse<PostListResponse>>> getPosts(
5959
// 게시글 단건 조회
6060
@GetMapping("/{postId}")
6161
public ResponseEntity<RsData<PostDetailResponse>> getPost(
62-
@PathVariable Long postId
62+
@PathVariable Long postId,
63+
@AuthenticationPrincipal CustomUserDetails user
6364
) {
64-
PostDetailResponse response = postService.getPost(postId);
65+
PostDetailResponse response = (user != null)
66+
? postService.getPostWithUser(postId, user.getUserId())
67+
: postService.getPost(postId);
6568
return ResponseEntity
6669
.status(HttpStatus.OK)
6770
.body(RsData.success(

src/main/java/com/back/domain/board/post/controller/PostControllerDocs.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ public interface PostControllerDocs {
4343
"postId": 101,
4444
"author": {
4545
"id": 5,
46-
"nickname": "홍길동"
46+
"nickname": "홍길동",
47+
"profileImageUrl": null
4748
},
4849
"title": "첫 번째 게시글",
4950
"content": "안녕하세요, 첫 글입니다!",
@@ -171,7 +172,7 @@ ResponseEntity<RsData<PostResponse>> createPost(
171172
"items": [
172173
{
173174
"postId": 1,
174-
"author": { "id": 10, "nickname": "홍길동" },
175+
"author": { "id": 10, "nickname": "홍길동", "profileImageUrl": null },
175176
"title": "첫 글",
176177
"categories": [{ "id": 1, "name": "공지사항" }],
177178
"likeCount": 5,
@@ -247,7 +248,7 @@ ResponseEntity<RsData<PageResponse<PostListResponse>>> getPosts(
247248
"message": "게시글이 조회되었습니다.",
248249
"data": {
249250
"postId": 101,
250-
"author": { "id": 5, "nickname": "홍길동" },
251+
"author": { "id": 5, "nickname": "홍길동", "profileImageUrl": null },
251252
"title": "첫 번째 게시글",
252253
"content": "안녕하세요, 첫 글입니다!",
253254
"categories": [
@@ -257,6 +258,8 @@ ResponseEntity<RsData<PageResponse<PostListResponse>>> getPosts(
257258
"likeCount": 10,
258259
"bookmarkCount": 2,
259260
"commentCount": 3,
261+
"likedByMe": false,
262+
"bookmarkedByMe": false,
260263
"createdAt": "2025-09-22T10:30:00",
261264
"updatedAt": "2025-09-22T10:30:00"
262265
}
@@ -296,7 +299,8 @@ ResponseEntity<RsData<PageResponse<PostListResponse>>> getPosts(
296299
)
297300
})
298301
ResponseEntity<RsData<PostDetailResponse>> getPost(
299-
@PathVariable Long postId
302+
@PathVariable Long postId,
303+
@AuthenticationPrincipal CustomUserDetails user
300304
);
301305

302306
@Operation(
@@ -318,7 +322,8 @@ ResponseEntity<RsData<PostDetailResponse>> getPost(
318322
"postId": 101,
319323
"author": {
320324
"id": 5,
321-
"nickname": "홍길동"
325+
"nickname": "홍길동",
326+
"profileImageUrl": null
322327
},
323328
"title": "수정된 게시글",
324329
"content": "안녕하세요, 수정했습니다!",

src/main/java/com/back/domain/board/post/dto/PostDetailResponse.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,16 @@ public record PostDetailResponse(
2929
long likeCount,
3030
long bookmarkCount,
3131
long commentCount,
32+
Boolean likedByMe,
33+
Boolean bookmarkedByMe,
3234
LocalDateTime createdAt,
3335
LocalDateTime updatedAt
3436
) {
3537
public static PostDetailResponse from(Post post) {
38+
return from(post, false, false);
39+
}
40+
41+
public static PostDetailResponse from(Post post, boolean likedByMe, boolean bookmarkedByMe) {
3642
return new PostDetailResponse(
3743
post.getId(),
3844
AuthorResponse.from(post.getUser()),
@@ -44,6 +50,8 @@ public static PostDetailResponse from(Post post) {
4450
post.getPostLikes().size(),
4551
post.getPostBookmarks().size(),
4652
post.getComments().size(),
53+
likedByMe,
54+
bookmarkedByMe,
4755
post.getCreatedAt(),
4856
post.getUpdatedAt()
4957
);

src/main/java/com/back/domain/board/post/service/PostService.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
import com.back.domain.board.post.dto.PostListResponse;
88
import com.back.domain.board.post.dto.PostRequest;
99
import com.back.domain.board.post.dto.PostResponse;
10+
import com.back.domain.board.post.repository.PostBookmarkRepository;
1011
import com.back.domain.board.post.repository.PostCategoryRepository;
12+
import com.back.domain.board.post.repository.PostLikeRepository;
1113
import com.back.domain.board.post.repository.PostRepository;
1214
import com.back.domain.user.entity.User;
1315
import com.back.domain.user.repository.UserRepository;
@@ -26,6 +28,8 @@
2628
@Transactional
2729
public class PostService {
2830
private final PostRepository postRepository;
31+
private final PostLikeRepository postLikeRepository;
32+
private final PostBookmarkRepository postBookmarkRepository;
2933
private final UserRepository userRepository;
3034
private final PostCategoryRepository postCategoryRepository;
3135

@@ -85,6 +89,18 @@ public PostDetailResponse getPost(Long postId) {
8589
return PostDetailResponse.from(post);
8690
}
8791

92+
// TODO: 로그인 회원용 게시글 단건 조회 서비스, 추후 리팩토링 필요
93+
@Transactional(readOnly = true)
94+
public PostDetailResponse getPostWithUser(Long postId, Long userId) {
95+
Post post = postRepository.findById(postId)
96+
.orElseThrow(() -> new CustomException(ErrorCode.POST_NOT_FOUND));
97+
98+
boolean likedByMe = postLikeRepository.existsByUserIdAndPostId(userId, postId);
99+
boolean bookmarkedByMe = postBookmarkRepository.existsByUserIdAndPostId(userId, postId);
100+
101+
return PostDetailResponse.from(post, likedByMe, bookmarkedByMe);
102+
}
103+
88104
/**
89105
* 게시글 수정 서비스
90106
* 1. Post 조회

0 commit comments

Comments
 (0)