11package com .somemore .recruitboard .repository ;
22
3- import static com .somemore .location .domain .QLocation .location ;
4- import static com .somemore .recruitboard .domain .QRecruitBoard .recruitBoard ;
5-
63import com .querydsl .core .types .ConstructorExpression ;
74import com .querydsl .core .types .OrderSpecifier ;
85import com .querydsl .core .types .Projections ;
129import com .somemore .center .domain .QCenter ;
1310import com .somemore .location .domain .QLocation ;
1411import 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 ;
1616import com .somemore .recruitboard .dto .condition .RecruitBoardNearByCondition ;
1717import com .somemore .recruitboard .dto .condition .RecruitBoardSearchCondition ;
1818import com .somemore .recruitboard .repository .mapper .RecruitBoardDetail ;
1919import com .somemore .recruitboard .repository .mapper .RecruitBoardWithCenter ;
2020import com .somemore .recruitboard .repository .mapper .RecruitBoardWithLocation ;
21-
22- import java .util .ArrayList ;
2321import java .util .List ;
2422import java .util .Optional ;
2523import java .util .UUID ;
3634public 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 final static QRecruitBoard recruitBoard = QRecruitBoard .recruitBoard ;
41+ private final static QLocation location = QLocation .location ;
42+ private final static 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+ BooleanExpression exp = isNotDeleted ()
217218 .and (centerIdEq (centerId ))
218219 .and (keywordEq (condition .keyword ()))
219220 .and (volunteerCategoryEq (condition .category ()))
220221 .and (regionEq (condition .region ()))
221222 .and (admittedEq (condition .admitted ()))
222223 .and (statusEq (condition .status ()));
223224
225+ Pageable pageable = condition .pageable ();
226+
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 }
@@ -352,41 +355,35 @@ private BooleanExpression locationBetween(RecruitBoardNearByCondition condition)
352355 }
353356
354357 private OrderSpecifier <?>[] toOrderSpecifiers (Sort sort ) {
355- QRecruitBoard recruitBoard = QRecruitBoard .recruitBoard ;
356-
357358 return sort .stream ()
358359 .map (order -> {
359360 String property = order .getProperty ();
360-
361361 if ("created_at" .equals (property )) {
362362 return order .isAscending ()
363363 ? recruitBoard .createdAt .asc ()
364364 : recruitBoard .createdAt .desc ();
365- } else if ("volunteer_start_date_time" .equals (property )) {
365+ }
366+ if ("volunteer_start_date_time" .equals (property )) {
366367 return order .isAscending ()
367368 ? recruitBoard .recruitmentInfo .volunteerStartDateTime .asc ()
368369 : recruitBoard .recruitmentInfo .volunteerStartDateTime .desc ();
369- } else {
370- throw new IllegalStateException ("Invalid sort property: " + property );
371370 }
371+ throw new IllegalStateException ("Invalid sort property: " + property );
372372 })
373373 .toArray (OrderSpecifier []::new );
374374 }
375375
376- private static ConstructorExpression <RecruitBoardWithCenter > getRecruitBoardWithCenterConstructorExpression (
377- QRecruitBoard recruitBoard , QCenter center ) {
376+ private static ConstructorExpression <RecruitBoardWithCenter > getRecruitBoardWithCenterConstructorExpression () {
378377 return Projections .constructor (RecruitBoardWithCenter .class ,
379378 recruitBoard , center .name );
380379 }
381380
382- private static ConstructorExpression <RecruitBoardWithLocation > getRecruitBoardWithLocationConstructorExpression (
383- QRecruitBoard recruitBoard , QLocation location ) {
381+ private static ConstructorExpression <RecruitBoardWithLocation > getRecruitBoardWithLocationConstructorExpression () {
384382 return Projections .constructor (RecruitBoardWithLocation .class ,
385383 recruitBoard , location .address , location .latitude , location .longitude );
386384 }
387385
388- private static ConstructorExpression <RecruitBoardDetail > getRecruitBoardDetailConstructorExpression (
389- QRecruitBoard recruitBoard , QLocation location , QCenter center ) {
386+ private static ConstructorExpression <RecruitBoardDetail > getRecruitBoardDetailConstructorExpression () {
390387 return Projections .constructor (RecruitBoardDetail .class ,
391388 recruitBoard , location .address , location .latitude , location .longitude , center .name );
392389 }
0 commit comments