Skip to content
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 10 commits into from
Jan 25, 2025
Merged

feat: 과목 조회 API 구현 #5

merged 10 commits into from
Jan 25, 2025

Conversation

3Juhwan
Copy link
Member

@3Juhwan 3Juhwan commented Jan 20, 2025

작업 내용

과목 조회 API를 구현했어요. 과목을 조회할 때, 여러 파라미터를 필터 조건처럼 사용할 수 있게 구현했어요. 꽤 확장성 있게 개발했습니다.

  • Api 클래스에 @RequestParam(required = false)로 설정하여 요청에 없는 파라미터는 null이 주입되도록 했어요.
  • JPA의 Specification을 사용해서 없는 파라미터를 동적으로 제외하고 DB 쿼리를 생성하게 했어요.

동적으로 DB 쿼리 생성하기

위 목적을 달성하기 위해 고려한 방식이 여러 개 있어요.

  1. 순수 자바를 사용하여 동적 쿼리 작성하기
  2. Jpa Specification
  3. QueryDSL

1번 방식은 구현할 코드가 많고, 너무 지저분해지고, 유지보수하기 어려운 코드가 될거라 생각해서 배제했어요! 아마 구현하게 된다면 다음과 같은 모습일 것 같아요.

String sql = "select * from subject where ";
if (subjectId != null) {
    sql += "id = " + subjectId;
}
if (subjectCode != null) {
    sql += "subjectCode = " + subjectCode;
}
...

결론은 2번 방식을 선택했어요~ 이게 세 가지 방법 중에 구현 코드가 적고, 그나마 유지보수하기 좋다고 생각했어요. 마음에 들지 않는 부분도 있지만요! 3번째 방식인 QueryDSL은 러닝커브가 높고 의존성이 많이 필요해서 추후에 적용하면 좋을 것 같아요. 2번 방식에 대한 코드는 PR을 확인하면 이해하기 쉬울 것 같아요!

개선이 필요한 점

  • @RequestParam(required = false)로 하여 널값을 주입하고 널값이 service까지 전파되도록 코드를 작성했는데, 좀 위험할 수 있을 것 같아요. 사실 다른 방법이 있는지 모르겠는데, 좀더 찾아봐야 할 것 같아요.
  • Jpa Specification를 사용해도 관리 포인트가 많더라고요. SubjectSpecifications.java를 보면 entity의 필드값이 하드코딩된 것을 볼 수 있어요. entity의 필드 값이 변경되면 SubjectSpecifications.java도 변경해야 해요.

Copy link

Test Results

9 tests   9 ✅  2s ⏱️
5 suites  0 💤
5 files    0 ❌

Results for commit 9b76ffd.

@3Juhwan 3Juhwan requested a review from boyekim January 25, 2025 02:46
Copy link
Member

@boyekim boyekim left a 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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요게 만약 subjectName만 있다면 findBySubjectName을 사용했을 때와 같은 쿼리가 나가는지 궁금해지네용
findAll()에 인자 넣어서 하는 방식 첨봐서 신기해여

Comment on lines +5 to +30
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);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

와짱이다
근데 확실히 검색 조건이 늘어날 수록 보수를 계속해야겠네요ㅜ 리팩토링 지점으로 남겨두고 넘어가도 좋을 것 같아여🐥🐥

@boyekim boyekim merged commit e4a5b53 into main Jan 25, 2025
3 checks passed
@boyekim boyekim deleted the api-query-subject branch January 25, 2025 06:38
@3Juhwan 3Juhwan self-assigned this Jan 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants