Skip to content

Commit

Permalink
[feat] : Pagination 짤림 이슈 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackBean99 committed Sep 13, 2023
1 parent 531cfec commit 7e1ea06
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public List<Map<String, String>> execute() {
return splitByAnswersInApplicantId(answers);
}

@Override
/* @Override
public ApplicantPaginationResponseDto execute(Integer page) {
if (page < 1) throw OutOfIndexException.EXCEPTION;
AnswerPageResponseDto answers = answerAdaptor.findAll(page);
Expand All @@ -140,6 +140,23 @@ public ApplicantPaginationResponseDto execute(Integer page) {
.applicants(results)
.maxPage(answers.getMaxPage())
.build();
}*/
@Override
public ApplicantPaginationResponseDto execute(Integer page) {
if (page < 1) throw OutOfIndexException.EXCEPTION;
List<Answer> answers = answerAdaptor.findAll();

List<Map<String, String>> results = splitByAnswersInApplicantId(answers);
Integer maxPage = results.size() / COUNTS_PER_PAGE;
results = results.stream()
.skip((page - 1) * COUNTS_PER_PAGE)
.limit(COUNTS_PER_PAGE)
.collect(Collectors.toList());
// maxPage
return ApplicantPaginationResponseDto.builder()
.applicants(results)
.maxPage(maxPage + 1)
.build();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class RecruitStatic {
public static final Integer PLANNER_COLUMNS_ID = 3;
// itemsPerPage
public static final Integer COUNTS_PER_PAGE = 8;
public static final Integer ANSWER_COUNTS_PER_PERSON = 31;
public static final Integer ANSWER_COUNTS_PER_PERSON = 33;

public static final int MILLI_TO_SECOND = 1000;
public static final int BAD_REQUEST = 400;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@
import static com.econovation.recruitcommon.consts.RecruitStatic.COUNTS_PER_PAGE;

import com.econovation.recruitcommon.annotation.Adaptor;
import com.econovation.recruitcommon.exception.OutOfIndexException;
import com.econovation.recruitdomain.domains.applicant.domain.Answer;
import com.econovation.recruitdomain.domains.applicant.domain.AnswerRepository;
import com.econovation.recruitdomain.domains.applicant.exception.ApplicantNotFoundException;
import com.econovation.recruitdomain.domains.dto.AnswerPageResponseDto;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
Expand All @@ -32,14 +39,14 @@ public List<Answer> findAll() {
return answerRepository.findAll();
}

public AnswerPageResponseDto findAll(Integer page) {
/* public AnswerPageResponseDto findAll(Integer page) {
// 그룹별로 31개씩 묶어서 8개씩 조회하도록 페이징 설정
PageRequest pageRequest =
PageRequest.of(page - 1, COUNTS_PER_PAGE * ANSWER_COUNTS_PER_PERSON, Sort.by("createdAt"));
Page<Answer> content = answerRepository.findAll(pageRequest);
Integer maxPage = content.getTotalPages();
return AnswerPageResponseDto.of(maxPage, content.getContent());
}
}*/

public List<Answer> findByAnswerIds(List<String> applicantIds) {
List<Answer> applicants = answerRepository.findByApplicantIdIn(applicantIds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import java.util.List;
import java.util.Optional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

@Repository
Expand All @@ -16,4 +18,7 @@ public interface AnswerRepository extends JpaRepository<Answer, Long> {
Page<Answer> findAll(Pageable pageable);

Optional<Answer> findByAnswer(String name);

@Query("SELECT a FROM Answer a GROUP BY a.applicantId")
List<Answer> findGroupedAnswersByApplicantId();
}

0 comments on commit 7e1ea06

Please sign in to comment.