Skip to content

Commit

Permalink
♻️ [Refactor] 중복 로직 static 메서드로 추출
Browse files Browse the repository at this point in the history
  • Loading branch information
88dldl committed Aug 31, 2024
1 parent 98a5e66 commit 82bbacf
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.avab.avab.repository;

import static com.avab.avab.domain.QFlow.flow;
import static com.avab.avab.domain.QReport.report;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
Expand All @@ -20,11 +17,8 @@
import com.avab.avab.domain.enums.Gender;
import com.avab.avab.domain.enums.Keyword;
import com.avab.avab.domain.enums.Purpose;
import com.avab.avab.domain.enums.ReportType;
import com.avab.avab.domain.mapping.QFlowRecreationKeyword;
import com.avab.avab.domain.mapping.QFlowRecreationPurpose;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.JPAExpressions;
import com.querydsl.jpa.impl.JPAQueryFactory;

import lombok.RequiredArgsConstructor;
Expand All @@ -34,21 +28,6 @@
public class FlowCustomRepositoryImpl implements FlowCustomRepository {
private final JPAQueryFactory queryFactory;

private BooleanExpression notSoftDeletedFlow() {
return flow.deletedAt.isNull();
}

private BooleanExpression notReportedFlowByUser(User user) {
if (user == null) {
return null;
}

return flow.id.notIn(
JPAExpressions.select(report.targetFlow.id)
.from(report)
.where(report.reportType.eq(ReportType.FLOW), report.reporter.eq(user)));
}

@Override
public List<Flow> recommendFlows(
List<Keyword> keyword,
Expand All @@ -69,7 +48,9 @@ public List<Flow> recommendFlows(
queryFactory
.select(flow.id)
.from(flow)
.where(notSoftDeletedFlow(), notReportedFlowByUser(user))
.where(
MaskingPredicates.notSoftDeletedFlow(),
MaskingPredicates.notReportedFlowByUser(user))
.fetch();

for (Long flowId : flows) {
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/com/avab/avab/repository/MaskingPredicates.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.avab.avab.repository;

import static com.avab.avab.domain.QFlow.flow;
import static com.avab.avab.domain.QRecreation.recreation;
import static com.avab.avab.domain.QReport.report;

import com.avab.avab.apiPayload.code.status.ErrorStatus;
import com.avab.avab.apiPayload.exception.RecreationException;
import com.avab.avab.domain.User;
import com.avab.avab.domain.enums.ReportType;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.JPAExpressions;

class MaskingPredicates {

static BooleanExpression notSoftDeletedFlow() {
return flow.deletedAt.isNull();
}

static BooleanExpression notSoftDeletedRecreation() {
return recreation.deletedAt.isNull();
}

static BooleanExpression notReportedFlowByUser(User user) {
if (user == null) {
return null;
}

return flow.id.notIn(
JPAExpressions.select(report.targetFlow.id)
.from(report)
.where(report.reportType.eq(ReportType.FLOW), report.reporter.eq(user)));
}

static BooleanExpression notReportedRecreationByUser(User user) {
if (user == null) throw new RecreationException(ErrorStatus.USER_NOT_FOUND);

return recreation.id.notIn(
JPAExpressions.select(report.targetRecreation.id)
.from(report)
.where(
report.reportType.eq(ReportType.RECREATION),
report.reporter.eq(user)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static com.avab.avab.domain.QFlow.flow;
import static com.avab.avab.domain.QRecreation.recreation;
import static com.avab.avab.domain.QRecreationReview.recreationReview;
import static com.avab.avab.domain.QReport.report;

import java.util.ArrayList;
import java.util.Comparator;
Expand Down Expand Up @@ -246,9 +245,9 @@ public List<Flow> findRelatedFlows(Long recreationId, User user) {
.select(flow)
.from(flow)
.where(
notSoftDeletedFlow(),
MaskingPredicates.notSoftDeletedFlow(),
isRecreationUsedInFlow(recreationId),
notReportedFlowByUser(user))
MaskingPredicates.notReportedFlowByUser(user))
.orderBy(Expressions.numberTemplate(Double.class, "function('rand')").asc())
.limit(2)
.fetch();
Expand All @@ -258,36 +257,6 @@ private BooleanExpression isRecreationUsedInFlow(Long recreationId) {
return flow.flowRecreationList.any().recreation.id.eq(recreationId);
}

private BooleanExpression notSoftDeletedFlow() {
return flow.deletedAt.isNull();
}

private BooleanExpression notSoftDeletedRecreation() {
return recreation.deletedAt.isNull();
}

private BooleanExpression notReportedFlowByUser(User user) {
if (user == null) {
return null;
}

return flow.id.notIn(
JPAExpressions.select(report.targetFlow.id)
.from(report)
.where(report.reportType.eq(ReportType.FLOW), report.reporter.eq(user)));
}

private BooleanExpression notReportedRecreationByUser(User user) {
if (user == null) throw new RecreationException(ErrorStatus.USER_NOT_FOUND);

return recreation.id.notIn(
JPAExpressions.select(report.targetRecreation.id)
.from(report)
.where(
report.reportType.eq(ReportType.RECREATION),
report.reporter.eq(user)));
}

// 목적, 시간, 나머지는 연관 레크레이션과 같음 (키워드, 인원, 연령대, 성별)
@Override
public List<Recreation> recommendRecreations(
Expand All @@ -314,7 +283,9 @@ public List<Recreation> recommendRecreations(
queryFactory
.select(recreation.id)
.from(recreation)
.where(notReportedRecreationByUser(user), notSoftDeletedRecreation())
.where(
MaskingPredicates.notReportedRecreationByUser(user),
MaskingPredicates.notSoftDeletedRecreation())
.fetch();

for (Long recreationId : recreations) {
Expand Down Expand Up @@ -434,14 +405,18 @@ public Page<Recreation> findTop9ByOrderByWeeklyViewCountDesc(Pageable pageable,
long totalCount =
queryFactory
.selectFrom(recreation)
.where(notReportedRecreationByUser(user), notSoftDeletedRecreation())
.where(
MaskingPredicates.notReportedRecreationByUser(user),
MaskingPredicates.notSoftDeletedRecreation())
.fetch()
.size();

List<Recreation> filteredRecreations =
queryFactory
.selectFrom(recreation)
.where(notReportedRecreationByUser(user), notSoftDeletedRecreation())
.where(
MaskingPredicates.notReportedRecreationByUser(user),
MaskingPredicates.notSoftDeletedRecreation())
.orderBy(recreation.weeklyViewCount.desc())
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
Expand Down

0 comments on commit 82bbacf

Please sign in to comment.