Skip to content

Commit

Permalink
fix: 아카이브 조회시 최신순 정렬
Browse files Browse the repository at this point in the history
  • Loading branch information
shinyubin989 committed Jan 29, 2024
1 parent a0d2ee7 commit 9a106cf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
10 changes: 7 additions & 3 deletions src/main/java/com/baro/archive/application/ArchiveService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import com.baro.archive.application.dto.ArchiveUnitResult;
import com.baro.archive.application.dto.GetArchiveQuery;
import com.baro.archive.domain.Archive;
import com.baro.archive.domain.ArchiveRepository;
import com.baro.member.domain.Member;
import com.baro.member.domain.MemberRepository;
import com.baro.memofolder.domain.MemoFolder;
import com.baro.memofolder.domain.MemoFolderRepository;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
Expand All @@ -27,13 +29,15 @@ public List<ArchiveUnitResult> getArchive(GetArchiveQuery query) {
MemoFolder memoFolder = memoFolderRepository.getById(query.folderId());
memoFolder.matchOwner(member.getId());

Comparator<Archive> comparator = Comparator.comparing(Archive::getCreatedAt).reversed();

return switch (query.tab()) {
case ALL -> archiveRepository.findAllArchives(query.memberId(), query.folderId())
.stream().map(ArchiveUnitResult::of).collect(Collectors.toList());
.stream().sorted(comparator).map(ArchiveUnitResult::of).collect(Collectors.toList());
case MEMO -> archiveRepository.findAllArchivedMemos(query.memberId(), query.folderId())
.stream().map(ArchiveUnitResult::of).collect(Collectors.toList());
.stream().sorted(comparator).map(ArchiveUnitResult::of).collect(Collectors.toList());
case TEMPLATE -> archiveRepository.findAllArchivedTemplates(query.memberId(), query.folderId())
.stream().map(ArchiveUnitResult::of).collect(Collectors.toList());
.stream().sorted(comparator).map(ArchiveUnitResult::of).collect(Collectors.toList());
};
}
}
28 changes: 21 additions & 7 deletions src/test/java/com/baro/archive/application/ArchiveServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.baro.memofolder.domain.MemoFolder;
import com.baro.memofolder.domain.MemoFolderRepository;
import com.baro.memofolder.fake.FakeMemoFolderRepository;
import java.time.LocalDateTime;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator.ReplaceUnderscores;
Expand Down Expand Up @@ -51,7 +52,12 @@ void setUp() {
var archive2 = archiveRepository.save(끄적이는_아카이브2(member, memoFolder));
var archive3 = archiveRepository.save(참고하는_아카이브1(member, memoFolder, 보고하기()));
var archive4 = archiveRepository.save(참고하는_아카이브2(member, memoFolder, 감사전하기()));
archive1.setCreatedAtForTest(LocalDateTime.of(2024, 1, 1, 0, 0));
archive2.setCreatedAtForTest(LocalDateTime.of(2024, 1, 1, 5, 0));
archive3.setCreatedAtForTest(LocalDateTime.of(2024, 1, 1, 10, 0));
archive4.setCreatedAtForTest(LocalDateTime.of(2024, 1, 1, 15, 0));
var query = new GetArchiveQuery(member.getId(), memoFolder.getId(), ArchiveTab.ALL);
System.out.println(archive1.getCreatedAt());

// when
var archives = archiveService.getArchive(query);
Expand All @@ -60,10 +66,10 @@ void setUp() {
assertAll(
() -> assertThat(archives).hasSize(4),
() -> assertThat(archives).containsExactly(
ArchiveUnitResult.of(archive1),
ArchiveUnitResult.of(archive2),
ArchiveUnitResult.of(archive4),
ArchiveUnitResult.of(archive3),
ArchiveUnitResult.of(archive4)
ArchiveUnitResult.of(archive2),
ArchiveUnitResult.of(archive1)
)
);
}
Expand All @@ -77,6 +83,10 @@ void setUp() {
var archive2 = archiveRepository.save(끄적이는_아카이브2(member, memoFolder));
var archive3 = archiveRepository.save(참고하는_아카이브1(member, memoFolder, 보고하기()));
var archive4 = archiveRepository.save(참고하는_아카이브2(member, memoFolder, 감사전하기()));
archive1.setCreatedAtForTest(LocalDateTime.of(2024, 1, 1, 0, 0));
archive2.setCreatedAtForTest(LocalDateTime.of(2024, 1, 1, 5, 0));
archive3.setCreatedAtForTest(LocalDateTime.of(2024, 1, 1, 10, 0));
archive4.setCreatedAtForTest(LocalDateTime.of(2024, 1, 1, 15, 0));
var query = new GetArchiveQuery(member.getId(), memoFolder.getId(), ArchiveTab.MEMO);

// when
Expand All @@ -86,8 +96,8 @@ void setUp() {
assertAll(
() -> assertThat(archives).hasSize(2),
() -> assertThat(archives).containsExactly(
ArchiveUnitResult.of(archive1),
ArchiveUnitResult.of(archive2)
ArchiveUnitResult.of(archive2),
ArchiveUnitResult.of(archive1)
)
);
}
Expand All @@ -101,6 +111,10 @@ void setUp() {
var archive2 = archiveRepository.save(끄적이는_아카이브2(member, memoFolder));
var archive3 = archiveRepository.save(참고하는_아카이브1(member, memoFolder, 보고하기()));
var archive4 = archiveRepository.save(참고하는_아카이브2(member, memoFolder, 감사전하기()));
archive1.setCreatedAtForTest(LocalDateTime.of(2024, 1, 1, 0, 0));
archive2.setCreatedAtForTest(LocalDateTime.of(2024, 1, 1, 5, 0));
archive3.setCreatedAtForTest(LocalDateTime.of(2024, 1, 1, 10, 0));
archive4.setCreatedAtForTest(LocalDateTime.of(2024, 1, 1, 15, 0));
var query = new GetArchiveQuery(member.getId(), memoFolder.getId(), ArchiveTab.TEMPLATE);

// when
Expand All @@ -110,8 +124,8 @@ void setUp() {
assertAll(
() -> assertThat(archives).hasSize(2),
() -> assertThat(archives).containsExactly(
ArchiveUnitResult.of(archive3),
ArchiveUnitResult.of(archive4)
ArchiveUnitResult.of(archive4),
ArchiveUnitResult.of(archive3)
)
);
}
Expand Down

0 comments on commit 9a106cf

Please sign in to comment.