Skip to content

Commit

Permalink
refactor: #15 게시글 조회 리팩토링
Browse files Browse the repository at this point in the history
  • Loading branch information
ehs208 committed Nov 5, 2024
1 parent 0758745 commit 5020c3d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ public class PostController {
@Operation(summary = "특정 사용자의 게시글 조회", description = "특정 사용자의 게시글을 조회합니다.")
public ResponseEntity<GlobalResponseDto<List<ArticlesResponseDto>>> getArticles(@PathVariable String customId) {
return ResponseEntity.status(HttpStatus.OK)
.body(GlobalResponseDto.success(postService.getArticles(customId)));
.body(GlobalResponseDto.success(postService.getArticles(customId, false)));
}

@GetMapping("/{customId}/list/media")
@Operation(summary = "특정 사용자의 미디어 게시글 조회", description = "특정 사용자의 미디어 게시글을 조회합니다.")
public ResponseEntity<GlobalResponseDto<List<ArticlesResponseDto>>> getMediaArticles(
@PathVariable String customId) {
return ResponseEntity.status(HttpStatus.OK)
.body(GlobalResponseDto.success(postService.getArticlesWithMedia(customId)));
.body(GlobalResponseDto.success(postService.getArticles(customId, true)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,43 +24,22 @@ public class PostService {
private final UserRepository userRepository;
private final PostRepository postRepository;

// 전체 게시글 조회
public List<ArticlesResponseDto> getArticles(String customId) {
// 게시글 조회 (미디어 필터 옵션 추가)
public List<ArticlesResponseDto> getArticles(String customId, boolean mediaOnly) {
User user = getUser(customId);
List<Object[]> posts = getPosts(user);

Map<Post, List<String>> groupedPosts = groupPostsByFilePath(posts);
Map<Post, List<String>> groupedPosts = groupPostsByFilePath(posts, mediaOnly);

return groupedPosts.entrySet().stream()
.map(entry -> ArticlesResponseDto.from(entry.getKey(), entry.getValue()))
.collect(Collectors.toList());
}

// 미디어 있는게 게시글만 조회
public List<ArticlesResponseDto> getArticlesWithMedia(String customId) {
User user = getUser(customId);
List<Object[]> posts = getPosts(user);

Map<Post, List<String>> groupedPosts = groupPostsByFilePathWithMedia(posts);

return groupedPosts.entrySet().stream()
.map(entry -> ArticlesResponseDto.from(entry.getKey(), entry.getValue()))
.collect(Collectors.toList());
}

// 이미지만 있는 게시글 파일 경로 그룹화
private Map<Post, List<String>> groupPostsByFilePathWithMedia(List<Object[]> posts) {
return posts.stream()
.filter(post -> post[1] != null)
.collect(Collectors.groupingBy(
post -> (Post)post[0],
Collectors.mapping(post -> (String)post[1], Collectors.toList())
));
}

// 전체 게시글 파일 경로 그룹화
private Map<Post, List<String>> groupPostsByFilePath(List<Object[]> posts) {
// 게시글 파일 경로 그룹화 (미디어 필터링 조건 추가)
private Map<Post, List<String>> groupPostsByFilePath(List<Object[]> posts, boolean mediaOnly) {
return posts.stream()
.filter(post -> !mediaOnly || post[1] != null) // 미디어 있는 경우만 필터링
.collect(Collectors.groupingBy(
post -> (Post)post[0],
Collectors.mapping(post -> (String)post[1], Collectors.toList())
Expand All @@ -69,13 +48,11 @@ private Map<Post, List<String>> groupPostsByFilePath(List<Object[]> posts) {

// 유저 정보로 게시글 조회
private List<Object[]> getPosts(User user) {
List<Object[]> posts = postRepository.findPostsByWriter(user);
return posts;
return postRepository.findPostsByWriter(user);
}

// 유저 정보 조회
private User getUser(String customId) {
User user = userRepository.findByCustomId(customId).orElseThrow(UserNotFoundException::new);
return user;
return userRepository.findByCustomId(customId).orElseThrow(UserNotFoundException::new);
}
}

0 comments on commit 5020c3d

Please sign in to comment.