Skip to content

Commit d372b3d

Browse files
committed
Feat: 내 북마크 게시글 목록 조회 API 구현
1 parent 3e95188 commit d372b3d

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

src/main/java/com/back/domain/board/post/repository/PostBookmarkRepository.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.back.domain.board.post.repository;
22

33
import com.back.domain.board.post.entity.PostBookmark;
4+
import org.springframework.data.domain.Page;
5+
import org.springframework.data.domain.Pageable;
46
import org.springframework.data.jpa.repository.JpaRepository;
57
import org.springframework.stereotype.Repository;
68

@@ -10,4 +12,5 @@
1012
public interface PostBookmarkRepository extends JpaRepository<PostBookmark, Long> {
1113
boolean existsByUserIdAndPostId(Long userId, Long postId);
1214
Optional<PostBookmark> findByUserIdAndPostId(Long userId, Long postId);
15+
Page<PostBookmark> findAllByUserId(Long userId, Pageable pageable);
1316
}

src/main/java/com/back/domain/user/controller/UserController.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,18 @@ public ResponseEntity<RsData<PageResponse<MyCommentResponse>>> getMyComments(
103103
response
104104
));
105105
}
106+
107+
// 내 북마크 게시글 목록 조회
108+
@GetMapping("/me/bookmarks")
109+
public ResponseEntity<RsData<PageResponse<PostListResponse>>> getMyBookmarks(
110+
@AuthenticationPrincipal CustomUserDetails user,
111+
@PageableDefault(sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable
112+
) {
113+
PageResponse<PostListResponse> response = userService.getMyBookmarks(user.getUserId(), pageable);
114+
return ResponseEntity
115+
.ok(RsData.success(
116+
"내 북마크 게시글 목록이 조회되었습니다.",
117+
response
118+
));
119+
}
106120
}

src/main/java/com/back/domain/user/service/UserService.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.back.domain.board.comment.repository.CommentRepository;
55
import com.back.domain.board.common.dto.PageResponse;
66
import com.back.domain.board.post.dto.PostListResponse;
7+
import com.back.domain.board.post.repository.PostBookmarkRepository;
78
import com.back.domain.board.post.repository.PostRepository;
89
import com.back.domain.user.dto.ChangePasswordRequest;
910
import com.back.domain.user.dto.UpdateUserProfileRequest;
@@ -31,8 +32,9 @@
3132
public class UserService {
3233
private final UserRepository userRepository;
3334
private final UserProfileRepository userProfileRepository;
34-
private final PostRepository postRepository;
3535
private final CommentRepository commentRepository;
36+
private final PostRepository postRepository;
37+
private final PostBookmarkRepository postBookmarkRepository;
3638
private final PasswordEncoder passwordEncoder;
3739

3840
/**
@@ -135,6 +137,7 @@ public void deleteUser(Long userId) {
135137
}
136138
}
137139

140+
// TODO: 내 게시글/댓글/북마크 목록 조회 N+1 발생 가능, 추후 리팩토링 필요
138141
/**
139142
* 내 게시글 목록 조회 서비스
140143
* 1. 사용자 조회 및 상태 검증
@@ -147,9 +150,11 @@ public PageResponse<PostListResponse> getMyPosts(Long userId, Pageable pageable)
147150
// 사용자 조회 및 상태 검증
148151
User user = getValidUser(userId);
149152

150-
// 게시글 목록 조회 및 응답 반환
153+
// 게시글 목록 조회
151154
Page<PostListResponse> page = postRepository.findAllByUserId(userId, pageable)
152155
.map(PostListResponse::from);
156+
157+
// 페이지 응답 반환
153158
return PageResponse.from(page);
154159
}
155160

@@ -165,9 +170,31 @@ public PageResponse<MyCommentResponse> getMyComments(Long userId, Pageable pagea
165170
// 사용자 조회 및 상태 검증
166171
User user = getValidUser(userId);
167172

168-
// 댓글 목록 조회 및 응답 반환
173+
// 댓글 목록 조회
169174
Page<MyCommentResponse> page = commentRepository.findAllByUserId(user.getId(), pageable)
170175
.map(MyCommentResponse::from);
176+
177+
// 페이지 응답 반환
178+
return PageResponse.from(page);
179+
}
180+
181+
/**
182+
* 내 북마크 게시글 목록 조회 서비스
183+
* 1. 사용자 조회 및 상태 검증
184+
* 2. 북마크 목록 조회
185+
* 3. PageResponse 반환
186+
*/
187+
@Transactional(readOnly = true)
188+
public PageResponse<PostListResponse> getMyBookmarks(Long userId, Pageable pageable) {
189+
190+
// 사용자 검증
191+
User user = getValidUser(userId);
192+
193+
// 북마크된 게시글 조회
194+
Page<PostListResponse> page = postBookmarkRepository.findAllByUserId(user.getId(), pageable)
195+
.map(bookmark -> PostListResponse.from(bookmark.getPost()));
196+
197+
// 페이지 응답 반환
171198
return PageResponse.from(page);
172199
}
173200

0 commit comments

Comments
 (0)