-
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
Conversation
Test Results9 tests 9 ✅ 2s ⏱️ Results for commit 9b76ffd. |
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.
화긴했습니당🐥🐥
String professorName | ||
) { | ||
Specification<Subject> condition = getCondition(subjectId, subjectName, subjectCode, classCode, professorName); | ||
List<Subject> subjects = subjectRepository.findAll(condition); |
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()에 인자 넣어서 하는 방식 첨봐서 신기해여
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); | ||
} |
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.
와짱이다
근데 확실히 검색 조건이 늘어날 수록 보수를 계속해야겠네요ㅜ 리팩토링 지점으로 남겨두고 넘어가도 좋을 것 같아여🐥🐥
작업 내용
과목 조회 API를 구현했어요. 과목을 조회할 때, 여러 파라미터를 필터 조건처럼 사용할 수 있게 구현했어요. 꽤 확장성 있게 개발했습니다.
@RequestParam(required = false)
로 설정하여 요청에 없는 파라미터는 null이 주입되도록 했어요.동적으로 DB 쿼리 생성하기
위 목적을 달성하기 위해 고려한 방식이 여러 개 있어요.
1번 방식은 구현할 코드가 많고, 너무 지저분해지고, 유지보수하기 어려운 코드가 될거라 생각해서 배제했어요! 아마 구현하게 된다면 다음과 같은 모습일 것 같아요.
결론은 2번 방식을 선택했어요~ 이게 세 가지 방법 중에 구현 코드가 적고, 그나마 유지보수하기 좋다고 생각했어요. 마음에 들지 않는 부분도 있지만요! 3번째 방식인 QueryDSL은 러닝커브가 높고 의존성이 많이 필요해서 추후에 적용하면 좋을 것 같아요. 2번 방식에 대한 코드는 PR을 확인하면 이해하기 쉬울 것 같아요!
개선이 필요한 점
@RequestParam(required = false)
로 하여 널값을 주입하고 널값이 service까지 전파되도록 코드를 작성했는데, 좀 위험할 수 있을 것 같아요. 사실 다른 방법이 있는지 모르겠는데, 좀더 찾아봐야 할 것 같아요.SubjectSpecifications.java
를 보면 entity의 필드값이 하드코딩된 것을 볼 수 있어요. entity의 필드 값이 변경되면SubjectSpecifications.java
도 변경해야 해요.