Skip to content

Commit 90f40ca

Browse files
authored
refactor: 모집글 QueryDsl (#244)
* refactor(recruit-board): 모집글 레포지토리 수정 * refactor(recruit-board): sonar qube 이슈 해결 * refactor(recruit-board): query 최적화
1 parent 92fea60 commit 90f40ca

File tree

1 file changed

+57
-61
lines changed

1 file changed

+57
-61
lines changed

src/main/java/com/somemore/recruitboard/repository/RecruitBoardRepositoryImpl.java

Lines changed: 57 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package com.somemore.recruitboard.repository;
22

3-
import static com.somemore.location.domain.QLocation.location;
4-
import static com.somemore.recruitboard.domain.QRecruitBoard.recruitBoard;
5-
63
import com.querydsl.core.types.ConstructorExpression;
74
import com.querydsl.core.types.OrderSpecifier;
85
import com.querydsl.core.types.Projections;
@@ -12,14 +9,15 @@
129
import com.somemore.center.domain.QCenter;
1310
import com.somemore.location.domain.QLocation;
1411
import com.somemore.location.utils.GeoUtils;
15-
import com.somemore.recruitboard.domain.*;
12+
import com.somemore.recruitboard.domain.QRecruitBoard;
13+
import com.somemore.recruitboard.domain.RecruitBoard;
14+
import com.somemore.recruitboard.domain.RecruitStatus;
15+
import com.somemore.recruitboard.domain.VolunteerCategory;
1616
import com.somemore.recruitboard.dto.condition.RecruitBoardNearByCondition;
1717
import com.somemore.recruitboard.dto.condition.RecruitBoardSearchCondition;
1818
import com.somemore.recruitboard.repository.mapper.RecruitBoardDetail;
1919
import com.somemore.recruitboard.repository.mapper.RecruitBoardWithCenter;
2020
import com.somemore.recruitboard.repository.mapper.RecruitBoardWithLocation;
21-
22-
import java.util.ArrayList;
2321
import java.util.List;
2422
import java.util.Optional;
2523
import java.util.UUID;
@@ -36,9 +34,13 @@
3634
public class RecruitBoardRepositoryImpl implements RecruitBoardRepository {
3735

3836
private final RecruitBoardJpaRepository recruitBoardJpaRepository;
39-
// private final RecruitBoardDocumentRepository documentRepository;
37+
// private final RecruitBoardDocumentRepository documentRepository;
4038
private final JPAQueryFactory queryFactory;
4139

40+
private static final QRecruitBoard recruitBoard = QRecruitBoard.recruitBoard;
41+
private static final QLocation location = QLocation.location;
42+
private static final QCenter center = QCenter.center;
43+
4244
@Override
4345
public RecruitBoard save(RecruitBoard recruitBoard) {
4446
return recruitBoardJpaRepository.save(recruitBoard);
@@ -51,28 +53,29 @@ public List<RecruitBoard> saveAll(List<RecruitBoard> recruitBoards) {
5153

5254
@Override
5355
public Optional<RecruitBoard> findById(Long id) {
54-
QRecruitBoard recruitBoard = QRecruitBoard.recruitBoard;
56+
57+
BooleanExpression exp = idEq(id)
58+
.and(isNotDeleted());
5559

5660
RecruitBoard result = queryFactory
5761
.selectFrom(recruitBoard)
58-
.where(isNotDeleted().and(idEq(id)))
62+
.where(exp)
5963
.fetchOne();
6064

6165
return Optional.ofNullable(result);
6266
}
6367

6468
@Override
6569
public List<Long> findNotCompletedIdsByCenterId(UUID centerId) {
66-
QRecruitBoard recruitBoard = QRecruitBoard.recruitBoard;
70+
71+
BooleanExpression exp = centerIdEq(centerId)
72+
.and(isNotCompleted())
73+
.and(isNotDeleted());
6774

6875
return queryFactory
6976
.select(recruitBoard.id)
7077
.from(recruitBoard)
71-
.where(
72-
recruitBoard.centerId.eq(centerId)
73-
.and(isNotCompleted())
74-
.and(isNotDeleted())
75-
)
78+
.where(exp)
7679
.fetch();
7780
}
7881

@@ -89,35 +92,37 @@ public List<RecruitBoard> findAllByIds(List<Long> ids) {
8992

9093
@Override
9194
public Optional<RecruitBoardWithLocation> findWithLocationById(Long id) {
92-
QRecruitBoard recruitBoard = QRecruitBoard.recruitBoard;
93-
QLocation location = QLocation.location;
94-
95-
return Optional.ofNullable(
96-
queryFactory.select(
97-
getRecruitBoardWithLocationConstructorExpression(recruitBoard, location))
98-
.from(recruitBoard)
99-
.join(location).on(recruitBoard.locationId.eq(location.id))
100-
.where(isNotDeleted().and(idEq(id)))
101-
.fetchOne());
95+
96+
BooleanExpression exp = idEq(id)
97+
.and(isNotDeleted());
98+
99+
RecruitBoardWithLocation result = queryFactory
100+
.select(getRecruitBoardWithLocationConstructorExpression())
101+
.from(recruitBoard)
102+
.join(location)
103+
.on(recruitBoard.locationId.eq(location.id))
104+
.where(exp)
105+
.fetchOne();
106+
107+
return Optional.ofNullable(result);
102108
}
103109

104110
@Override
105111
public Page<RecruitBoardWithCenter> findAllWithCenter(RecruitBoardSearchCondition condition) {
106-
QRecruitBoard recruitBoard = QRecruitBoard.recruitBoard;
107-
QCenter center = QCenter.center;
108112

109-
Pageable pageable = condition.pageable();
110-
BooleanExpression predicate = isNotDeleted()
113+
BooleanExpression exp = isNotDeleted()
111114
.and(keywordEq(condition.keyword()))
112115
.and(volunteerCategoryEq(condition.category()))
113116
.and(regionEq(condition.region()))
114117
.and(admittedEq(condition.admitted()))
115118
.and(statusEq(condition.status()));
116119

120+
Pageable pageable = condition.pageable();
121+
117122
List<RecruitBoardWithCenter> content = queryFactory
118-
.select(getRecruitBoardWithCenterConstructorExpression(recruitBoard, center))
123+
.select(getRecruitBoardWithCenterConstructorExpression())
119124
.from(recruitBoard)
120-
.where(predicate)
125+
.where(exp)
121126
.join(center).on(recruitBoard.centerId.eq(center.id))
122127
.offset(pageable.getOffset())
123128
.limit(pageable.getPageSize())
@@ -127,30 +132,28 @@ public Page<RecruitBoardWithCenter> findAllWithCenter(RecruitBoardSearchConditio
127132
JPAQuery<Long> countQuery = queryFactory
128133
.select(recruitBoard.count())
129134
.from(recruitBoard)
130-
.where(predicate);
135+
.where(exp)
136+
.join(center).on(recruitBoard.centerId.eq(center.id));
131137

132138
return PageableExecutionUtils.getPage(content, pageable, countQuery::fetchOne);
133139
}
134140

135141
@Override
136142
public Page<RecruitBoardDetail> findAllNearby(RecruitBoardNearByCondition condition) {
137-
QRecruitBoard recruitBoard = QRecruitBoard.recruitBoard;
138-
QLocation location = QLocation.location;
139-
QCenter center = QCenter.center;
140-
141-
Pageable pageable = condition.pageable();
142143

143-
BooleanExpression predicate = locationBetween(condition)
144+
BooleanExpression exp = locationBetween(condition)
144145
.and(keywordEq(condition.keyword()))
145146
.and(statusEq(condition.status()))
146147
.and(isNotDeleted());
147148

149+
Pageable pageable = condition.pageable();
150+
148151
List<RecruitBoardDetail> content = queryFactory
149-
.select(getRecruitBoardDetailConstructorExpression(recruitBoard, location, center))
152+
.select(getRecruitBoardDetailConstructorExpression())
150153
.from(recruitBoard)
151154
.join(location).on(recruitBoard.locationId.eq(location.id))
152155
.join(center).on(recruitBoard.centerId.eq(center.id))
153-
.where(predicate)
156+
.where(exp)
154157
.offset(pageable.getOffset())
155158
.limit(pageable.getPageSize())
156159
.orderBy(toOrderSpecifiers(pageable.getSort()))
@@ -161,7 +164,7 @@ public Page<RecruitBoardDetail> findAllNearby(RecruitBoardNearByCondition condit
161164
.from(recruitBoard)
162165
.join(location).on(recruitBoard.locationId.eq(location.id))
163166
.join(center).on(recruitBoard.centerId.eq(center.id))
164-
.where(predicate);
167+
.where(exp);
165168

166169
return PageableExecutionUtils.getPage(content, pageable, countQuery::fetchOne);
167170
}
@@ -210,20 +213,20 @@ public Page<RecruitBoardDetail> findAllNearby(RecruitBoardNearByCondition condit
210213
@Override
211214
public Page<RecruitBoard> findAllByCenterId(UUID centerId,
212215
RecruitBoardSearchCondition condition) {
213-
QRecruitBoard recruitBoard = QRecruitBoard.recruitBoard;
214216

215-
Pageable pageable = condition.pageable();
216-
BooleanExpression predicate = isNotDeleted()
217-
.and(centerIdEq(centerId))
217+
BooleanExpression exp = centerIdEq(centerId)
218218
.and(keywordEq(condition.keyword()))
219219
.and(volunteerCategoryEq(condition.category()))
220220
.and(regionEq(condition.region()))
221221
.and(admittedEq(condition.admitted()))
222-
.and(statusEq(condition.status()));
222+
.and(statusEq(condition.status()))
223+
.and(isNotDeleted());
224+
225+
Pageable pageable = condition.pageable();
223226

224227
List<RecruitBoard> content = queryFactory
225228
.selectFrom(recruitBoard)
226-
.where(predicate)
229+
.where(exp)
227230
.offset(pageable.getOffset())
228231
.limit(pageable.getPageSize())
229232
.orderBy(toOrderSpecifiers(pageable.getSort()))
@@ -232,7 +235,7 @@ public Page<RecruitBoard> findAllByCenterId(UUID centerId,
232235
JPAQuery<Long> countQuery = queryFactory
233236
.select(recruitBoard.count())
234237
.from(recruitBoard)
235-
.where(predicate);
238+
.where(exp);
236239

237240
return PageableExecutionUtils.getPage(content, pageable, countQuery::fetchOne);
238241
}
@@ -306,7 +309,6 @@ private BooleanExpression isNotDeleted() {
306309
}
307310

308311
private BooleanExpression isNotCompleted() {
309-
QRecruitBoard recruitBoard = QRecruitBoard.recruitBoard;
310312
return recruitBoard.recruitStatus.in(RecruitStatus.RECRUITING, RecruitStatus.CLOSED);
311313
}
312314

@@ -352,41 +354,35 @@ private BooleanExpression locationBetween(RecruitBoardNearByCondition condition)
352354
}
353355

354356
private OrderSpecifier<?>[] toOrderSpecifiers(Sort sort) {
355-
QRecruitBoard recruitBoard = QRecruitBoard.recruitBoard;
356-
357357
return sort.stream()
358358
.map(order -> {
359359
String property = order.getProperty();
360-
361360
if ("created_at".equals(property)) {
362361
return order.isAscending()
363362
? recruitBoard.createdAt.asc()
364363
: recruitBoard.createdAt.desc();
365-
} else if ("volunteer_start_date_time".equals(property)) {
364+
}
365+
if ("volunteer_start_date_time".equals(property)) {
366366
return order.isAscending()
367367
? recruitBoard.recruitmentInfo.volunteerStartDateTime.asc()
368368
: recruitBoard.recruitmentInfo.volunteerStartDateTime.desc();
369-
} else {
370-
throw new IllegalStateException("Invalid sort property: " + property);
371369
}
370+
throw new IllegalStateException("Invalid sort property: " + property);
372371
})
373372
.toArray(OrderSpecifier[]::new);
374373
}
375374

376-
private static ConstructorExpression<RecruitBoardWithCenter> getRecruitBoardWithCenterConstructorExpression(
377-
QRecruitBoard recruitBoard, QCenter center) {
375+
private static ConstructorExpression<RecruitBoardWithCenter> getRecruitBoardWithCenterConstructorExpression() {
378376
return Projections.constructor(RecruitBoardWithCenter.class,
379377
recruitBoard, center.name);
380378
}
381379

382-
private static ConstructorExpression<RecruitBoardWithLocation> getRecruitBoardWithLocationConstructorExpression(
383-
QRecruitBoard recruitBoard, QLocation location) {
380+
private static ConstructorExpression<RecruitBoardWithLocation> getRecruitBoardWithLocationConstructorExpression() {
384381
return Projections.constructor(RecruitBoardWithLocation.class,
385382
recruitBoard, location.address, location.latitude, location.longitude);
386383
}
387384

388-
private static ConstructorExpression<RecruitBoardDetail> getRecruitBoardDetailConstructorExpression(
389-
QRecruitBoard recruitBoard, QLocation location, QCenter center) {
385+
private static ConstructorExpression<RecruitBoardDetail> getRecruitBoardDetailConstructorExpression() {
390386
return Projections.constructor(RecruitBoardDetail.class,
391387
recruitBoard, location.address, location.latitude, location.longitude, center.name);
392388
}

0 commit comments

Comments
 (0)