Skip to content

Commit

Permalink
feat: Add CustomizedRoomRepository (#52)
Browse files Browse the repository at this point in the history
- 필터링 된 방을 조회하기 위한 findFilteredRooms 메서드 구현
  • Loading branch information
janeljs committed May 29, 2021
1 parent ec545ba commit f97ac0a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
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);
}
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;
}
}

0 comments on commit f97ac0a

Please sign in to comment.