Skip to content

Commit 2dd226f

Browse files
authored
최신 게시글 100개 조회 (#60)
* fix: dto 이름 수정 * HotFeat: 모든 게시글 중 최근 100개 조회 * fix: Q타입 객체 네이밍 수정
1 parent b78d673 commit 2dd226f

File tree

9 files changed

+77
-39
lines changed

9 files changed

+77
-39
lines changed

src/main/java/org/ahpuh/surf/post/controller/PostController.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,11 @@ public ResponseEntity<Void> cancelFavorite(
105105
}
106106

107107
@GetMapping("/follow/posts")
108-
public ResponseEntity<CursorResult<FollowingPostDto>> explore(
108+
public ResponseEntity<CursorResult<ExploreDto>> explore(
109109
@AuthenticationPrincipal final JwtAuthentication authentication,
110110
@RequestParam final Long cursorId
111111
) {
112-
final CursorResult<FollowingPostDto> followingPostDtos = postService.explore(authentication.userId, cursorId, PageRequest.of(0, 10));
112+
final CursorResult<ExploreDto> followingPostDtos = postService.followingExplore(authentication.userId, cursorId, PageRequest.of(0, 10));
113113
return ResponseEntity.ok().body(followingPostDtos);
114114
}
115115

@@ -149,4 +149,12 @@ public ResponseEntity<Integer> getAllPostByCategory(
149149
return ResponseEntity.ok().body(postService.getRecentScore(categoryId));
150150
}
151151

152+
@GetMapping("/posts/recent")
153+
public ResponseEntity<List<ExploreDto>> recentAllPosts(
154+
@AuthenticationPrincipal final JwtAuthentication authentication
155+
) {
156+
final List<ExploreDto> recentAllPosts = postService.recentAllPosts(authentication.userId);
157+
return ResponseEntity.ok().body(recentAllPosts);
158+
}
159+
152160
}

src/main/java/org/ahpuh/surf/post/converter/PostConverter.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,21 @@ public List<CategorySimpleDto> sortPostScoresByCategory(
113113
return categorySimpleDtos;
114114
}
115115

116+
public ExploreDto toRecentAllPosts(final Post post, final Optional<Like> like) {
117+
final ExploreDto recentPostDtos = ExploreDto.builder()
118+
.userId(post.getUser().getUserId())
119+
.userName(post.getUser().getUserName())
120+
.profilePhotoUrl(post.getUser().getProfilePhotoUrl())
121+
.categoryName(post.getCategory().getName())
122+
.colorCode(post.getCategory().getColorCode())
123+
.postId(post.getPostId())
124+
.content(post.getContent())
125+
.score(post.getScore())
126+
.selectedDate(post.getSelectedDate())
127+
.createdAt(post.getCreatedAt())
128+
.build();
129+
like.ifPresent(likeEntity -> recentPostDtos.setLiked(likeEntity.getLikeId()));
130+
return recentPostDtos;
131+
}
132+
116133
}

