Skip to content

Commit

Permalink
🐛 fix: 메시지함 커서 정보를 limit 처리된 데이터로 바인딩 (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
loveysuby committed Oct 17, 2024
1 parent f5a0a95 commit d21144e
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import slvtwn.khu.toyouserver.common.response.ResponseType;
Expand Down Expand Up @@ -77,15 +78,17 @@ public RollingPaperResponse findById(Long userId, Long rollingPaperId) {
public RollingPaperPagedResponse findReceivedRollingPapers(Long userId, Long groupId,
Long targetId, Integer limit) {
List<Long> memberIds = getMemberIds(userId, groupId);
PageRequest pageRequest = PageRequest.ofSize(limit);
Slice<RollingPaper> rollingPapers = rollingPaperRepository.findAllByMembersAfterCursor(memberIds, targetId,
PageRequest pageRequest = PageRequest.of(0, limit + 1, Sort.by(Sort.Direction.DESC, "id"));
Slice<RollingPaper> rollingPapers = rollingPaperRepository.findByMembersWithCursor(memberIds, targetId,
pageRequest);
Long nextCursorId = rollingPapers.hasNext() ?
rollingPapers.getContent().get(rollingPapers.getContent().size() - 1).getId() : null;
CursorPageInfo cursorPageInfo = CursorPageInfo.from(rollingPapers, nextCursorId);
Long nextCursorId =
rollingPapers.getContent().size() > limit ? rollingPapers.getContent().get(0).getId() : null;
List<RollingPaperResponse> responses = rollingPapers.stream()
.limit(limit)
.map(RollingPaperResponse::from)
.toList();
CursorPageInfo cursorPageInfo = CursorPageInfo.from(rollingPapers, nextCursorId, responses.size(),
nextCursorId != null);
return RollingPaperPagedResponse.from(cursorPageInfo, responses);
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/slvtwn/khu/toyouserver/dto/CursorPageInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.springframework.data.domain.Slice;

public record CursorPageInfo(Long nextCursorId, int numberOfElements, boolean hasNext) {
public static CursorPageInfo from(Slice<?> data, Long lastCursor) {
return new CursorPageInfo(lastCursor, data.getNumberOfElements(), data.hasNext());
public static CursorPageInfo from(Slice<?> data, Long nextCursorId, int numberOfElements, boolean hasNext) {
return new CursorPageInfo(nextCursorId, data.getNumberOfElements(), data.hasNext());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
public interface RollingPaperRepository extends JpaRepository<RollingPaper, Long> {

@Query("SELECT r FROM RollingPaper r WHERE r.member.id IN :memberIds AND r.id >= :targetId ORDER BY r.id DESC")
Slice<RollingPaper> findAllByMembersAfterCursor(@Param("memberIds") List<Long> memberIds,
@Param("targetId") Long targetId,
Pageable pageable);
Slice<RollingPaper> findByMembersWithCursor(@Param("memberIds") List<Long> memberIds,
@Param("targetId") Long targetId,
Pageable pageable);

List<RollingPaper> findAllByMemberIn(List<Member> members);

void deleteAllByMemberIn(List<Member> members);
}
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,106 @@ class RollingPaperServiceTest {
assertThat(response.contents()).usingRecursiveComparison()
.isEqualTo(expectedResponse);
}

@Test
void 수신한_롤링페이퍼_조회는_커서가_존재할_경우_커서_이후의_롤링페이퍼만_조회된다() {
// given
User sender = new User("sender", LocalDate.now(), "introduction", "profile_picture", null);
User user = new User("name", LocalDate.now(), "introduction", "profile_picture", null);
Group group = new Group("group");
Member member = new Member(user, group);

RollingPaper rollingPaper1 = new RollingPaper(null, "title1", "rollingPaper1",
1L, member, new SenderSnapshot(sender.getName(), sender.getProfilePicture()));
RollingPaper rollingPaper2 = new RollingPaper(null, "title2", "rollingPaper2",
1L, member, new SenderSnapshot(sender.getName(), sender.getProfilePicture()));
RollingPaper rollingPaper3 = new RollingPaper(null, "title3", "rollingPaper3",
1L, member, new SenderSnapshot(sender.getName(), sender.getProfilePicture()));

entityManager.persist(user);
entityManager.persist(group);
entityManager.persist(member);
entityManager.persist(rollingPaper1);
entityManager.persist(rollingPaper2);
entityManager.persist(rollingPaper3);

// when
RollingPaperPagedResponse response = rollingPaperService.findReceivedRollingPapers(
user.getId(),
group.getId(),
rollingPaper2.getId(), 10);

// then
List<RollingPaperResponse> expectedResponse = List.of(
RollingPaperResponse.from(rollingPaper3),
RollingPaperResponse.from(rollingPaper2)
);

assertThat(response.contents()).usingRecursiveComparison()
.isEqualTo(expectedResponse);
}

@Test
void 커서_정보는_다음_페이지가_존재할_경우_다음_페이지의_커서를_포함한다() {
// given
User sender = new User("sender", LocalDate.now(), "introduction", "profile_picture", null);
User user = new User("name", LocalDate.now(), "introduction", "profile_picture", null);
Group group = new Group("group");
Member member = new Member(user, group);

RollingPaper rollingPaper1 = new RollingPaper(null, "title1", "rollingPaper1",
1L, member, new SenderSnapshot(sender.getName(), sender.getProfilePicture()));
RollingPaper rollingPaper2 = new RollingPaper(null, "title2", "rollingPaper2",
1L, member, new SenderSnapshot(sender.getName(), sender.getProfilePicture()));
RollingPaper rollingPaper3 = new RollingPaper(null, "title3", "rollingPaper3",
1L, member, new SenderSnapshot(sender.getName(), sender.getProfilePicture()));

entityManager.persist(user);
entityManager.persist(group);
entityManager.persist(member);
entityManager.persist(rollingPaper1);
entityManager.persist(rollingPaper2);
entityManager.persist(rollingPaper3);

// when
RollingPaperPagedResponse response = rollingPaperService.findReceivedRollingPapers(
user.getId(),
group.getId(),
rollingPaper1.getId(), 2); // 1번부터 2개 조회 -> expected 1,2 / next 3

// then
assertThat(response.cursorPageInfo().nextCursorId()).isEqualTo(rollingPaper3.getId());
}

@Test
void 조회_가능_최대치_도달_경우_다음_페이지_존재_여부는_False_이다() {
// given
User sender = new User("sender", LocalDate.now(), "introduction", "profile_picture", null);
User user = new User("name", LocalDate.now(), "introduction", "profile_picture", null);
Group group = new Group("group");
Member member = new Member(user, group);

RollingPaper rollingPaper1 = new RollingPaper(null, "title1", "rollingPaper1",
1L, member, new SenderSnapshot(sender.getName(), sender.getProfilePicture()));
RollingPaper rollingPaper2 = new RollingPaper(null, "title2", "rollingPaper2",
1L, member, new SenderSnapshot(sender.getName(), sender.getProfilePicture()));
RollingPaper rollingPaper3 = new RollingPaper(null, "title3", "rollingPaper3",
1L, member, new SenderSnapshot(sender.getName(), sender.getProfilePicture()));

entityManager.persist(user);
entityManager.persist(group);
entityManager.persist(member);
entityManager.persist(rollingPaper1);
entityManager.persist(rollingPaper2);
entityManager.persist(rollingPaper3);

// when
RollingPaperPagedResponse response = rollingPaperService.findReceivedRollingPapers(
user.getId(),
group.getId(),
rollingPaper1.getId(), 2);

// then
assertThat(response.cursorPageInfo().hasNext()).isFalse();
}
}

0 comments on commit d21144e

Please sign in to comment.