-
Notifications
You must be signed in to change notification settings - Fork 3
Refactor/user problem result query #186
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
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8eb45f9
refactor : 기본 언어가 저장되지 않은 유저에 대해 조회시 1번 언어로 자동 설정 및 조회되도록 설정
minjee2758 3d4b88c
refactor : 유저 정보 변경 시, 닉네임 중복 방지
minjee2758 36a7244
refactor : /api/users/daily-solved 앤드포인트에 문제 번호 리스트 포함
minjee2758 1258cfb
refactor : 메일 서비스 undo
minjee2758 1cbb68a
refactor :type
minjee2758 496566d
쿼리 수정
minjee2758 c6cd7f8
Merge branch 'dev' into refactor/userProblem-result-query
minjee2758 ca08a77
오타 수정
minjee2758 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 hidden or 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 |
|---|---|---|
|
|
@@ -2,16 +2,16 @@ | |
|
|
||
|
|
||
| import java.time.LocalDate; | ||
| import java.util.HashSet; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import org.ezcode.codetest.domain.submission.dto.DailyCorrectCount; | ||
| import org.ezcode.codetest.domain.submission.model.entity.QUserProblemResult; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| import com.querydsl.core.group.GroupBy; | ||
| import com.querydsl.core.types.Projections; | ||
| import com.querydsl.core.types.dsl.NumberExpression; | ||
| import com.querydsl.core.types.dsl.Expressions; | ||
| import com.querydsl.jpa.impl.JPAQueryFactory; | ||
|
|
||
|
|
@@ -29,24 +29,38 @@ public List<DailyCorrectCount> countCorrectByUserGroupedByDate(Long userId) { | |
| QUserProblemResult upr = QUserProblemResult.userProblemResult; | ||
|
|
||
| var date = Expressions.dateTemplate(LocalDate.class, "DATE({0})", upr.modifiedAt); | ||
| NumberExpression<Long> countDistinctProblem = upr.problem.id.countDistinct(); | ||
|
|
||
| return queryFactory | ||
| var results = queryFactory | ||
| .select( | ||
| date, | ||
| upr.problem.id.countDistinct(), | ||
| upr.problem.id | ||
| ) | ||
| .from(upr) | ||
| .where( | ||
| upr.user.id.eq(userId), | ||
| upr.isCorrect.eq(true) | ||
| ) | ||
| .orderBy(date.asc()) | ||
| .transform( | ||
| GroupBy.groupBy(date).list( | ||
| Projections.constructor( | ||
| DailyCorrectCount.class, | ||
| date, | ||
| countDistinctProblem, | ||
| GroupBy.set(upr.problem.id) | ||
| ) | ||
| ) | ||
| ); | ||
| .fetch(); | ||
|
|
||
| // 결과를 수동으로 그룹화해야함 | ||
| Map<LocalDate, Set<Long>> problemIdsByDate = new LinkedHashMap<>(); | ||
| Map<LocalDate, Long> countsByDate = new LinkedHashMap<>(); | ||
|
|
||
| for (var result : results) { | ||
| LocalDate resultDate = result.get(0, LocalDate.class); | ||
| Long problemId = result.get(2, Long.class); | ||
|
|
||
| problemIdsByDate.computeIfAbsent(resultDate, k -> new HashSet<>()).add(problemId); | ||
| countsByDate.put(resultDate, (long) problemIdsByDate.get(resultDate).size()); | ||
| } | ||
|
Comment on lines
+48
to
+56
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. 선택 컬럼 인덱스 불일치 및 불필요한 위 수정으로 select 컬럼이 2개가 되므로 아래처럼 인덱스와 반환 구성을 정리하세요. - Map<LocalDate, Long> countsByDate = new LinkedHashMap<>();
-
- for (var result : results) {
- LocalDate resultDate = result.get(0, LocalDate.class);
- Long problemId = result.get(2, Long.class);
-
- problemIdsByDate.computeIfAbsent(resultDate, k -> new HashSet<>()).add(problemId);
- countsByDate.put(resultDate, (long) problemIdsByDate.get(resultDate).size());
- }
-
- return problemIdsByDate.entrySet().stream()
- .map(entry -> new DailyCorrectCount(
- entry.getKey(),
- countsByDate.get(entry.getKey()),
- entry.getValue()
- ))
- .collect(Collectors.toList());
+ for (var result : results) {
+ LocalDate resultDate = result.get(0, LocalDate.class);
+ Long problemId = result.get(1, Long.class);
+ problemIdsByDate
+ .computeIfAbsent(resultDate, k -> new HashSet<>())
+ .add(problemId);
+ }
+
+ return problemIdsByDate.entrySet().stream()
+ .map(entry -> new DailyCorrectCount(
+ entry.getKey(),
+ (long) entry.getValue().size(),
+ entry.getValue()
+ ))
+ .collect(Collectors.toList());Also applies to: 58-64 🤖 Prompt for AI Agents |
||
|
|
||
| return problemIdsByDate.entrySet().stream() | ||
| .map(entry -> new DailyCorrectCount( | ||
| entry.getKey(), | ||
| countsByDate.get(entry.getKey()), | ||
| entry.getValue() | ||
| )) | ||
| .collect(Collectors.toList()); | ||
| } | ||
| } | ||
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.
집계 함수와 비집계 컬럼 동시 선택으로 JPQL/SQL 오류 위험이 큽니다.
GROUP BY 없이
countDistinct()와date,problem.id를 함께 select하면 JPQL 규약 위반이며, 다수 DB에서 실행 오류가 납니다. 수기 그룹핑으로 갈 거라면 집계를 쿼리에서 제거해야 합니다.아래 최소 수정으로 집계 컬럼을 제거하세요(아래 인덱스 수정은 다음 코멘트 참조).
Also applies to: 44-44