src/main/java/org/ahpuh/surf/post/dto/FollowingPostDto.java renamed to src/main/java/org/ahpuh/surf/post/dto/ExploreDto.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
@NoArgsConstructor(access = AccessLevel.PROTECTED)
1111
@AllArgsConstructor
1212
@Builder
13-
public class FollowingPostDto {
13+
public class ExploreDto {
1414

1515
private Long userId;
1616

@@ -43,18 +43,18 @@ public class FollowingPostDto {
4343
private Boolean isLiked = false;
4444

4545
@QueryProjection
46-
public FollowingPostDto(final Long userId,
47-
final String userName,
48-
final String profilePhotoUrl,
49-
final String categoryName,
50-
final String colorCode,
51-
final Long postId,
52-
final String content,
53-
final Integer score,
54-
final String imageUrl,
55-
final String fileUrl,
56-
final LocalDate selectedDate,
57-
final LocalDateTime createdAt) {
46+
public ExploreDto(final Long userId,
47+
final String userName,
48+
final String profilePhotoUrl,
49+
final String categoryName,
50+
final String colorCode,
51+
final Long postId,
52+
final String content,
53+
final Integer score,
54+
final String imageUrl,
55+
final String fileUrl,
56+
final LocalDate selectedDate,
57+
final LocalDateTime createdAt) {
5858
this.userId = userId;
5959
this.userName = userName;
6060
this.profilePhotoUrl = profilePhotoUrl;

src/main/java/org/ahpuh/surf/post/repository/PostRepository.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ public interface PostRepository extends JpaRepository<Post, Long>, PostRepositor
2222

2323
List<Post> findByUserAndCategoryAndSelectedDateLessThanAndCreatedAtLessThanOrderBySelectedDateDesc(User user, Category category, LocalDate selectedDate, LocalDateTime createdAt, Pageable page);
2424

25-
Boolean existsBySelectedDateLessThanAndCreatedAtLessThan(LocalDate selectedDate, LocalDateTime createdAt);
26-
2725
Post findTop1ByCategoryOrderBySelectedDateDesc(Category category);
2826

2927
List<Post> findByCategory(Category category);
3028

29+
List<Post> findTop100ByIsDeletedIsFalseOrderByCreatedAtDesc(Pageable page);
30+
3131
}

src/main/java/org/ahpuh/surf/post/repository/PostRepositoryImpl.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ public class PostRepositoryImpl implements PostRepositoryQuerydsl {
1919
private final JPAQueryFactory queryFactory;
2020

2121
@Override
22-
public List<FollowingPostDto> findFollowingPosts(final Long userId, final Pageable page) {
22+
public List<ExploreDto> findFollowingPosts(final Long userId, final Pageable page) {
2323
return queryFactory
24-
.select(new QFollowingPostDto(
24+
.select(new QExploreDto(
2525
post.user.userId.as("userId"),
2626
post.user.userName.as("userName"),
2727
post.user.profilePhotoUrl.as("profilePhotoUrl"),
@@ -45,9 +45,9 @@ public List<FollowingPostDto> findFollowingPosts(final Long userId, final Pageab
4545
}
4646

4747
@Override
48-
public List<FollowingPostDto> findNextFollowingPosts(final Long userId, final LocalDate selectedDate, final LocalDateTime createdAt, final Pageable page) {
48+
public List<ExploreDto> findNextFollowingPosts(final Long userId, final LocalDate selectedDate, final LocalDateTime createdAt, final Pageable page) {
4949
return queryFactory
50-
.select(new QFollowingPostDto(
50+
.select(new QExploreDto(
5151
post.user.userId.as("userId"),
5252
post.user.userName.as("userName"),
5353
post.user.profilePhotoUrl.as("profilePhotoUrl"),

src/main/java/org/ahpuh/surf/post/repository/PostRepositoryQuerydsl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.ahpuh.surf.post.repository;
22

3-
import org.ahpuh.surf.post.dto.FollowingPostDto;
3+
import org.ahpuh.surf.post.dto.ExploreDto;
44
import org.ahpuh.surf.post.dto.PostCountDto;
55
import org.ahpuh.surf.post.dto.PostScoreCategoryDto;
66
import org.ahpuh.surf.user.entity.User;
@@ -12,9 +12,9 @@
1212

1313
public interface PostRepositoryQuerydsl {
1414

15-
List<FollowingPostDto> findFollowingPosts(Long userId, Pageable page);
15+
List<ExploreDto> findFollowingPosts(Long userId, Pageable page);
1616

17-
List<FollowingPostDto> findNextFollowingPosts(Long userId, LocalDate selectedDate, LocalDateTime createdAt, Pageable page);
17+
List<ExploreDto> findNextFollowingPosts(Long userId, LocalDate selectedDate, LocalDateTime createdAt, Pageable page);
1818

1919
List<PostCountDto> findAllDateAndCountBetween(int year, User user);
2020

src/main/java/org/ahpuh/surf/post/service/PostService.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ public interface PostService {
1919

2020
void delete(Long postID);
2121

22-
Long clickFavorite(final Long userId, final Long postId);
22+
Long clickFavorite(Long userId, Long postId);
2323

2424
List<PostCountDto> getCountsPerDayWithYear(int year, Long userId);
2525

2626
List<CategorySimpleDto> getScoresWithCategoryByUserId(Long userId);
2727

28-
CursorResult<FollowingPostDto> explore(Long userId, final Long cursorId, final Pageable page);
28+
CursorResult<ExploreDto> followingExplore(Long userId, Long cursorId, Pageable page);
2929

3030
List<PostResponseDto> getPost(Long userId, Integer year, Integer month);
3131

@@ -34,4 +34,7 @@ public interface PostService {
3434
CursorResult<AllPostResponseDto> getAllPostByCategory(Long myId, Long userId, Long categoryId, Long cursorId, Pageable page);
3535

3636
int getRecentScore(Long categoryId);
37+
38+
List<ExploreDto> recentAllPosts(Long myId);
39+
3740
}

src/main/java/org/ahpuh/surf/post/service/PostServiceImpl.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515
import org.ahpuh.surf.post.repository.PostRepository;
1616
import org.ahpuh.surf.user.entity.User;
1717
import org.ahpuh.surf.user.repository.UserRepository;
18+
import org.springframework.data.domain.PageRequest;
1819
import org.springframework.data.domain.Pageable;
1920
import org.springframework.stereotype.Service;
2021
import org.springframework.transaction.annotation.Transactional;
2122

2223
import java.time.LocalDate;
2324
import java.util.ArrayList;
2425
import java.util.List;
26+
import java.util.stream.Collectors;
2527

2628
@RequiredArgsConstructor
2729
@Transactional(readOnly = true)
@@ -77,45 +79,45 @@ public Long clickFavorite(final Long userId, final Long postId) {
7779
}
7880

7981
@Override
80-
public CursorResult<FollowingPostDto> explore(final Long myId, final Long cursorId, final Pageable page) {
82+
public CursorResult<ExploreDto> followingExplore(final Long myId, final Long cursorId, final Pageable page) {
8183
final User me = userRepository.findById(myId)
8284
.orElseThrow(() -> EntityExceptionHandler.UserNotFound(myId));
8385
if (followRepository.findByUser(me).isEmpty()) {
84-
final List<FollowingPostDto> emptyList = new ArrayList<>();
85-
emptyList.add(FollowingPostDto.builder().build());
86+
final List<ExploreDto> emptyList = new ArrayList<>();
87+
emptyList.add(ExploreDto.builder().build());
8688
return new CursorResult<>(emptyList, false);
8789
}
8890

8991
final Post findPost = postRepository.findById(cursorId).orElse(null);
9092

91-
final List<FollowingPostDto> followingPostDtos = findPost == null ?
93+
final List<ExploreDto> exploreDtos = findPost == null ?
9294
postRepository.findFollowingPosts(myId, page) :
9395
postRepository.findNextFollowingPosts(myId, findPost.getSelectedDate(), findPost.getCreatedAt(), page);
9496

95-
for (final FollowingPostDto dto : followingPostDtos) {
97+
for (final ExploreDto dto : exploreDtos) {
9698
likeRepository.findByUserIdAndPost(myId, getPostById(dto.getPostId()))
9799
.ifPresent(like -> dto.setLiked(like.getLikeId()));
98100
}
99101

100-
final long lastIdOfIndex = followingPostDtos.isEmpty() ? 0 : followingPostDtos.get(followingPostDtos.size() - 1).getPostId();
102+
final long lastIdOfIndex = exploreDtos.isEmpty() ? 0 : exploreDtos.get(exploreDtos.size() - 1).getPostId();
101103

102104
final boolean hasNext = !postRepository.findNextFollowingPosts(
103105
myId,
104-
followingPostDtos
106+
exploreDtos
105107
.stream()
106108
.filter(post -> post.getPostId().equals(lastIdOfIndex))
107109
.findFirst()
108110
.get()
109111
.getSelectedDate(),
110-
followingPostDtos
112+
exploreDtos
111113
.stream()
112114
.filter(post -> post.getPostId().equals(lastIdOfIndex))
113115
.findFirst()
114116
.get()
115117
.getCreatedAt(),
116118
page).isEmpty();
117119

118-
return new CursorResult<>(followingPostDtos, hasNext);
120+
return new CursorResult<>(exploreDtos, hasNext);
119121
}
120122

121123
public List<PostCountDto> getCountsPerDayWithYear(final int year, final Long userId) {
@@ -240,4 +242,12 @@ private Post getPostById(final Long postId) {
240242
return postRepository.findById(postId)
241243
.orElseThrow(() -> EntityExceptionHandler.PostNotFound(postId));
242244
}
245+
246+
public List<ExploreDto> recentAllPosts(final Long myId) {
247+
return postRepository.findTop100ByIsDeletedIsFalseOrderByCreatedAtDesc(PageRequest.of(0, 100))
248+
.stream()
249+
.map(postEntity -> postConverter.toRecentAllPosts(postEntity, likeRepository.findByUserIdAndPost(myId, postEntity)))
250+
.collect(Collectors.toList());
251+
}
252+
243253
}

src/test/java/org/ahpuh/surf/post/repository/PostRepositoryTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import org.ahpuh.surf.category.repository.CategoryRepository;
66
import org.ahpuh.surf.follow.entity.Follow;
77
import org.ahpuh.surf.follow.repository.FollowRepository;
8-
import org.ahpuh.surf.post.dto.FollowingPostDto;
9-
import org.ahpuh.surf.post.dto.QFollowingPostDto;
8+
import org.ahpuh.surf.post.dto.ExploreDto;
9+
import org.ahpuh.surf.post.dto.QExploreDto;
1010
import org.ahpuh.surf.post.entity.Post;
1111
import org.ahpuh.surf.user.controller.UserController;
1212
import org.ahpuh.surf.user.dto.UserJoinRequestDto;
@@ -141,8 +141,8 @@ void setUp() {
141141
void testQueryDsl() {
142142
final JPAQueryFactory query = new JPAQueryFactory(entityManager);
143143
final PageRequest page = PageRequest.of(0, 10);
144-
final List<FollowingPostDto> posts = query
145-
.select(new QFollowingPostDto(
144+
final List<ExploreDto> posts = query
145+
.select(new QExploreDto(
146146
post.user.userId.as("userId"),
147147
post.user.userName.as("userName"),
148148
post.user.profilePhotoUrl.as("profilePhotoUrl"),

0 commit comments

Comments
 (0)