-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add CustomizedRoomRepository (#52)
- 필터링 된 방을 조회하기 위한 findFilteredRooms 메서드 구현
- Loading branch information
Showing
2 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
BE/airbnb/src/main/java/airbnb/repository/CustomizedRoomRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package airbnb.repository; | ||
|
||
import airbnb.domain.Room; | ||
import airbnb.request.SearchRequest; | ||
import airbnb.response.RoomResponse; | ||
|
||
import java.util.List; | ||
|
||
public interface CustomizedRoomRepository { | ||
List<Room> findFilteredRooms(SearchRequest searchRequest); | ||
} |
56 changes: 56 additions & 0 deletions
56
BE/airbnb/src/main/java/airbnb/repository/CustomizedRoomRepositoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package airbnb.repository; | ||
|
||
import airbnb.domain.Room; | ||
import airbnb.request.SearchRequest; | ||
import com.querydsl.core.types.dsl.BooleanExpression; | ||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
import java.time.LocalDate; | ||
import java.util.List; | ||
|
||
import static airbnb.domain.QBooking.booking; | ||
import static airbnb.domain.QRoom.room; | ||
import static org.springframework.util.StringUtils.hasText; | ||
|
||
@RequiredArgsConstructor | ||
public class CustomizedRoomRepositoryImpl implements CustomizedRoomRepository { | ||
|
||
private final JPAQueryFactory queryFactory; | ||
|
||
@Override | ||
public List<Room> findFilteredRooms(SearchRequest searchRequest) { | ||
return queryFactory | ||
.selectFrom(room) | ||
.leftJoin(booking).on(room.id.eq(booking.id)) | ||
.where(placeIdEquals(searchRequest.getPlaceId()), | ||
dateNotBetween(searchRequest.getCheckIn(), searchRequest.getCheckOut()), | ||
priceBetween(searchRequest.getPriceMin(), searchRequest.getPriceMax()), | ||
maximumNumberOfGuestsGoeSumOf(searchRequest.getAdults(), searchRequest.getChildren(), searchRequest.getInfants())) | ||
.fetch(); | ||
} | ||
|
||
private BooleanExpression placeIdEquals(String placeId) { | ||
return hasText(placeId) ? room.location.placeId.eq(placeId) : null; | ||
} | ||
|
||
private BooleanExpression dateNotBetween(LocalDate checkIn, LocalDate checkOut) { | ||
return ((checkIn != null) && (checkOut != null)) ? | ||
booking.checkOut.loe(checkIn).or(booking.checkIn.goe(checkOut)) : null; | ||
} | ||
|
||
private BooleanExpression priceBetween(Integer priceMin, Integer priceMax) { | ||
return ((priceMin != null) && (priceMax != null)) ? | ||
room.pricePerNight.goe(priceMin).and(room.pricePerNight.loe(priceMax)) : null; | ||
} | ||
|
||
private BooleanExpression maximumNumberOfGuestsGoeSumOf(Integer adults, Integer children, Integer infants) { | ||
if (children == null) { | ||
children = 0; | ||
} | ||
if (infants == null) { | ||
infants = 0; | ||
} | ||
return adults != null ? room.maximumNumberOfGuests.goe(adults + children + infants) : null; | ||
} | ||
} |