From 3a18915553983670f38436039c30621052365941 Mon Sep 17 00:00:00 2001 From: penrose15 Date: Wed, 24 Jul 2024 22:31:30 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20cursor=20=EA=B8=B0=EC=A4=80=20recordAt?= =?UTF-8?q?=EB=A1=9C=20=EB=B3=80=EA=B2=BD=20(#101)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../memory/repository/MemoryRepository.java | 11 ++-- .../repository/MemoryRepositoryImpl.java | 59 ++++++++++++------- .../repository/MemoryRepositoryTest.java | 56 +++++++----------- .../memory/api/MemoryController.java | 2 +- .../dto/request/TimelineRequestDto.java | 7 --- 5 files changed, 65 insertions(+), 70 deletions(-) diff --git a/module-infrastructure/persistence-database/src/main/java/com/depromeet/memory/repository/MemoryRepository.java b/module-infrastructure/persistence-database/src/main/java/com/depromeet/memory/repository/MemoryRepository.java index 274849be..211b8565 100644 --- a/module-infrastructure/persistence-database/src/main/java/com/depromeet/memory/repository/MemoryRepository.java +++ b/module-infrastructure/persistence-database/src/main/java/com/depromeet/memory/repository/MemoryRepository.java @@ -1,11 +1,10 @@ package com.depromeet.memory.repository; import com.depromeet.memory.Memory; +import com.depromeet.memory.Timeline; import java.time.LocalDate; import java.util.List; import java.util.Optional; -import org.springframework.data.domain.Pageable; -import org.springframework.data.domain.Slice; public interface MemoryRepository { Memory save(Memory memory); @@ -16,11 +15,11 @@ public interface MemoryRepository { Optional update(Long memoryId, Memory memoryUpdate); - Slice findPrevMemoryByMemberId( - Long memberId, Long cursorId, Pageable pageable, LocalDate recordAt); + Timeline findPrevMemoryByMemberId( + Long memberId, LocalDate cursorRecordAt, LocalDate recordAt); - Slice findNextMemoryByMemberId( - Long memberId, Long cursorId, Pageable pageable, LocalDate recordAt); + Timeline findNextMemoryByMemberId( + Long memberId, LocalDate cursorRecordAt, LocalDate recordAt); List getCalendarByYearAndMonth(Long memberId, Integer year, Short month); } diff --git a/module-infrastructure/persistence-database/src/main/java/com/depromeet/memory/repository/MemoryRepositoryImpl.java b/module-infrastructure/persistence-database/src/main/java/com/depromeet/memory/repository/MemoryRepositoryImpl.java index 344be10d..c1900ffc 100644 --- a/module-infrastructure/persistence-database/src/main/java/com/depromeet/memory/repository/MemoryRepositoryImpl.java +++ b/module-infrastructure/persistence-database/src/main/java/com/depromeet/memory/repository/MemoryRepositoryImpl.java @@ -7,6 +7,7 @@ import static com.depromeet.pool.entity.QPoolEntity.poolEntity; import com.depromeet.memory.Memory; +import com.depromeet.memory.Timeline; import com.depromeet.memory.entity.MemoryEntity; import com.depromeet.memory.entity.QMemoryEntity; import com.querydsl.core.types.dsl.BooleanExpression; @@ -18,9 +19,9 @@ import java.util.Optional; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; -import org.springframework.data.domain.Slice; -import org.springframework.data.domain.SliceImpl; +import org.springframework.data.domain.Sort; import org.springframework.stereotype.Repository; @Slf4j @@ -78,59 +79,75 @@ public Optional update(Long memoryId, Memory memoryUpdate) { .map(entity -> entity.update(MemoryEntity.from(memoryUpdate)).toModel()); } - // ---- 날짜 선택 후 위아래 무한 스크롤 구현 - @Override - public Slice findPrevMemoryByMemberId( - Long memberId, Long cursorId, Pageable pageable, LocalDate recordAt) { + public Timeline findPrevMemoryByMemberId( + Long memberId, LocalDate cursorRecordAt, LocalDate recordAt) { + Pageable pageable = PageRequest.of(0, 10, Sort.Direction.DESC, "recordAt"); List result = queryFactory .selectFrom(memory) .where( memory.member.id.eq(memberId), - ltCursorId(cursorId), + ltCursorRecordAt(cursorRecordAt), loeRecordAt(recordAt)) .limit(pageable.getPageSize() + 1) .orderBy(memory.recordAt.desc()) .fetch(); List content = toModel(result); - boolean hasPrev = false; + boolean hasNext = false; + Long nextMemoryId = null; if (content.size() > pageable.getPageSize()) { content = new ArrayList<>(content); content.removeLast(); - hasPrev = true; + hasNext = true; + Memory lastMemory = content.getLast(); + nextMemoryId = lastMemory.getId(); } - return new SliceImpl<>(content, pageable, hasPrev); + return Timeline.builder() + .timelineContents(content) + .pageSize(10) + .cursorId(nextMemoryId) + .hasNext(hasNext) + .build(); } @Override - public Slice findNextMemoryByMemberId( - Long memberId, Long cursorId, Pageable pageable, LocalDate recordAt) { + public Timeline findNextMemoryByMemberId( + Long memberId, LocalDate cursorRecordAt, LocalDate recordAt) { + Pageable pageable = PageRequest.of(0, 10, Sort.Direction.DESC, "recordAt"); + List result = queryFactory .selectFrom(memory) .where( memory.member.id.eq(memberId), - gtCursorId(cursorId), + gtCursorRecordAt(cursorRecordAt), goeRecordAt(recordAt)) .limit(pageable.getPageSize() + 1) .orderBy(memory.recordAt.asc()) .fetch(); - List content = toModel(result); boolean hasNext = false; + Long nextMemoryId = null; if (content.size() > pageable.getPageSize()) { content = new ArrayList<>(content); content.removeLast(); hasNext = true; + Memory lastMemory = content.getLast(); + nextMemoryId = lastMemory.getId(); } content = content.reversed(); - return new SliceImpl<>(content, pageable, hasNext); + return Timeline.builder() + .timelineContents(content) + .pageSize(10) + .cursorId(nextMemoryId) + .hasNext(hasNext) + .build(); } @Override @@ -161,18 +178,18 @@ private BooleanExpression loeRecordAt(LocalDate recordAt) { return memory.recordAt.loe(recordAt); } - private BooleanExpression ltCursorId(Long cursorId) { - if (cursorId == null) { + private BooleanExpression ltCursorRecordAt(LocalDate cursorRecordAt) { + if (cursorRecordAt == null) { return null; } - return memory.id.lt(cursorId); + return memory.recordAt.lt(cursorRecordAt); } - private BooleanExpression gtCursorId(Long cursorId) { - if (cursorId == null) { + private BooleanExpression gtCursorRecordAt(LocalDate cursorRecordAt) { + if (cursorRecordAt == null) { return null; } - return memory.id.gt(cursorId); + return memory.recordAt.gt(cursorRecordAt); } private BooleanExpression goeRecordAt(LocalDate recordAt) { diff --git a/module-infrastructure/persistence-database/src/test/java/com/depromeet/memory/repository/MemoryRepositoryTest.java b/module-infrastructure/persistence-database/src/test/java/com/depromeet/memory/repository/MemoryRepositoryTest.java index ecb1169c..af8a86d1 100644 --- a/module-infrastructure/persistence-database/src/test/java/com/depromeet/memory/repository/MemoryRepositoryTest.java +++ b/module-infrastructure/persistence-database/src/test/java/com/depromeet/memory/repository/MemoryRepositoryTest.java @@ -11,6 +11,7 @@ import com.depromeet.member.repository.MemberRepositoryImpl; import com.depromeet.memory.Memory; import com.depromeet.memory.MemoryDetail; +import com.depromeet.memory.Timeline; import com.querydsl.jpa.impl.JPAQueryFactory; import java.time.LocalDate; import java.util.List; @@ -20,18 +21,12 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.context.annotation.Import; -import org.springframework.data.domain.PageRequest; -import org.springframework.data.domain.Pageable; -import org.springframework.data.domain.Slice; -import org.springframework.data.domain.Sort; import org.springframework.test.context.junit.jupiter.SpringExtension; @DataJpaTest @Import(TestQueryDslConfig.class) @ExtendWith(SpringExtension.class) public class MemoryRepositoryTest { - private Pageable pageable; - @Autowired private JPAQueryFactory queryFactory; @Autowired private MemoryJpaRepository memoryJpaRepository; private MemoryRepositoryImpl memoryRepositoryImpl; @@ -45,8 +40,6 @@ public class MemoryRepositoryTest { @BeforeEach void setUp() { - pageable = getPageable(); - memberRepositoryImpl = new MemberRepositoryImpl(memberJpaRepository); memoryRepositoryImpl = new MemoryRepositoryImpl(queryFactory, memoryJpaRepository); memoryDetailRepositoryImpl = new MemoryDetailRepositoryImpl(memoryDetailJpaRepository); @@ -62,16 +55,12 @@ void setUp() { } } - private Pageable getPageable() { - return PageRequest.of(0, 30, Sort.by(Sort.Order.desc("recordAt"))); - } - @Test void findPrevMemoryByMemberId로_최근_날짜_이전_30일_recordAt_Desc로_가져오는지_테스트() { // when - Slice resultSlice = - memoryRepositoryImpl.findPrevMemoryByMemberId(member.getId(), null, pageable, null); - List result = resultSlice.getContent(); + Timeline timelines = + memoryRepositoryImpl.findPrevMemoryByMemberId(member.getId(), null, null); + List result = timelines.getTimelineContents(); Memory lastMemory = result.getLast(); // then @@ -85,10 +74,9 @@ private Pageable getPageable() { LocalDate recordAt = LocalDate.of(2024, 8, 31); // when - Slice resultSlice = - memoryRepositoryImpl.findPrevMemoryByMemberId( - member.getId(), null, pageable, recordAt); - List result = resultSlice.getContent(); + Timeline timelines = + memoryRepositoryImpl.findPrevMemoryByMemberId(member.getId(), null, recordAt); + List result = timelines.getTimelineContents(); Memory lastMemory = result.getLast(); // then @@ -101,18 +89,17 @@ private Pageable getPageable() { // given LocalDate recordAt = LocalDate.of(2024, 8, 31); - Slice initResultSlice = - memoryRepositoryImpl.findPrevMemoryByMemberId( - member.getId(), null, pageable, recordAt); + Timeline initTimelines = + memoryRepositoryImpl.findPrevMemoryByMemberId(member.getId(), null, recordAt); - List initResultSliceList = initResultSlice.getContent(); - Memory lastDate = initResultSliceList.getLast(); + List timelineContents = initTimelines.getTimelineContents(); + Memory lastDate = timelineContents.getLast(); // when - Slice resultSlice = + Timeline timelines = memoryRepositoryImpl.findPrevMemoryByMemberId( - member.getId(), lastDate.getId(), pageable, null); - List result = resultSlice.getContent(); + member.getId(), lastDate.getRecordAt(), null); + List result = timelines.getTimelineContents(); // then assertThat(result.size()).isEqualTo(30); @@ -124,18 +111,17 @@ private Pageable getPageable() { // given LocalDate recordAt = LocalDate.of(2024, 8, 31); - Slice initResultSlice = - memoryRepositoryImpl.findPrevMemoryByMemberId( - member.getId(), null, pageable, recordAt); + Timeline initTimeline = + memoryRepositoryImpl.findPrevMemoryByMemberId(member.getId(), null, recordAt); - List initResultSliceList = initResultSlice.getContent(); - Memory firstDate = initResultSliceList.getFirst(); + List initTimelineContents = initTimeline.getTimelineContents(); + Memory firstDate = initTimelineContents.getFirst(); // when - Slice resultSlice = + Timeline resultSlice = memoryRepositoryImpl.findNextMemoryByMemberId( - member.getId(), firstDate.getId(), pageable, null); - List result = resultSlice.getContent(); + member.getId(), firstDate.getRecordAt(), null); + List result = resultSlice.getTimelineContents(); // then assertThat(result.size()).isEqualTo(30); diff --git a/module-presentation/src/main/java/com/depromeet/memory/api/MemoryController.java b/module-presentation/src/main/java/com/depromeet/memory/api/MemoryController.java index add94d5a..52bc16ef 100644 --- a/module-presentation/src/main/java/com/depromeet/memory/api/MemoryController.java +++ b/module-presentation/src/main/java/com/depromeet/memory/api/MemoryController.java @@ -47,7 +47,7 @@ public ApiResponse update( @GetMapping("/timeline") public ApiResponse timeline( - @LoginMember Long memberId, @ModelAttribute TimelineRequestDto timelineRequestDto) { + @LoginMember Long memberId, TimelineRequestDto timelineRequestDto) { CustomSliceResponse result = memoryFacade.getTimelineByMemberIdAndCursorAndDate(memberId, timelineRequestDto); return ApiResponse.success(MemorySuccessType.GET_TIMELINE_SUCCESS, result); diff --git a/module-presentation/src/main/java/com/depromeet/memory/dto/request/TimelineRequestDto.java b/module-presentation/src/main/java/com/depromeet/memory/dto/request/TimelineRequestDto.java index 7a9fb241..fb81c753 100644 --- a/module-presentation/src/main/java/com/depromeet/memory/dto/request/TimelineRequestDto.java +++ b/module-presentation/src/main/java/com/depromeet/memory/dto/request/TimelineRequestDto.java @@ -1,8 +1,6 @@ package com.depromeet.memory.dto.request; import io.swagger.v3.oas.annotations.media.Schema; -import jakarta.validation.constraints.Min; -import jakarta.validation.constraints.NotNull; import java.time.YearMonth; import lombok.*; import org.springframework.format.annotation.DateTimeFormat; @@ -20,9 +18,4 @@ public class TimelineRequestDto { @Builder.Default @Schema(description = "조회하고 싶은 날짜 조회 이후, 날짜 기준 이전 정보를 보고 싶다면 false, 이후 정보를 보고 싶다면 true") private boolean showNewer = false; - - @Min(1) - @NotNull - @Schema(description = "페이지 크기") - private Integer size; }