Skip to content
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
bb29cba
fix: @QueryProjection으로 잘못된 타입 쿼리시 컴파일 에러나도록 수정
ohhamma Aug 29, 2025
6d365e8
fix: ActionChange dto에 값 삽입시 Long 타입 맞춰주기
ohhamma Aug 29, 2025
93bbab4
fix: 복잡한 쿼리에 as로 별칭 추가
ohhamma Aug 29, 2025
d421633
fix: orderBy에서 발생하는 문법 오류로 별칭 제거
ohhamma Aug 29, 2025
27229c1
fix: query 결과를 가져와서 자바에서 정렬하도록 수정
ohhamma Aug 29, 2025
7dda8c2
fix: 튜플로 쿼리 결과 반환 후 스트림으로 dto에 값 삽입하도록 수정
ohhamma Aug 29, 2025
2a53534
fix: 하나릐 CaseBuilder로 계산하도록 수정
ohhamma Aug 29, 2025
f14c16a
fix: 날짜 계산하는 로직 수정
ohhamma Aug 29, 2025
3d0aaf6
fix: 사용하지 않는 로직 주석 처리해보기
ohhamma Aug 29, 2025
0169cdc
fix: @QueryProjection으로 구현 재시도
ohhamma Aug 29, 2025
01d34b4
fix: querydsl이 인식하는 DateTimeExpression로 명시적 변환
ohhamma Aug 29, 2025
665e8ef
fix: 모든 쿼리 로직을 하나의 메소드에 작성
ohhamma Aug 29, 2025
adef807
fix: Projections.constructor() 사용하도록 수정
ohhamma Aug 29, 2025
8fca830
fix: actionChange 필드 getter 수정
ohhamma Aug 29, 2025
9b6ca64
fix: calculateMonthlyChange 함수 사용하도록 수정
ohhamma Aug 29, 2025
7e9bdb3
fix: CaseBuilder를 독립적으로 수행하도록 수정
ohhamma Aug 29, 2025
c6f0728
fix: 하나의 CaseBuilder로 계산하도록 수정
ohhamma Aug 29, 2025
6c27216
fix: Integer 명시하도록 수정
ohhamma Aug 29, 2025
a7b8127
fix: dto에 필드 타입 long으로 수정
ohhamma Aug 29, 2025
79fdfe8
fix: NumberExpression 타입 Long으로 수정
ohhamma Aug 29, 2025
291e860
fix: numberTemplate으로 타입 고정
ohhamma Aug 29, 2025
427e0f8
fix: dto 필드 타입 Long으로 변경
ohhamma Aug 29, 2025
76c5272
fix: Tuple을 사용하도록 수정
ohhamma Aug 29, 2025
59e39b3
fix: coalesce을 사용하여 null인 경우 0으로 나타나게 수정
ohhamma Aug 29, 2025
5dc6b0c
fix: longValue로 타입 명시
ohhamma Aug 29, 2025
c5eee07
fix: querydsl6으로 버전 업그레이드하여 코드 수정
ohhamma Aug 29, 2025
daf2250
fix: coalesce 추가
ohhamma Aug 29, 2025
2b0baaf
fix: querydsl5로 다시 다운그레이드
ohhamma Aug 29, 2025
d41e4d5
fix: hibernate 6.2로 다운그레이드
ohhamma Aug 29, 2025
0f2f8bf
fix: querydsl6으로 업그레이드하고 castToNum으로 명시적 캐스팅
ohhamma Aug 29, 2025
5e66d65
fix: ActionChange null 들어오면 모든 필드 null값으로 채움
ohhamma Aug 29, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

import com.example.wini.domain.template.domain.Action;

public record ActionChange(Action action, Long change) {}
public record ActionChange(Action action, long monthlyChange) {}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.example.wini.domain.log.dto.response;

