Skip to content

Commit

Permalink
🐛 fix: 메시지 조회는 현재 커서 이후 데이터부터 조회 (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
loveysuby committed Sep 30, 2024
1 parent a68fd59 commit d508f19
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package slvtwn.khu.toyouserver.application;

import static java.lang.Long.MIN_VALUE;

import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
Expand Down Expand Up @@ -65,9 +67,10 @@ public RollingPaperResponse findById(Long userId, Long rollingPaperId) {

public List<RollingPaperResponse> findReceivedRollingPapers(Long userId, Long groupId,
Long targetId, Integer limit) {
Long currentCursor = targetId != null ? targetId : MIN_VALUE;
List<Long> memberIds = getMemberIds(userId, groupId);
PageRequest pageRequest = PageRequest.ofSize(limit);
return rollingPaperRepository.findAllByMembersAfterCursor(memberIds, targetId, pageRequest).stream()
return rollingPaperRepository.findAllByMembersAfterCursor(memberIds, currentCursor, pageRequest).stream()
.map(RollingPaperResponse::from)
.toList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

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")
@Query("SELECT r FROM RollingPaper r WHERE r.member.id IN :memberIds AND r.id >= :targetId ORDER BY r.id DESC")
List<RollingPaper> findAllByMembersAfterCursor(@Param("memberIds") List<Long> memberIds,
@Param("targetId") Long targetId,
Pageable pageable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public ToyouResponse<RollingPaperResponse> findById(@UserAuthentication Long use
@GetMapping("/rollingpapers")
public ToyouResponse<List<RollingPaperResponse>> findReceivedRollingPapers(@UserAuthentication Long userId,
@RequestParam Long groupId,
@RequestParam Long targetId,
@RequestParam(defaultValue = "0") Long targetId,
@RequestParam(defaultValue = "10") int limit) {
return ToyouResponse.from(rollingPaperService.findReceivedRollingPapers(userId, groupId, targetId, limit));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;

import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
Expand All @@ -14,13 +12,11 @@
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.transaction.annotation.Transactional;
import slvtwn.khu.toyouserver.domain.Group;
import slvtwn.khu.toyouserver.domain.Member;
import slvtwn.khu.toyouserver.domain.RollingPaper;
import slvtwn.khu.toyouserver.domain.User;
import slvtwn.khu.toyouserver.dto.CoverRequest;
import slvtwn.khu.toyouserver.dto.RollingPaperRequest;
import slvtwn.khu.toyouserver.dto.RollingPaperResponse;

Expand Down Expand Up @@ -125,7 +121,7 @@ class RollingPaperServiceTest {

// when
List<RollingPaperResponse> response = rollingPaperService.findReceivedRollingPapers(user.getId(), group.getId(),
rollingPaper.getId() + 1, 10);
null, 10);

// then
assertThat(response).usingRecursiveComparison()
Expand Down Expand Up @@ -155,7 +151,7 @@ class RollingPaperServiceTest {
List<RollingPaperResponse> responseWithoutGroupId = rollingPaperService.findReceivedRollingPapers(
user.getId(),
null,
anotherRollingPaper.getId() + 1, 10);
0L, 10);

// then
assertThat(responseWithoutGroupId).usingRecursiveComparison()
Expand Down Expand Up @@ -188,7 +184,7 @@ class RollingPaperServiceTest {
List<RollingPaperResponse> responseWithGroupId = rollingPaperService.findReceivedRollingPapers(
user.getId(),
group1.getId(),
rollingPaper.getId() + 1, 10);
0L, 10);

// then
assertThat(responseWithGroupId).usingRecursiveComparison()
Expand Down Expand Up @@ -217,13 +213,11 @@ class RollingPaperServiceTest {
entityManager.persist(rollingPaper2);
entityManager.persist(rollingPaper3);

long totalRollingPapersCount = rollingPaper3.getId() + 1;

// when
List<RollingPaperResponse> response = rollingPaperService.findReceivedRollingPapers(
user.getId(),
group1.getId(),
totalRollingPapersCount, 10);
0L, 10);

// then
List<RollingPaperResponse> expectedResponse = List.of(
Expand Down Expand Up @@ -257,7 +251,7 @@ class RollingPaperServiceTest {
List<RollingPaperResponse> response = rollingPaperService.findReceivedRollingPapers(
user.getId(),
group.getId(),
rollingPaper3.getId() + 1, 10);
null, 10);

// then
List<RollingPaperResponse> expectedResponse = List.of(
Expand Down

0 comments on commit d508f19

Please sign in to comment.