1
1
package com .sweetypie .sweetypie .repository .dynamic ;
2
2
3
+ import com .querydsl .core .BooleanBuilder ;
4
+ import com .querydsl .core .QueryResults ;
5
+ import com .querydsl .jpa .JPAExpressions ;
6
+ import com .querydsl .jpa .impl .JPAQueryFactory ;
3
7
import com .sweetypie .sweetypie .dto .AccommodationDto ;
4
8
import com .sweetypie .sweetypie .dto .QAccommodationDto ;
5
9
import com .sweetypie .sweetypie .dto .QSearchAccommodationDto ;
6
10
import com .sweetypie .sweetypie .dto .SearchAccommodationDto ;
7
11
import com .sweetypie .sweetypie .model .QAccommodation ;
8
12
import com .sweetypie .sweetypie .model .QBookedDate ;
9
13
import com .sweetypie .sweetypie .model .QBookmark ;
10
- import com .querydsl .core .BooleanBuilder ;
11
- import com .querydsl .core .QueryResults ;
12
- import com .querydsl .jpa .JPAExpressions ;
13
- import com .querydsl .jpa .impl .JPAQueryFactory ;
14
14
import lombok .RequiredArgsConstructor ;
15
15
import org .springframework .data .domain .Page ;
16
16
import org .springframework .data .domain .PageImpl ;
17
17
import org .springframework .data .domain .Pageable ;
18
18
import org .springframework .stereotype .Repository ;
19
19
20
20
import java .time .LocalDate ;
21
+ import java .util .List ;
21
22
22
23
@ Repository
23
24
@ RequiredArgsConstructor
@@ -30,68 +31,20 @@ public class DynamicAccommodationRepository {
30
31
QBookmark bookmark = QBookmark .bookmark ;
31
32
32
33
public AccommodationDto findById (Long memberId , Long accommodationId ) {
33
- AccommodationDto result ;
34
-
35
- if (memberId != null ) {
36
- result = queryFactory
37
- .select (new QAccommodationDto (ac .id , ac .city , ac .gu , ac .address , ac .title , ac .bathroomNum , ac .bedroomNum ,
38
- ac .bedNum , ac .capacity , ac .price , ac .contact , ac .latitude , ac .longitude , ac .locationDesc , ac .transportationDesc ,
39
- ac .accommodationDesc , ac .rating , ac .reviewNum , ac .accommodationType , ac .buildingType , ac .hostName , ac .hostDesc ,
40
- ac .hostReviewNum , bookmark ))
41
- .from (ac )
42
- .where (ac .id .eq (accommodationId ))
43
- .leftJoin (bookmark )
44
- .on (bookmark .accommodation .id .eq (ac .id ).and (bookmark .member .id .eq (memberId )))
45
- .fetchOne ();
46
- } else {
47
- result = queryFactory
48
- .select (new QAccommodationDto (ac .id , ac .city , ac .gu , ac .address , ac .title , ac .bathroomNum , ac .bedroomNum ,
49
- ac .bedNum , ac .capacity , ac .price , ac .contact , ac .latitude , ac .longitude , ac .locationDesc , ac .transportationDesc ,
50
- ac .accommodationDesc , ac .rating , ac .reviewNum , ac .accommodationType , ac .buildingType , ac .hostName , ac .hostDesc ,
51
- ac .hostReviewNum ))
52
- .from (ac )
53
- .where (ac .id .eq (accommodationId ))
54
- .fetchOne ();
55
- }
56
34
57
- return result ;
35
+ return getQueryResult ( memberId , accommodationId ) ;
58
36
}
59
37
60
38
public Page <SearchAccommodationDto > findAccommodationsBySearch (String searchKeyword , LocalDate checkIn , LocalDate checkout ,
61
39
int guestNum , Long memberId , String types , Pageable page ) {
62
40
BooleanBuilder bdBuilder = new BooleanBuilder ();
63
41
BooleanBuilder acBuilder = new BooleanBuilder ();
64
42
65
- if (searchKeyword != null && !searchKeyword .equals ("" )) {
66
- String match = "[^\uAC00 -\uD7A3 xfe0-9a-zA-Z\\ s]" ;
67
-
68
- searchKeyword = searchKeyword .replace ("특별시" , "" )
69
- .replace ("광역시" , "" )
70
- .replaceAll (match , "" );
71
-
72
- for (String keyword : searchKeyword .split (" " )) {
73
- if (keyword .charAt (keyword .length () - 1 ) == '시' ) {
74
- keyword = keyword .substring (0 , keyword .length () - 1 );
75
- }
76
- acBuilder .and (ac .city .startsWith (keyword ).or (ac .gu .startsWith (keyword )));
77
- }
78
- }
79
-
80
- if (types != null ) {
81
- BooleanBuilder typeBuilder = new BooleanBuilder ();
82
-
83
- for (String type : types .split (" " )) {
84
- typeBuilder .or (ac .accommodationType .eq (type ));
85
- }
86
- acBuilder .and (typeBuilder );
87
- }
88
-
89
43
acBuilder .and (ac .capacity .goe (guestNum ));
90
- bdBuilder .and (bd .date .goe (checkIn ));
91
44
92
- if ( checkout != null ) {
93
- bdBuilder . and ( bd . date . before ( checkout ) );
94
- }
45
+ setSearchKeywordQuery ( searchKeyword , acBuilder );
46
+ setAccommodationTypesQuery ( types , acBuilder );
47
+ setCheckInCheckOutQuery ( checkIn , checkout , bdBuilder );
95
48
96
49
acBuilder .andNot (ac .id .in (JPAExpressions
97
50
.select (bd .accommodation .id )
@@ -111,29 +64,16 @@ public Page<SearchAccommodationDto> findAccommodationsByMapSearch(float minLatit
111
64
BooleanBuilder acBuilder = new BooleanBuilder ();
112
65
113
66
acBuilder .and (ac .capacity .goe (guestNum ));
114
- bdBuilder .and (bd .date .goe (checkIn ));
115
-
116
- if (types != null ) {
117
- BooleanBuilder typeBuilder = new BooleanBuilder ();
118
67
119
- for (String type : types .split (" " )) {
120
- typeBuilder .or (ac .accommodationType .eq (type ));
121
- }
122
- acBuilder .and (typeBuilder );
123
- }
124
-
125
- if (checkout != null ) {
126
- bdBuilder .and (bd .date .before (checkout ));
127
- }
68
+ setAccommodationTypesQuery (types , acBuilder );
69
+ setCheckInCheckOutQuery (checkIn , checkout , bdBuilder );
70
+ setCoordinate (minLatitude , maxLatitude , minLongitude , maxLongitude , acBuilder );
128
71
129
72
acBuilder .andNot (ac .id .in (JPAExpressions
130
73
.select (bd .accommodation .id )
131
74
.from (bd )
132
75
.where (bdBuilder )));
133
76
134
- acBuilder .and (ac .latitude .between (minLatitude , maxLatitude ));
135
- acBuilder .and (ac .longitude .between (minLongitude , maxLongitude ));
136
-
137
77
QueryResults <SearchAccommodationDto > results = getQueryResults (acBuilder , memberId , page );
138
78
139
79
return new PageImpl <>(results .getResults (), page , results .getTotal ());
@@ -166,6 +106,32 @@ public Page<SearchAccommodationDto> findByCity(String city, Long memberId, Pagea
166
106
return new PageImpl <>(results .getResults (), page , results .getTotal ());
167
107
}
168
108
109
+ public List <Integer > findPricesBySearch (String searchKeyword , Float minLatitude , Float maxLatitude , Float minLongitude , Float maxLongitude ,
110
+ LocalDate checkIn , LocalDate checkout , int guestNum , String types ) {
111
+
112
+ BooleanBuilder acBuilder = new BooleanBuilder ();
113
+ BooleanBuilder bdBuilder = new BooleanBuilder ();
114
+
115
+ setSearchKeywordQuery (searchKeyword , acBuilder );
116
+ setAccommodationTypesQuery (types , acBuilder );
117
+ setCheckInCheckOutQuery (checkIn , checkout , bdBuilder );
118
+ setCoordinate (minLatitude , maxLatitude , minLongitude , maxLongitude , acBuilder );
119
+
120
+ acBuilder .and (ac .capacity .goe (guestNum ));
121
+
122
+ acBuilder .andNot (ac .id .in (JPAExpressions
123
+ .select (bd .accommodation .id )
124
+ .from (bd )
125
+ .where (bdBuilder )));
126
+
127
+ return queryFactory
128
+ .select (ac .price )
129
+ .from (ac )
130
+ .where (acBuilder )
131
+ .orderBy (ac .price .asc ())
132
+ .fetch ();
133
+ }
134
+
169
135
private QueryResults <SearchAccommodationDto > getQueryResults (BooleanBuilder builder , Long memberId , Pageable page ) {
170
136
QueryResults <SearchAccommodationDto > results ;
171
137
@@ -197,4 +163,77 @@ private QueryResults<SearchAccommodationDto> getQueryResults(BooleanBuilder buil
197
163
198
164
return results ;
199
165
}
166
+
167
+ private AccommodationDto getQueryResult (Long memberId , Long accommodationId ) {
168
+ AccommodationDto result ;
169
+
170
+ if (memberId != null ) {
171
+ result = queryFactory
172
+ .select (new QAccommodationDto (ac .id , ac .city , ac .gu , ac .address , ac .title , ac .bathroomNum , ac .bedroomNum ,
173
+ ac .bedNum , ac .capacity , ac .price , ac .contact , ac .latitude , ac .longitude , ac .locationDesc , ac .transportationDesc ,
174
+ ac .accommodationDesc , ac .rating , ac .reviewNum , ac .accommodationType , ac .buildingType , ac .hostName , ac .hostDesc ,
175
+ ac .hostReviewNum , bookmark ))
176
+ .from (ac )
177
+ .where (ac .id .eq (accommodationId ))
178
+ .leftJoin (bookmark )
179
+ .on (bookmark .accommodation .id .eq (ac .id ).and (bookmark .member .id .eq (memberId )))
180
+ .fetchOne ();
181
+ } else {
182
+ result = queryFactory
183
+ .select (new QAccommodationDto (ac .id , ac .city , ac .gu , ac .address , ac .title , ac .bathroomNum , ac .bedroomNum ,
184
+ ac .bedNum , ac .capacity , ac .price , ac .contact , ac .latitude , ac .longitude , ac .locationDesc , ac .transportationDesc ,
185
+ ac .accommodationDesc , ac .rating , ac .reviewNum , ac .accommodationType , ac .buildingType , ac .hostName , ac .hostDesc ,
186
+ ac .hostReviewNum ))
187
+ .from (ac )
188
+ .where (ac .id .eq (accommodationId ))
189
+ .fetchOne ();
190
+ }
191
+
192
+ return result ;
193
+ }
194
+
195
+ private void setSearchKeywordQuery (String searchKeyword , BooleanBuilder builder ) {
196
+ if (searchKeyword != null && !searchKeyword .equals ("" )) {
197
+ String match = "[^\uAC00 -\uD7A3 xfe0-9a-zA-Z\\ s]" ;
198
+
199
+ searchKeyword = searchKeyword .replace ("특별시" , "" )
200
+ .replace ("광역시" , "" )
201
+ .replaceAll (match , "" );
202
+
203
+ for (String keyword : searchKeyword .split (" " )) {
204
+ if (keyword .charAt (keyword .length () - 1 ) == '시' ) {
205
+ keyword = keyword .substring (0 , keyword .length () - 1 );
206
+ }
207
+ builder .and (ac .city .startsWith (keyword ).or (ac .gu .startsWith (keyword )));
208
+ }
209
+ }
210
+ }
211
+
212
+ private void setCheckInCheckOutQuery (LocalDate checkIn , LocalDate checkout , BooleanBuilder builder ) {
213
+ builder .and (bd .date .goe (checkIn ));
214
+
215
+ if (checkout != null ) {
216
+ builder .and (bd .date .before (checkout ));
217
+ }
218
+ }
219
+
220
+ private void setAccommodationTypesQuery (String types , BooleanBuilder builder ) {
221
+ if (types != null ) {
222
+ BooleanBuilder typeBuilder = new BooleanBuilder ();
223
+
224
+ for (String type : types .split (" " )) {
225
+ typeBuilder .or (ac .accommodationType .eq (type ));
226
+ }
227
+ builder .and (typeBuilder );
228
+ }
229
+ }
230
+
231
+
232
+ private void setCoordinate (Float minLatitude , Float maxLatitude , Float minLongitude , Float maxLongitude , BooleanBuilder builder ) {
233
+
234
+ if (minLatitude != null && maxLatitude != null && minLongitude != null && maxLongitude != null ) {
235
+ builder .and (ac .latitude .between (minLatitude , maxLatitude ));
236
+ builder .and (ac .longitude .between (minLongitude , maxLongitude ));
237
+ }
238
+ }
200
239
}
0 commit comments