Skip to content

Commit

Permalink
feat: 피드 이미지 order 순으로 정렬하는 로직 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
hyeonjerry committed Sep 27, 2023
1 parent bf9658b commit 7f42f5f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.emmsale.image.domain.repository.ImageRepository;
import com.emmsale.member.domain.Member;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
Expand Down Expand Up @@ -64,14 +65,18 @@ public FeedListResponse findAllFeeds(final Member member, final Long eventId) {
}

private Map<Long, List<Image>> getFeedImagesMap(final List<Long> feedIds) {
return imageRepository.findAllByFeedIdIn(feedIds).stream()
final Map<Long, List<Image>> feedImagesMap = imageRepository.findAllByFeedIdIn(feedIds).stream()
.collect(Collectors.groupingBy(
Image::getContentId,
Collectors.mapping(
Function.identity(),
Collectors.toList()
)
));

feedImagesMap.forEach((feedId, images) -> images.sort(Comparator.comparing(Image::getOrder)));

return feedImagesMap;
}

private Map<Long, Long> getFeedIdCommentCountMap(final List<Long> feedIds) {
Expand Down Expand Up @@ -104,6 +109,7 @@ public FeedDetailResponse findFeed(final Member member, final Long id) {
final Feed feed = feedRepository.findById(id)
.orElseThrow(() -> new FeedException(FeedExceptionType.NOT_FOUND_FEED));
final List<Image> images = imageRepository.findAllByFeedId(feed.getId());
images.sort(Comparator.comparing(Image::getOrder));

validateBlockedMemberFeed(member, feed);
validateDeletedFeed(feed);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,19 +234,23 @@ void findFeedWithDeletedFeedIdTest() {
@DisplayName("피드 이미지 조회 테스트")
class FeedQueryWithImage {

private Image image1;
private Image image2;
private Image image3;
private List<Image> images;

@BeforeEach
void setUp() {
images = imageRepository.saveAll(List.of(
new Image("image-uuid", ImageType.FEED, feed1.getId(), 1, LocalDateTime.now()),
new Image("image-uuid", ImageType.FEED, feed1.getId(), 2, LocalDateTime.now())
)
);
image1 = new Image("image-uuid1", ImageType.FEED, feed1.getId(), 1, LocalDateTime.now());
image2 = new Image("image-uuid2", ImageType.FEED, feed1.getId(), 2, LocalDateTime.now());
image3 = new Image("image-uuid3", ImageType.FEED, feed1.getId(), 3, LocalDateTime.now());
images = List.of(image1, image2, image3);

imageRepository.saveAll(List.of(image2, image1, image3));
}

@Test
@DisplayName("피드에 이미지가 있을 경우 피드 목록에서 이미지 리스트를 함께 반환한다.")
@DisplayName("피드에 이미지가 있을 경우 피드 목록에서 이미지 리스트를 order순으로 정렬하여 함께 반환한다.")
void findAllFeedsWithImages() {
//given
final Long eventId = event.getId();
Expand All @@ -268,7 +272,7 @@ void findAllFeedsWithImages() {
}

@Test
@DisplayName("피드에 이미지가 있을 경우 피드 목록에서 이미지 리스트를 함께 반환한다.")
@DisplayName("피드에 이미지가 있을 경우 피드 목록에서 이미지 리스트를 order순으로 정렬하여 함께 반환한다.")
void findDetailFeedWithImages() {
//given
final FeedDetailResponse expect = FeedDetailResponse.from(feed1, images);
Expand Down

0 comments on commit 7f42f5f

Please sign in to comment.