-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat: 과목 조회 API 구현 #5
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
134ff79
refactor: subject 필드명 수정
3Juhwan bdd3911
refactor: dto 패키지 추가
3Juhwan 296211c
chore: 테스트 설정 값 추가
3Juhwan 7cb0ada
feat: 다중 조건에 따라 과목 검색 기능 구현
3Juhwan 6b5afa3
Merge branch 'main' into api-query-subject
3Juhwan 1490b75
refactor: 테스트 클래스명 수정
3Juhwan 6fe710a
refactor: 검증 부분을 리플렉션을 활용하여 가독성 개선
3Juhwan e7f84a8
fix: 불필요한 주석 해제
3Juhwan 1f54e49
refactor: import 문 수정
3Juhwan 9b76ffd
chore: 머지 충돌 해결
3Juhwan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
3 changes: 2 additions & 1 deletion
3
src/main/java/kr/allcll/seatfinder/subject/SubjectRepository.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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
package kr.allcll.seatfinder.subject; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; | ||
|
||
public interface SubjectRepository extends JpaRepository<Subject, Long> { | ||
public interface SubjectRepository extends JpaRepository<Subject, Long>, JpaSpecificationExecutor<Subject> { | ||
|
||
} |
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
31 changes: 31 additions & 0 deletions
31
src/main/java/kr/allcll/seatfinder/subject/SubjectSpecifications.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,31 @@ | ||
package kr.allcll.seatfinder.subject; | ||
|
||
import org.springframework.data.jpa.domain.Specification; | ||
|
||
public class SubjectSpecifications { | ||
|
||
public static Specification<Subject> hasSubjectId(Long subjectId) { | ||
return (root, query, builder) -> | ||
subjectId == null ? null : builder.equal(root.get("id"), subjectId); | ||
} | ||
|
||
public static Specification<Subject> hasSubjectName(String subjectName) { | ||
return (root, query, builder) -> | ||
subjectName == null ? null : builder.equal(root.get("subjectName"), subjectName); | ||
} | ||
|
||
public static Specification<Subject> hasSubjectCode(String subjectCode) { | ||
return (root, query, builder) -> | ||
subjectCode == null ? null : builder.equal(root.get("subjectCode"), subjectCode); | ||
} | ||
|
||
public static Specification<Subject> hasClassCode(String classCode) { | ||
return (root, query, builder) -> | ||
classCode == null ? null : builder.equal(root.get("classCode"), classCode); | ||
} | ||
|
||
public static Specification<Subject> hasProfessorName(String professorName) { | ||
return (root, query, builder) -> | ||
professorName == null ? null : builder.equal(root.get("professorName"), professorName); | ||
} | ||
Comment on lines
+5
to
+30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 와짱이다 |
||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/kr/allcll/seatfinder/subject/dto/SubjectResponse.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,60 @@ | ||
package kr.allcll.seatfinder.subject.dto; | ||
|
||
import kr.allcll.seatfinder.subject.Subject; | ||
|
||
public record SubjectResponse( | ||
Long subjectId, | ||
String offeringUniversity, | ||
String offeringDepartment, | ||
String subjectCode, | ||
String classCode, | ||
String subjectName, | ||
String subjectType, | ||
String electiveArea, | ||
String academicYearSemester, | ||
String credit, | ||
String theoryHours, | ||
String practiceHours, | ||
String classType, | ||
String exchangeEligible, | ||
String schedule, | ||
String lectureRoom, | ||
String professorName, | ||
String supervisingDepartment, | ||
String notesAndRestrictions, | ||
String classCategory, | ||
String onlineLecture, | ||
String lectureLanguage, | ||
String foreignerOnly, | ||
String domesticOnly | ||
) { | ||
|
||
public static SubjectResponse from(Subject subject) { | ||
return new SubjectResponse( | ||
subject.getId(), | ||
subject.getOfferingUniversity(), | ||
subject.getOfferingDepartment(), | ||
subject.getSubjectCode(), | ||
subject.getClassCode(), | ||
subject.getSubjectName(), | ||
subject.getSubjectType(), | ||
subject.getElectiveArea(), | ||
subject.getAcademicYearSemester(), | ||
subject.getCredit(), | ||
subject.getTheoryHours(), | ||
subject.getPracticeHours(), | ||
subject.getClassType(), | ||
subject.getExchangeEligible(), | ||
subject.getSchedule(), | ||
subject.getLectureRoom(), | ||
subject.getProfessorName(), | ||
subject.getSupervisingDepartment(), | ||
subject.getNotesAndRestrictions(), | ||
subject.getClassCategory(), | ||
subject.getOnlineLecture(), | ||
subject.getLectureLanguage(), | ||
subject.getForeignerOnly(), | ||
subject.getDomesticOnly() | ||
); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/kr/allcll/seatfinder/subject/dto/SubjectsResponse.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,15 @@ | ||
package kr.allcll.seatfinder.subject.dto; | ||
|
||
import java.util.List; | ||
import kr.allcll.seatfinder.subject.Subject; | ||
|
||
public record SubjectsResponse( | ||
List<SubjectResponse> subjectResponses | ||
) { | ||
|
||
public static SubjectsResponse from(List<Subject> subjects) { | ||
return new SubjectsResponse(subjects.stream() | ||
.map(SubjectResponse::from) | ||
.toList()); | ||
} | ||
} |
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
90 changes: 90 additions & 0 deletions
90
src/test/java/kr/allcll/seatfinder/subject/SubjectServiceTest.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,90 @@ | ||
package kr.allcll.seatfinder.subject; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.tuple; | ||
|
||
import java.util.List; | ||
import kr.allcll.seatfinder.subject.dto.SubjectResponse; | ||
import kr.allcll.seatfinder.subject.dto.SubjectsResponse; | ||
import kr.allcll.seatfinder.support.fixture.SubjectFixture; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; | ||
|
||
@SpringBootTest(webEnvironment = WebEnvironment.NONE) | ||
class SubjectServiceTest { | ||
|
||
@Autowired | ||
private SubjectService subjectService; | ||
|
||
@Autowired | ||
private SubjectRepository subjectRepository; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
subjectRepository.deleteAllInBatch(); | ||
initializeSubjects(); | ||
} | ||
|
||
private void initializeSubjects() { | ||
subjectRepository.saveAll( | ||
List.of( | ||
SubjectFixture.createSubject(null, "컴퓨터구조", "003278", "001", "유재석"), | ||
SubjectFixture.createSubject(null, "운영체제", "003279", "001", "노홍철"), | ||
SubjectFixture.createSubject(null, "자료구조", "003280", "001", "하하"), | ||
SubjectFixture.createSubject(null, "알고리즘", "003281", "001", "길"), | ||
SubjectFixture.createSubject(null, "컴퓨터구조", "003278", "002", "정형돈"), | ||
SubjectFixture.createSubject(null, "운영체제", "003279", "002", "나영석"), | ||
SubjectFixture.createSubject(null, "자료구조", "003280", "003", "박명수"), | ||
SubjectFixture.createSubject(null, "알고리즘", "003281", "004", "전진") | ||
) | ||
); | ||
} | ||
|
||
@DisplayName("과목명으로 과목을 조회한다.") | ||
@Test | ||
void findSubjectByQueryTest() { | ||
SubjectsResponse subjectsResponse = subjectService.findSubjectsByCondition(null, "컴퓨터구조", null, null, null); | ||
|
||
assertThat(subjectsResponse.subjectResponses()).hasSize(2) | ||
.extracting( | ||
SubjectResponse::subjectName, | ||
SubjectResponse::subjectCode, | ||
SubjectResponse::classCode, | ||
SubjectResponse::professorName | ||
) | ||
.containsExactly( | ||
tuple("컴퓨터구조", "003278", "001", "유재석"), | ||
tuple("컴퓨터구조", "003278", "002", "정형돈") | ||
); | ||
} | ||
|
||
@DisplayName("학수번호, 분반, 교수명으로 과목을 조회한다.") | ||
@Test | ||
void findSubjectByQueryTest2() { | ||
SubjectsResponse subjectsResponse = subjectService.findSubjectsByCondition(null, null, "003279", "001", "노홍철"); | ||
|
||
assertThat(subjectsResponse.subjectResponses()).hasSize(1) | ||
.extracting( | ||
SubjectResponse::subjectName, | ||
SubjectResponse::subjectCode, | ||
SubjectResponse::classCode, | ||
SubjectResponse::professorName | ||
) | ||
.containsExactly( | ||
tuple("운영체제", "003279", "001", "노홍철") | ||
); | ||
} | ||
|
||
@DisplayName("존재하지 않는 조건으로 과목을 조회한다.") | ||
@Test | ||
void findSubjectByQueryTest3() { | ||
SubjectsResponse subjectsResponse = subjectService.findSubjectsByCondition(100L, null, "003279", "001", | ||
"유재석"); | ||
|
||
assertThat(subjectsResponse.subjectResponses()).hasSize(0); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/test/java/kr/allcll/seatfinder/support/fixture/SubjectFixture.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,17 @@ | ||
package kr.allcll.seatfinder.support.fixture; | ||
|
||
import kr.allcll.seatfinder.subject.Subject; | ||
|
||
public class SubjectFixture { | ||
|
||
public static Subject createSubject( | ||
Long subjectId, | ||
String subjectName, | ||
String subjectCode, | ||
String classCode, | ||
String professorName | ||
) { | ||
return new Subject(subjectId, "", "", subjectCode, classCode, subjectName, "", "", "", "", "", "", "", "", "", | ||
"", professorName, "", "", "", "", "", "", ""); | ||
} | ||
} |
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,20 @@ | ||
spring: | ||
datasource: | ||
driver-class-name: org.h2.Driver | ||
url: 'jdbc:h2:mem:test' | ||
username: sa | ||
|
||
h2: | ||
console: | ||
enabled: true | ||
path: /h2-console | ||
|
||
jpa: | ||
database-platform: org.hibernate.dialect.H2Dialect | ||
hibernate: | ||
ddl-auto: create-drop | ||
properties: | ||
hibernate: | ||
dialect: org.hibernate.dialect.H2Dialect | ||
format_sql: true | ||
show_sql: true |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
요게 만약 subjectName만 있다면 findBySubjectName을 사용했을 때와 같은 쿼리가 나가는지 궁금해지네용
findAll()에 인자 넣어서 하는 방식 첨봐서 신기해여