public record SimpleActionChange(String text, long change) {
public record SimpleActionChange(String text, long monthlyChange) {
public static SimpleActionChange from(ActionChange actionChange) {
return new SimpleActionChange(actionChange.action().getText(), actionChange.change());
return new SimpleActionChange(actionChange.action().getText(), actionChange.monthlyChange());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
import com.querydsl.core.types.Projections;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.core.types.dsl.CaseBuilder;
import com.querydsl.core.types.dsl.Expressions;
import com.querydsl.core.types.dsl.NumberExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -73,41 +75,55 @@ public List<Note> findSavedNotesSortedByCreatedAt(Long memberId, Long roomId, So
@Override
public ActionChange findMostIncreasedPositiveActionChange(Long memberId, Long roomId) {
LocalDate today = LocalDate.now();
NumberExpression<Long> thisMonthNotes = countThisMonthNotes(today);
NumberExpression<Long> lastMonthNotes = countLastMonthNotes(today);
NumberExpression<Long> increaseCount = thisMonthNotes.subtract(lastMonthNotes);
NumberExpression<Long> monthlyChange = new CaseBuilder()
.when(isCreatedThisMonth(today))
.then(1L)
.when(isCreatedLastMonth(today))
.then(-1L)
.otherwise(0L);

return queryFactory
.select(Projections.constructor(ActionChange.class, action, increaseCount))
List<ActionChange> results = queryFactory
.select(Projections.constructor(
ActionChange.class, action, Expressions.numberTemplate(Long.class, "sum({0})", monthlyChange)))
.from(note)
.join(note.action, action)
.join(action.actionCategory, actionCategory)
.where(isThisRoom(roomId)
.and(isReceiver(memberId))
.and(actionCategory.emotionType.eq(EmotionType.POSITIVE)))
.groupBy(action)
.orderBy(increaseCount.desc())
.fetchFirst();
.groupBy(action.id)
.fetch();

return results.stream()
.max(Comparator.comparing(ActionChange::monthlyChange))
.orElse(null);
}

@Override
public ActionChange findMostDecreasedNegativeActionChange(Long memberId, Long roomId) {
LocalDate today = LocalDate.now();
NumberExpression<Long> thisMonthNotes = countThisMonthNotes(today);
NumberExpression<Long> lastMonthNotes = countLastMonthNotes(today);
NumberExpression<Long> decreaseCount = thisMonthNotes.subtract(lastMonthNotes);
NumberExpression<Long> monthlyChange = new CaseBuilder()
.when(isCreatedThisMonth(today))
.then(1L)
.when(isCreatedLastMonth(today))
.then(-1L)
.otherwise(0L);

return queryFactory
.select(Projections.constructor(ActionChange.class, action, decreaseCount))
List<ActionChange> results = queryFactory
.select(Projections.constructor(
ActionChange.class, action, Expressions.numberTemplate(Long.class, "sum({0})", monthlyChange)))
.from(note)
.join(note.action, action)
.join(action.actionCategory, actionCategory)
.where(isThisRoom(roomId)
.and(isReceiver(memberId))
.and(actionCategory.emotionType.eq(EmotionType.NEGATIVE)))
.groupBy(action)
.orderBy(decreaseCount.asc())
.fetchFirst();
.groupBy(action.id)
.fetch();

return results.stream()
.min(Comparator.comparing(ActionChange::monthlyChange))
.orElse(null);
}

@Override
Expand Down Expand Up @@ -190,32 +206,23 @@ public Long countTotalNotesExchanged(Long roomId) {
.fetchFirst();
}

private NumberExpression<Long> countThisMonthNotes(LocalDate today) {
LocalDateTime startOfThisMonth = today.withDayOfMonth(1).atStartOfDay();
LocalDateTime endOfThisMonth = today.plusDays(1).atStartOfDay();
private BooleanExpression isCreatedThisMonth(LocalDate today) {
LocalDate firstDayOfThisMonth = today.withDayOfMonth(1);

return new CaseBuilder()
.when(note.createdAt.goe(startOfThisMonth).and(note.createdAt.lt(endOfThisMonth)))
.then(1L)
.otherwise(0L)
.sum();
LocalDateTime startOfThisMonth = firstDayOfThisMonth.atStartOfDay();
LocalDateTime endOfThisMonth = firstDayOfThisMonth.plusMonths(1).atStartOfDay();

return note.createdAt.goe(startOfThisMonth).and(note.createdAt.lt(endOfThisMonth));
}

private NumberExpression<Long> countLastMonthNotes(LocalDate today) {
LocalDate lastMonthOfToday = today.minusMonths(1);
private BooleanExpression isCreatedLastMonth(LocalDate today) {
LocalDate firstDayOfThisMonth = today.withDayOfMonth(1);
LocalDate firstDayOfLastMonth = firstDayOfThisMonth.minusMonths(1);

LocalDateTime startOfLastMonth = lastMonthOfToday.withDayOfMonth(1).atStartOfDay();
LocalDateTime endOfLastMonth = lastMonthOfToday.plusDays(1).atStartOfDay();
if (today.getDayOfMonth() == today.lengthOfMonth()) {
endOfLastMonth =
lastMonthOfToday.with(TemporalAdjusters.lastDayOfMonth()).atStartOfDay();
}
LocalDateTime startOfLastMonth = firstDayOfLastMonth.atStartOfDay();
LocalDateTime endOfLastMonth = firstDayOfThisMonth.atStartOfDay();

return new CaseBuilder()
.when(note.createdAt.goe(startOfLastMonth).and(note.createdAt.lt(endOfLastMonth)))
.then(1L)
.otherwise(0L)
.sum();
return note.createdAt.goe(startOfLastMonth).and(note.createdAt.lt(endOfLastMonth));
}

private BooleanExpression isCreatedLatest() {
Expand Down
Loading