11package com .sweetypie .sweetypie .repository .dynamic ;
22
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 ;
37import com .sweetypie .sweetypie .dto .AccommodationDto ;
48import com .sweetypie .sweetypie .dto .QAccommodationDto ;
59import com .sweetypie .sweetypie .dto .QSearchAccommodationDto ;
610import com .sweetypie .sweetypie .dto .SearchAccommodationDto ;
711import com .sweetypie .sweetypie .model .QAccommodation ;
812import com .sweetypie .sweetypie .model .QBookedDate ;
913import 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 ;
1414import lombok .RequiredArgsConstructor ;
1515import org .springframework .data .domain .Page ;
1616import org .springframework .data .domain .PageImpl ;
1717import org .springframework .data .domain .Pageable ;
1818import org .springframework .stereotype .Repository ;
1919
2020import java .time .LocalDate ;
21+ import java .util .List ;
2122
2223@ Repository
2324@ RequiredArgsConstructor
@@ -30,68 +31,20 @@ public class DynamicAccommodationRepository {
3031 QBookmark bookmark = QBookmark .bookmark ;
3132
3233 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- }
5634
57- return result ;
35+ return getQueryResult ( memberId , accommodationId ) ;
5836 }
5937
6038 public Page <SearchAccommodationDto > findAccommodationsBySearch (String searchKeyword , LocalDate checkIn , LocalDate checkout ,
6139 int guestNum , Long memberId , String types , Pageable page ) {
6240 BooleanBuilder bdBuilder = new BooleanBuilder ();
6341 BooleanBuilder acBuilder = new BooleanBuilder ();
6442
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-
8943 acBuilder .and (ac .capacity .goe (guestNum ));
90- bdBuilder .and (bd .date .goe (checkIn ));
9144
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 );
9548
9649 acBuilder .andNot (ac .id .in (JPAExpressions
9750 .select (bd .accommodation .id )
@@ -111,29 +64,16 @@ public Page<SearchAccommodationDto> findAccommodationsByMapSearch(float minLatit
11164 BooleanBuilder acBuilder = new BooleanBuilder ();
11265
11366 acBuilder .and (ac .capacity .goe (guestNum ));
114- bdBuilder .and (bd .date .goe (checkIn ));
115-
116- if (types != null ) {
117- BooleanBuilder typeBuilder = new BooleanBuilder ();
11867
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 );
12871
12972 acBuilder .andNot (ac .id .in (JPAExpressions
13073 .select (bd .accommodation .id )
13174 .from (bd )
13275 .where (bdBuilder )));
13376
134- acBuilder .and (ac .latitude .between (minLatitude , maxLatitude ));
135- acBuilder .and (ac .longitude .between (minLongitude , maxLongitude ));
136-
13777 QueryResults <SearchAccommodationDto > results = getQueryResults (acBuilder , memberId , page );
13878
13979 return new PageImpl <>(results .getResults (), page , results .getTotal ());
@@ -166,6 +106,32 @@ public Page<SearchAccommodationDto> findByCity(String city, Long memberId, Pagea
166106 return new PageImpl <>(results .getResults (), page , results .getTotal ());
167107 }
168108
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+
169135 private QueryResults <SearchAccommodationDto > getQueryResults (BooleanBuilder builder , Long memberId , Pageable page ) {
170136 QueryResults <SearchAccommodationDto > results ;
171137
@@ -197,4 +163,77 @@ private QueryResults<SearchAccommodationDto> getQueryResults(BooleanBuilder buil
197163
198164 return results ;
199165 }
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+ }
200239}
0 commit comments