diff --git a/src/main/java/com/somemore/recruitboard/repository/RecruitBoardRepositoryImpl.java b/src/main/java/com/somemore/recruitboard/repository/RecruitBoardRepositoryImpl.java index 1408bdf9b..b52c49a7c 100644 --- a/src/main/java/com/somemore/recruitboard/repository/RecruitBoardRepositoryImpl.java +++ b/src/main/java/com/somemore/recruitboard/repository/RecruitBoardRepositoryImpl.java @@ -1,8 +1,5 @@ package com.somemore.recruitboard.repository; -import static com.somemore.location.domain.QLocation.location; -import static com.somemore.recruitboard.domain.QRecruitBoard.recruitBoard; - import com.querydsl.core.types.ConstructorExpression; import com.querydsl.core.types.OrderSpecifier; import com.querydsl.core.types.Projections; @@ -12,14 +9,15 @@ import com.somemore.center.domain.QCenter; import com.somemore.location.domain.QLocation; import com.somemore.location.utils.GeoUtils; -import com.somemore.recruitboard.domain.*; +import com.somemore.recruitboard.domain.QRecruitBoard; +import com.somemore.recruitboard.domain.RecruitBoard; +import com.somemore.recruitboard.domain.RecruitStatus; +import com.somemore.recruitboard.domain.VolunteerCategory; import com.somemore.recruitboard.dto.condition.RecruitBoardNearByCondition; import com.somemore.recruitboard.dto.condition.RecruitBoardSearchCondition; import com.somemore.recruitboard.repository.mapper.RecruitBoardDetail; import com.somemore.recruitboard.repository.mapper.RecruitBoardWithCenter; import com.somemore.recruitboard.repository.mapper.RecruitBoardWithLocation; - -import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.UUID; @@ -36,9 +34,13 @@ public class RecruitBoardRepositoryImpl implements RecruitBoardRepository { private final RecruitBoardJpaRepository recruitBoardJpaRepository; -// private final RecruitBoardDocumentRepository documentRepository; + // private final RecruitBoardDocumentRepository documentRepository; private final JPAQueryFactory queryFactory; + private static final QRecruitBoard recruitBoard = QRecruitBoard.recruitBoard; + private static final QLocation location = QLocation.location; + private static final QCenter center = QCenter.center; + @Override public RecruitBoard save(RecruitBoard recruitBoard) { return recruitBoardJpaRepository.save(recruitBoard); @@ -51,11 +53,13 @@ public List saveAll(List recruitBoards) { @Override public Optional findById(Long id) { - QRecruitBoard recruitBoard = QRecruitBoard.recruitBoard; + + BooleanExpression exp = idEq(id) + .and(isNotDeleted()); RecruitBoard result = queryFactory .selectFrom(recruitBoard) - .where(isNotDeleted().and(idEq(id))) + .where(exp) .fetchOne(); return Optional.ofNullable(result); @@ -63,16 +67,15 @@ public Optional findById(Long id) { @Override public List findNotCompletedIdsByCenterId(UUID centerId) { - QRecruitBoard recruitBoard = QRecruitBoard.recruitBoard; + + BooleanExpression exp = centerIdEq(centerId) + .and(isNotCompleted()) + .and(isNotDeleted()); return queryFactory .select(recruitBoard.id) .from(recruitBoard) - .where( - recruitBoard.centerId.eq(centerId) - .and(isNotCompleted()) - .and(isNotDeleted()) - ) + .where(exp) .fetch(); } @@ -89,35 +92,37 @@ public List findAllByIds(List ids) { @Override public Optional findWithLocationById(Long id) { - QRecruitBoard recruitBoard = QRecruitBoard.recruitBoard; - QLocation location = QLocation.location; - - return Optional.ofNullable( - queryFactory.select( - getRecruitBoardWithLocationConstructorExpression(recruitBoard, location)) - .from(recruitBoard) - .join(location).on(recruitBoard.locationId.eq(location.id)) - .where(isNotDeleted().and(idEq(id))) - .fetchOne()); + + BooleanExpression exp = idEq(id) + .and(isNotDeleted()); + + RecruitBoardWithLocation result = queryFactory + .select(getRecruitBoardWithLocationConstructorExpression()) + .from(recruitBoard) + .join(location) + .on(recruitBoard.locationId.eq(location.id)) + .where(exp) + .fetchOne(); + + return Optional.ofNullable(result); } @Override public Page findAllWithCenter(RecruitBoardSearchCondition condition) { - QRecruitBoard recruitBoard = QRecruitBoard.recruitBoard; - QCenter center = QCenter.center; - Pageable pageable = condition.pageable(); - BooleanExpression predicate = isNotDeleted() + BooleanExpression exp = isNotDeleted() .and(keywordEq(condition.keyword())) .and(volunteerCategoryEq(condition.category())) .and(regionEq(condition.region())) .and(admittedEq(condition.admitted())) .and(statusEq(condition.status())); + Pageable pageable = condition.pageable(); + List content = queryFactory - .select(getRecruitBoardWithCenterConstructorExpression(recruitBoard, center)) + .select(getRecruitBoardWithCenterConstructorExpression()) .from(recruitBoard) - .where(predicate) + .where(exp) .join(center).on(recruitBoard.centerId.eq(center.id)) .offset(pageable.getOffset()) .limit(pageable.getPageSize()) @@ -127,30 +132,28 @@ public Page findAllWithCenter(RecruitBoardSearchConditio JPAQuery countQuery = queryFactory .select(recruitBoard.count()) .from(recruitBoard) - .where(predicate); + .where(exp) + .join(center).on(recruitBoard.centerId.eq(center.id)); return PageableExecutionUtils.getPage(content, pageable, countQuery::fetchOne); } @Override public Page findAllNearby(RecruitBoardNearByCondition condition) { - QRecruitBoard recruitBoard = QRecruitBoard.recruitBoard; - QLocation location = QLocation.location; - QCenter center = QCenter.center; - - Pageable pageable = condition.pageable(); - BooleanExpression predicate = locationBetween(condition) + BooleanExpression exp = locationBetween(condition) .and(keywordEq(condition.keyword())) .and(statusEq(condition.status())) .and(isNotDeleted()); + Pageable pageable = condition.pageable(); + List content = queryFactory - .select(getRecruitBoardDetailConstructorExpression(recruitBoard, location, center)) + .select(getRecruitBoardDetailConstructorExpression()) .from(recruitBoard) .join(location).on(recruitBoard.locationId.eq(location.id)) .join(center).on(recruitBoard.centerId.eq(center.id)) - .where(predicate) + .where(exp) .offset(pageable.getOffset()) .limit(pageable.getPageSize()) .orderBy(toOrderSpecifiers(pageable.getSort())) @@ -161,7 +164,7 @@ public Page findAllNearby(RecruitBoardNearByCondition condit .from(recruitBoard) .join(location).on(recruitBoard.locationId.eq(location.id)) .join(center).on(recruitBoard.centerId.eq(center.id)) - .where(predicate); + .where(exp); return PageableExecutionUtils.getPage(content, pageable, countQuery::fetchOne); } @@ -210,20 +213,20 @@ public Page findAllNearby(RecruitBoardNearByCondition condit @Override public Page findAllByCenterId(UUID centerId, RecruitBoardSearchCondition condition) { - QRecruitBoard recruitBoard = QRecruitBoard.recruitBoard; - Pageable pageable = condition.pageable(); - BooleanExpression predicate = isNotDeleted() - .and(centerIdEq(centerId)) + BooleanExpression exp = centerIdEq(centerId) .and(keywordEq(condition.keyword())) .and(volunteerCategoryEq(condition.category())) .and(regionEq(condition.region())) .and(admittedEq(condition.admitted())) - .and(statusEq(condition.status())); + .and(statusEq(condition.status())) + .and(isNotDeleted()); + + Pageable pageable = condition.pageable(); List content = queryFactory .selectFrom(recruitBoard) - .where(predicate) + .where(exp) .offset(pageable.getOffset()) .limit(pageable.getPageSize()) .orderBy(toOrderSpecifiers(pageable.getSort())) @@ -232,7 +235,7 @@ public Page findAllByCenterId(UUID centerId, JPAQuery countQuery = queryFactory .select(recruitBoard.count()) .from(recruitBoard) - .where(predicate); + .where(exp); return PageableExecutionUtils.getPage(content, pageable, countQuery::fetchOne); } @@ -306,7 +309,6 @@ private BooleanExpression isNotDeleted() { } private BooleanExpression isNotCompleted() { - QRecruitBoard recruitBoard = QRecruitBoard.recruitBoard; return recruitBoard.recruitStatus.in(RecruitStatus.RECRUITING, RecruitStatus.CLOSED); } @@ -352,41 +354,35 @@ private BooleanExpression locationBetween(RecruitBoardNearByCondition condition) } private OrderSpecifier[] toOrderSpecifiers(Sort sort) { - QRecruitBoard recruitBoard = QRecruitBoard.recruitBoard; - return sort.stream() .map(order -> { String property = order.getProperty(); - if ("created_at".equals(property)) { return order.isAscending() ? recruitBoard.createdAt.asc() : recruitBoard.createdAt.desc(); - } else if ("volunteer_start_date_time".equals(property)) { + } + if ("volunteer_start_date_time".equals(property)) { return order.isAscending() ? recruitBoard.recruitmentInfo.volunteerStartDateTime.asc() : recruitBoard.recruitmentInfo.volunteerStartDateTime.desc(); - } else { - throw new IllegalStateException("Invalid sort property: " + property); } + throw new IllegalStateException("Invalid sort property: " + property); }) .toArray(OrderSpecifier[]::new); } - private static ConstructorExpression getRecruitBoardWithCenterConstructorExpression( - QRecruitBoard recruitBoard, QCenter center) { + private static ConstructorExpression getRecruitBoardWithCenterConstructorExpression() { return Projections.constructor(RecruitBoardWithCenter.class, recruitBoard, center.name); } - private static ConstructorExpression getRecruitBoardWithLocationConstructorExpression( - QRecruitBoard recruitBoard, QLocation location) { + private static ConstructorExpression getRecruitBoardWithLocationConstructorExpression() { return Projections.constructor(RecruitBoardWithLocation.class, recruitBoard, location.address, location.latitude, location.longitude); } - private static ConstructorExpression getRecruitBoardDetailConstructorExpression( - QRecruitBoard recruitBoard, QLocation location, QCenter center) { + private static ConstructorExpression getRecruitBoardDetailConstructorExpression() { return Projections.constructor(RecruitBoardDetail.class, recruitBoard, location.address, location.latitude, location.longitude, center.name); }