Skip to content

Commit 8584d70

Browse files
authored
Merge pull request #95 from dnd-side-project/fix/94-log-growth
2 parents a2157ac + 5e66d65 commit 8584d70

File tree

4 files changed

+61
-46
lines changed

4 files changed

+61
-46
lines changed

โ€Žbuild.gradleโ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ dependencies {
4949
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
5050

5151
// Querydsl
52-
implementation 'com.querydsl:querydsl-jpa:5.0.0:jakarta'
53-
annotationProcessor "com.querydsl:querydsl-apt:5.0.0:jakarta"
52+
implementation 'io.github.openfeign.querydsl:querydsl-jpa:6.10.1'
53+
annotationProcessor 'io.github.openfeign.querydsl:querydsl-apt:6.10.1:jpa'
5454
annotationProcessor "jakarta.annotation:jakarta.annotation-api"
5555
annotationProcessor "jakarta.persistence:jakarta.persistence-api"
5656

โ€Žsrc/main/java/com/example/wini/domain/log/dto/response/ActionChange.javaโ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

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

5-
public record ActionChange(Action action, Long change) {}
5+
public record ActionChange(Action action, Long monthlyChange) {}
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.example.wini.domain.log.dto.response;
22

3-
public record SimpleActionChange(String text, long change) {
3+
public record SimpleActionChange(String text, Long monthlyChange) {
44
public static SimpleActionChange from(ActionChange actionChange) {
5-
return new SimpleActionChange(actionChange.action().getText(), actionChange.change());
5+
if (actionChange == null) {
6+
return new SimpleActionChange(null, null);
7+
}
8+
return new SimpleActionChange(actionChange.action().getText(), actionChange.monthlyChange());
69
}
710
}

โ€Žsrc/main/java/com/example/wini/domain/note/repository/NoteCustomRepositoryImpl.javaโ€Ž

Lines changed: 53 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,17 @@
1010
import com.example.wini.domain.note.domain.SortOrder;
1111
import com.example.wini.domain.template.domain.ActionCategory;
1212
import com.example.wini.domain.template.domain.EmotionType;
13+
import com.querydsl.core.Tuple;
1314
import com.querydsl.core.types.Order;
1415
import com.querydsl.core.types.OrderSpecifier;
15-
import com.querydsl.core.types.Projections;
16-
import com.querydsl.core.types.dsl.BooleanExpression;
17-
import com.querydsl.core.types.dsl.CaseBuilder;
18-
import com.querydsl.core.types.dsl.NumberExpression;
16+
import com.querydsl.core.types.dsl.*;
1917
import com.querydsl.jpa.impl.JPAQueryFactory;
2018
import java.time.DayOfWeek;
2119
import java.time.LocalDate;
2220
import java.time.LocalDateTime;
2321
import java.time.temporal.TemporalAdjusters;
2422
import java.util.ArrayList;
23+
import java.util.Comparator;
2524
import java.util.List;
2625
import java.util.Optional;
2726
import lombok.RequiredArgsConstructor;
@@ -73,41 +72,63 @@ public List<Note> findSavedNotesSortedByCreatedAt(Long memberId, Long roomId, So
7372
@Override
7473
public ActionChange findMostIncreasedPositiveActionChange(Long memberId, Long roomId) {
7574
LocalDate today = LocalDate.now();
76-
NumberExpression<Long> thisMonthNotes = countThisMonthNotes(today);
77-
NumberExpression<Long> lastMonthNotes = countLastMonthNotes(today);
78-
NumberExpression<Long> increaseCount = thisMonthNotes.subtract(lastMonthNotes);
7975

80-
return queryFactory
81-
.select(Projections.constructor(ActionChange.class, action, increaseCount))
76+
NumberExpression<Integer> rawChange = new CaseBuilder()
77+
.when(isCreatedThisMonth(today))
78+
.then(1)
79+
.when(isCreatedLastMonth(today))
80+
.then(-1)
81+
.otherwise(0);
82+
83+
NumberExpression<Long> monthlyChange = rawChange.castToNum(Long.class);
84+
NumberExpression<Long> monthlyChangeSum = monthlyChange.sumLong().coalesce(0L);
85+
86+
List<Tuple> results = queryFactory
87+
.select(action, monthlyChangeSum)
8288
.from(note)
8389
.join(note.action, action)
8490
.join(action.actionCategory, actionCategory)
8591
.where(isThisRoom(roomId)
8692
.and(isReceiver(memberId))
8793
.and(actionCategory.emotionType.eq(EmotionType.POSITIVE)))
88-
.groupBy(action)
89-
.orderBy(increaseCount.desc())
90-
.fetchFirst();
94+
.groupBy(action.id)
95+
.fetch();
96+
97+
return results.stream()
98+
.map(t -> new ActionChange(t.get(action), t.get(monthlyChangeSum)))
99+
.max(Comparator.comparing(ActionChange::monthlyChange))
100+
.orElse(null);
91101
}
92102

93103
@Override
94104
public ActionChange findMostDecreasedNegativeActionChange(Long memberId, Long roomId) {
95105
LocalDate today = LocalDate.now();
96-
NumberExpression<Long> thisMonthNotes = countThisMonthNotes(today);
97-
NumberExpression<Long> lastMonthNotes = countLastMonthNotes(today);
98-
NumberExpression<Long> decreaseCount = thisMonthNotes.subtract(lastMonthNotes);
99106

100-
return queryFactory
101-
.select(Projections.constructor(ActionChange.class, action, decreaseCount))
107+
NumberExpression<Integer> rawChange = new CaseBuilder()
108+
.when(isCreatedThisMonth(today))
109+
.then(1)
110+
.when(isCreatedLastMonth(today))
111+
.then(-1)
112+
.otherwise(0);
113+
114+
NumberExpression<Long> monthlyChange = rawChange.castToNum(Long.class);
115+
NumberExpression<Long> monthlyChangeSum = monthlyChange.sumLong().coalesce(0L);
116+
117+
List<Tuple> results = queryFactory
118+
.select(action, monthlyChangeSum)
102119
.from(note)
103120
.join(note.action, action)
104121
.join(action.actionCategory, actionCategory)
105122
.where(isThisRoom(roomId)
106123
.and(isReceiver(memberId))
107124
.and(actionCategory.emotionType.eq(EmotionType.NEGATIVE)))
108-
.groupBy(action)
109-
.orderBy(decreaseCount.asc())
110-
.fetchFirst();
125+
.groupBy(action.id)
126+
.fetch();
127+
128+
return results.stream()
129+
.map(t -> new ActionChange(t.get(action), t.get(monthlyChangeSum)))
130+
.min(Comparator.comparing(ActionChange::monthlyChange))
131+
.orElse(null);
111132
}
112133

113134
@Override
@@ -190,32 +211,23 @@ public Long countTotalNotesExchanged(Long roomId) {
190211
.fetchFirst();
191212
}
192213

193-
private NumberExpression<Long> countThisMonthNotes(LocalDate today) {
194-
LocalDateTime startOfThisMonth = today.withDayOfMonth(1).atStartOfDay();
195-
LocalDateTime endOfThisMonth = today.plusDays(1).atStartOfDay();
214+
private BooleanExpression isCreatedThisMonth(LocalDate today) {
215+
LocalDate firstDayOfThisMonth = today.withDayOfMonth(1);
216+
217+
LocalDateTime startOfThisMonth = firstDayOfThisMonth.atStartOfDay();
218+
LocalDateTime endOfThisMonth = firstDayOfThisMonth.plusMonths(1).atStartOfDay();
196219

197-
return new CaseBuilder()
198-
.when(note.createdAt.goe(startOfThisMonth).and(note.createdAt.lt(endOfThisMonth)))
199-
.then(1L)
200-
.otherwise(0L)
201-
.sum();
220+
return note.createdAt.goe(startOfThisMonth).and(note.createdAt.lt(endOfThisMonth));
202221
}
203222

204-
private NumberExpression<Long> countLastMonthNotes(LocalDate today) {
205-
LocalDate lastMonthOfToday = today.minusMonths(1);
223+
private BooleanExpression isCreatedLastMonth(LocalDate today) {
224+
LocalDate firstDayOfThisMonth = today.withDayOfMonth(1);
225+
LocalDate firstDayOfLastMonth = firstDayOfThisMonth.minusMonths(1);
206226

207-
LocalDateTime startOfLastMonth = lastMonthOfToday.withDayOfMonth(1).atStartOfDay();
208-
LocalDateTime endOfLastMonth = lastMonthOfToday.plusDays(1).atStartOfDay();
209-
if (today.getDayOfMonth() == today.lengthOfMonth()) {
210-
endOfLastMonth =
211-
lastMonthOfToday.with(TemporalAdjusters.lastDayOfMonth()).atStartOfDay();
212-
}
227+
LocalDateTime startOfLastMonth = firstDayOfLastMonth.atStartOfDay();
228+
LocalDateTime endOfLastMonth = firstDayOfThisMonth.atStartOfDay();
213229

214-
return new CaseBuilder()
215-
.when(note.createdAt.goe(startOfLastMonth).and(note.createdAt.lt(endOfLastMonth)))
216-
.then(1L)
217-
.otherwise(0L)
218-
.sum();
230+
return note.createdAt.goe(startOfLastMonth).and(note.createdAt.lt(endOfLastMonth));
219231
}
220232

221233
private BooleanExpression isCreatedLatest() {

0 commit comments

Comments
ย (0)