-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat-be: 프로세스 목록 조회 API 필터링 및 정렬 구현 (#756)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: cutehumanS2 <[email protected]>
- Loading branch information
1 parent
2e05f2b
commit 7434b61
Showing
16 changed files
with
520 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
backend/src/main/java/com/cruru/applicant/domain/ApplicantSortOption.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.cruru.applicant.domain; | ||
|
||
import com.cruru.applicant.domain.dto.ApplicantCard; | ||
import com.cruru.applicant.exception.badrequest.ApplicantSortException; | ||
import java.util.Arrays; | ||
import java.util.Comparator; | ||
|
||
public enum ApplicantSortOption { | ||
|
||
ASC(Comparator.naturalOrder()), | ||
DESC(Comparator.reverseOrder()); | ||
|
||
private final Comparator<Comparable> comparator; | ||
|
||
ApplicantSortOption(Comparator<Comparable> comparator) { | ||
this.comparator = comparator; | ||
} | ||
|
||
public static Comparator<ApplicantCard> getCombinedComparator(String sortByCreatedAt, String sortByScore) { | ||
ApplicantSortOption createdAtOption = convertToSortOption(sortByCreatedAt); | ||
ApplicantSortOption scoreOption = convertToSortOption(sortByScore); | ||
|
||
return createdAtOption.getCreatedAtComparator() | ||
.thenComparing(scoreOption.getScoreComparator()); | ||
} | ||
|
||
private static ApplicantSortOption convertToSortOption(String sortOption) { | ||
return Arrays.stream(ApplicantSortOption.values()) | ||
.filter(option -> option.name().equalsIgnoreCase(sortOption)) | ||
.findAny() | ||
.orElseThrow(ApplicantSortException::new); | ||
} | ||
|
||
private Comparator<ApplicantCard> getCreatedAtComparator() { | ||
return Comparator.comparing(ApplicantCard::createdAt, comparator); | ||
} | ||
|
||
private Comparator<ApplicantCard> getScoreComparator() { | ||
return Comparator.comparing(ApplicantCard::averageScore, comparator); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
backend/src/main/java/com/cruru/applicant/domain/EvaluationStatus.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.cruru.applicant.domain; | ||
|
||
import com.cruru.applicant.domain.dto.ApplicantCard; | ||
import com.cruru.applicant.exception.badrequest.EvaluationStatusException; | ||
import java.util.Arrays; | ||
import java.util.function.Predicate; | ||
|
||
public enum EvaluationStatus { | ||
|
||
ALL(card -> true), | ||
NOT_EVALUATED(ApplicantCard::hasEvaluation), | ||
EVALUATED(ApplicantCard::hasNoEvaluation); | ||
|
||
private final Predicate<ApplicantCard> predicate; | ||
|
||
EvaluationStatus(Predicate<ApplicantCard> predicate) { | ||
this.predicate = predicate; | ||
} | ||
|
||
public static boolean matches(ApplicantCard card, String evaluationStatus) { | ||
return Arrays.stream(EvaluationStatus.values()) | ||
.filter(status -> status.name().equalsIgnoreCase(evaluationStatus)) | ||
.findAny() | ||
.orElseThrow(EvaluationStatusException::new) | ||
.predicate | ||
.test(card); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
backend/src/main/java/com/cruru/applicant/exception/badrequest/ApplicantSortException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.cruru.applicant.exception.badrequest; | ||
|
||
import com.cruru.advice.badrequest.BadRequestException; | ||
|
||
public class ApplicantSortException extends BadRequestException { | ||
|
||
private static final String MESSAGE = "지원하는 정렬 조건이 아닙니다."; | ||
|
||
public ApplicantSortException() { | ||
super(MESSAGE); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...end/src/main/java/com/cruru/applicant/exception/badrequest/EvaluationStatusException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.cruru.applicant.exception.badrequest; | ||
|
||
import com.cruru.advice.badrequest.BadRequestException; | ||
|
||
public class EvaluationStatusException extends BadRequestException { | ||
|
||
private static final String MESSAGE = "지원하는 평가 상태가 아닙니다."; | ||
|
||
public EvaluationStatusException() { | ||
super(MESSAGE); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
backend/src/test/java/com/cruru/applicant/domain/ApplicantSortOptionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package com.cruru.applicant.domain; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.cruru.applicant.domain.dto.ApplicantCard; | ||
import com.cruru.util.fixture.ApplicantCardFixture; | ||
import com.cruru.util.fixture.DefaultFilterAndOrderFixture; | ||
import java.time.LocalDateTime; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
|
||
@DisplayName("지원자 정렬 테스트") | ||
class ApplicantSortOptionTest { | ||
|
||
@Nested | ||
@DisplayName("지원자의 지원 날짜 정렬 테스트") | ||
class getCreatedAtComparatorTest { | ||
|
||
@DisplayName("지원자의 지원 날짜 기준으로 오름차순으로 정렬한다.") | ||
@ParameterizedTest | ||
@ValueSource(strings = {"ASC", "asc"}) | ||
void getCreatedAtComparator_asc(String sortByCreatedAt) { | ||
// given | ||
LocalDateTime createdAt1 = LocalDateTime.of(2024, 10, 2, 20, 0); | ||
LocalDateTime createdAt2 = LocalDateTime.of(2024, 10, 3, 20, 0); | ||
ApplicantCard applicantCard1 = ApplicantCardFixture.evaluatedApplicantCard(createdAt1); | ||
ApplicantCard applicantCard2 = ApplicantCardFixture.evaluatedApplicantCard(createdAt2); | ||
List<ApplicantCard> applicantCards = Arrays.asList(applicantCard2, applicantCard1); | ||
|
||
// when | ||
applicantCards.sort(ApplicantSortOption.getCombinedComparator( | ||
sortByCreatedAt, | ||
DefaultFilterAndOrderFixture.DEFAULT_SORT_BY_SCORE | ||
)); | ||
|
||
// then | ||
assertThat(applicantCards).containsExactly(applicantCard1, applicantCard2); | ||
} | ||
|
||
@DisplayName("지원자의 지원 날짜 기준으로 내림차순으로 정렬한다.") | ||
@ParameterizedTest | ||
@ValueSource(strings = {"DESC", "desc"}) | ||
void getCreatedAtComparator_desc(String sortByCreatedAt) { | ||
// given | ||
LocalDateTime createdAt1 = LocalDateTime.of(2024, 10, 2, 20, 0); | ||
LocalDateTime createdAt2 = LocalDateTime.of(2024, 10, 3, 20, 0); | ||
ApplicantCard applicantCard1 = ApplicantCardFixture.evaluatedApplicantCard(createdAt1); | ||
ApplicantCard applicantCard2 = ApplicantCardFixture.evaluatedApplicantCard(createdAt2); | ||
List<ApplicantCard> applicantCards = Arrays.asList(applicantCard1, applicantCard2); | ||
|
||
// when | ||
applicantCards.sort(ApplicantSortOption.getCombinedComparator( | ||
sortByCreatedAt, | ||
DefaultFilterAndOrderFixture.DEFAULT_SORT_BY_SCORE | ||
|
||
)); | ||
|
||
// then | ||
assertThat(applicantCards).containsExactly(applicantCard2, applicantCard1); | ||
} | ||
} | ||
|
||
@Nested | ||
@DisplayName("지원자의 평균 점수 정렬 테스트") | ||
class getScoreComparatorTest { | ||
|
||
@DisplayName("지원자의 평균 점수 기준으로 오름차순으로 정렬한다.") | ||
@ParameterizedTest | ||
@ValueSource(strings = {"ASC", "asc"}) | ||
void getScoreComparator_asc(String sortByCreatedAt) { | ||
// given | ||
LocalDateTime createdAt = LocalDateTime.of(2024, 10, 2, 20, 0); | ||
ApplicantCard applicantCard1 = ApplicantCardFixture.evaluatedApplicantCard(createdAt); | ||
ApplicantCard applicantCard2 = ApplicantCardFixture.evaluatedApplicantCard(createdAt); | ||
List<ApplicantCard> applicantCards = Arrays.asList(applicantCard1, applicantCard2); | ||
|
||
// when | ||
applicantCards.sort(ApplicantSortOption.getCombinedComparator( | ||
DefaultFilterAndOrderFixture.DEFAULT_SORT_BY_CREATED_AT, | ||
sortByCreatedAt | ||
)); | ||
|
||
// then | ||
assertThat(applicantCards).containsExactly(applicantCard2, applicantCard1); | ||
} | ||
|
||
@DisplayName("지원자의 평균 점수 기준으로 내림차순으로 정렬한다.") | ||
@ParameterizedTest | ||
@ValueSource(strings = {"DESC", "desc"}) | ||
void getScoreComparator_desc(String sortByCreatedAt) { | ||
// given | ||
LocalDateTime createdAt = LocalDateTime.of(2024, 10, 2, 20, 0); | ||
ApplicantCard applicantCard1 = ApplicantCardFixture.evaluatedApplicantCard(createdAt); | ||
ApplicantCard applicantCard2 = ApplicantCardFixture.evaluatedApplicantCard(createdAt); | ||
List<ApplicantCard> applicantCards = Arrays.asList(applicantCard2, applicantCard1); | ||
|
||
// when | ||
applicantCards.sort(ApplicantSortOption.getCombinedComparator( | ||
DefaultFilterAndOrderFixture.DEFAULT_SORT_BY_CREATED_AT, | ||
sortByCreatedAt | ||
)); | ||
|
||
// then | ||
assertThat(applicantCards).containsExactly(applicantCard1, applicantCard2); | ||
} | ||
} | ||
} |
Oops, something went wrong.