Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ [REFACTOR] paging시 total page -> totalElement 반환 #563 #564

Merged
merged 1 commit into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
@AllArgsConstructor
@Builder
public class TestPageDto {
private Long totalPage;
private Long totalElements;
private List<TestRecordDto> testRecordDtos = new ArrayList<>();
public static TestPageDto getTestPageDto(Long totalPage, List<TestRecordDto> testRecordDtos) {
public static TestPageDto getTestPageDto(Long totalElements, List<TestRecordDto> testRecordDtos) {
return TestPageDto.builder()
.totalPage(totalPage)
.totalElements(totalElements)
.testRecordDtos(testRecordDtos)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package swm_nm.morandi.domain.testInfo.repository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import swm_nm.morandi.domain.testDuring.dto.TestStatus;
Expand All @@ -8,7 +9,7 @@
import java.util.List;
public interface TestRepository extends JpaRepository<Tests, Long>{
//Paging하여 테스트 기록을 가져옴
List<Tests> findAllTestsByMember_MemberIdAndTestStatus(Long memberId, TestStatus testStatus, Pageable pageable);
Page<Tests> findAllTestsByMember_MemberIdAndTestStatus(Long memberId, TestStatus testStatus, Pageable pageable);
//1년동안의 테스트 기록을 가져와서 레이팅 반환에 사용함
List<Tests> findAllTestsByMember_MemberIdAndTestStatusAndTestDateAfterOrderByTestDateAsc(Long memberId, TestStatus testStatus, LocalDateTime oneYearAgo);
Long countByMember_MemberIdAndTestStatus(Long memberId, TestStatus testStatus);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package swm_nm.morandi.domain.testInfo.service;

import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
Expand Down Expand Up @@ -32,12 +33,10 @@ public TestPageDto getTestRecordDtosLatest(TestRecordRequestDto testRecordReques
Long memberId = SecurityUtils.getCurrentMemberId();
Integer page = testRecordRequestDto.getPage();
Integer size = testRecordRequestDto.getSize();
Long totalCount = testRepository.countByMember_MemberIdAndTestStatus(memberId, TestStatus.COMPLETED);
Long totalPage = (totalCount + size - 1) / size;

//페이징하여 최근 4개의 테스트 기록을 가져옴
Pageable pageable = PageRequest.of(page - 1, size, Sort.by(DESC, "testDate"));
List<Tests> recentTests = testRepository.findAllTestsByMember_MemberIdAndTestStatus(memberId, TestStatus.COMPLETED, pageable);
Page<Tests> recentTests = testRepository.findAllTestsByMember_MemberIdAndTestStatus(memberId, TestStatus.COMPLETED, pageable);

//테스트 기록을 받아와서 dto로 변환하면서 getAttemptProblemDtos를 통해 테스트 문제들을 dto로 변환
List<TestRecordDto> testRecordDtos =
Expand All @@ -50,7 +49,7 @@ public TestPageDto getTestRecordDtosLatest(TestRecordRequestDto testRecordReques
//테스트 기록이 4개 미만일 경우 더미 데이터를 넣어줌
getTestRecordDtos(testRecordDtos);

TestPageDto testPageDto = TestPageDto.getTestPageDto(totalPage, testRecordDtos);
TestPageDto testPageDto = TestPageDto.getTestPageDto(recentTests.getTotalElements(), testRecordDtos);

return testPageDto;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@Setter
@AllArgsConstructor
public class AllTestHistoryResponse {
public int totalPage;
public long totalElements;
public int currentSize;
public List<TestHistoryDto> testHistorys;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public AllTestHistoryResponse findAllTestStatusByCondition(TestHistoryCondition

return AllTestHistoryResponse.builder()
.testHistorys(testHistoryDtos)
.totalPage(testHistory.getTotalPages())
.totalElements(testHistory.getTotalElements())
.currentSize(testHistory.getSize())
.build();

Expand Down