Skip to content

Commit

Permalink
Merge pull request #249 from JNU-econovation/feature/BE-91
Browse files Browse the repository at this point in the history
[REFACTOR] 면접 기록 지원 기수별로 조회하도록 수정
  • Loading branch information
rlajm1203 authored Sep 10, 2024
2 parents 6c782d2 + dde0684 commit c1dff01
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public ResponseEntity<RecordResponseDto> findByApplicantId(String applicantId) {
@ApiErrorExceptionsExample(RecordFindExceptionDocs.class)
@GetMapping("/page/{page}/records")
public ResponseEntity<RecordsViewResponseDto> findAll(
@PathVariable(name = "page") Integer page, @ParameterObject String sortType) {
return new ResponseEntity<>(recordUseCase.execute(page, sortType), HttpStatus.OK);
@PathVariable(name = "page") Integer page, @ParameterObject String sortType, @ParameterObject Integer year) {
return new ResponseEntity<>(recordUseCase.execute(page, year, sortType), HttpStatus.OK);
}

@Operation(summary = "지원자의 면접기록을 전부 조회합니다")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@
import com.econovation.recruitdomain.out.RecordLoadPort;
import com.econovation.recruitdomain.out.RecordRecordPort;
import com.econovation.recruitdomain.out.ScoreLoadPort;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import java.util.*;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -61,26 +57,40 @@ public List<Record> findAll() {
* Records를 모두 조회합니다 )
*/
@Override
public RecordsViewResponseDto execute(Integer page, String sortType) {
public RecordsViewResponseDto execute(Integer page, Integer year, String sortType) {
List<Record> result = recordLoadPort.findAll(page);
PageInfo pageInfo = getPageInfo(page);
if (result.isEmpty()) {

List<String> applicantIds = result.stream().map(Record::getApplicantId).toList();
List<Score> scores = scoreLoadPort.findByApplicantIds(applicantIds);
List<MongoAnswer> applicants = applicantQueryUseCase.execute(applicantIds).stream().filter(applicant ->year == null || applicant.getYear().equals(year)).toList();

if (result.isEmpty() || applicants.isEmpty()) {
return RecordsViewResponseDto.of(
pageInfo,
Collections.emptyList(),
Collections.emptyMap(),
Collections.emptyList());
}

List<String> applicantIds = result.stream().map(Record::getApplicantId).toList();
List<Score> scores = scoreLoadPort.findByApplicantIds(applicantIds);
Map<String, Integer> yearByAnswerIdMap = applicants.stream().collect(Collectors.toMap(MongoAnswer::getId, MongoAnswer::getYear));
Map<String, Double> scoreMap =
scores.stream()
.filter(score ->year == null || yearByAnswerIdMap.get(score.getApplicantId()).equals(year))
.collect(
Collectors.groupingBy(
Score::getApplicantId,
Collectors.averagingDouble(Score::getScore)));
List<MongoAnswer> applicants = applicantQueryUseCase.execute(applicantIds);

result = result.stream().filter(record -> year == null ||
Optional.ofNullable(record.getApplicantId())
.map(yearByAnswerIdMap::get)
.map(y -> y.equals(year))
.orElse(false)
)
.toList();

applicants = new ArrayList<>(applicants); // Unmodifiable List일 경우 Sort 불가. stream().toList()의 결과는 Unmodifiable List

if (sortType.equals("score")) {
List<Record> records = sortRecordsByScoresDesc(result, scoreMap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public interface RecordUseCase {

List<Record> findAll();

RecordsViewResponseDto execute(Integer page, String sortType);
RecordsViewResponseDto execute(Integer page, Integer year, String sortType);

Record findByApplicantId(String applicantId);

Expand Down

0 comments on commit c1dff01

Please sign in to comment.