|
| 1 | +package com.example.solidconnection.mentor.repository.custom; |
| 2 | + |
| 3 | +import static com.example.solidconnection.location.country.domain.QCountry.country; |
| 4 | +import static com.example.solidconnection.location.region.domain.QRegion.region; |
| 5 | +import static com.example.solidconnection.mentor.domain.QMentorApplication.mentorApplication; |
| 6 | +import static com.example.solidconnection.siteuser.domain.QSiteUser.siteUser; |
| 7 | +import static com.example.solidconnection.university.domain.QUniversity.university; |
| 8 | +import static org.springframework.util.StringUtils.hasText; |
| 9 | + |
| 10 | +import com.example.solidconnection.admin.dto.MentorApplicationResponse; |
| 11 | +import com.example.solidconnection.admin.dto.MentorApplicationSearchCondition; |
| 12 | +import com.example.solidconnection.admin.dto.MentorApplicationSearchResponse; |
| 13 | +import com.example.solidconnection.admin.dto.SiteUserResponse; |
| 14 | +import com.example.solidconnection.mentor.domain.MentorApplicationStatus; |
| 15 | +import com.querydsl.core.types.ConstructorExpression; |
| 16 | +import com.querydsl.core.types.Projections; |
| 17 | +import com.querydsl.core.types.dsl.BooleanExpression; |
| 18 | +import com.querydsl.jpa.impl.JPAQuery; |
| 19 | +import com.querydsl.jpa.impl.JPAQueryFactory; |
| 20 | +import jakarta.persistence.EntityManager; |
| 21 | +import java.time.LocalDate; |
| 22 | +import java.time.LocalDateTime; |
| 23 | +import java.time.ZoneId; |
| 24 | +import java.util.List; |
| 25 | +import org.springframework.beans.factory.annotation.Autowired; |
| 26 | +import org.springframework.data.domain.Page; |
| 27 | +import org.springframework.data.domain.PageImpl; |
| 28 | +import org.springframework.data.domain.Pageable; |
| 29 | +import org.springframework.stereotype.Repository; |
| 30 | + |
| 31 | +@Repository |
| 32 | +public class MentorApplicationFilterRepositoryImpl implements MentorApplicationFilterRepository { |
| 33 | + |
| 34 | + private static final ZoneId SYSTEM_ZONE_ID = ZoneId.systemDefault(); |
| 35 | + |
| 36 | + private static final ConstructorExpression<SiteUserResponse> SITE_USER_RESPONSE_PROJECTION = |
| 37 | + Projections.constructor( |
| 38 | + SiteUserResponse.class, |
| 39 | + siteUser.id, |
| 40 | + siteUser.nickname, |
| 41 | + siteUser.profileImageUrl |
| 42 | + ); |
| 43 | + |
| 44 | + private static final ConstructorExpression<MentorApplicationResponse> MENTOR_APPLICATION_RESPONSE_PROJECTION = |
| 45 | + Projections.constructor( |
| 46 | + MentorApplicationResponse.class, |
| 47 | + mentorApplication.id, |
| 48 | + region.koreanName, |
| 49 | + country.koreanName, |
| 50 | + university.koreanName, |
| 51 | + mentorApplication.mentorProofUrl, |
| 52 | + mentorApplication.mentorApplicationStatus, |
| 53 | + mentorApplication.rejectedReason, |
| 54 | + mentorApplication.createdAt, |
| 55 | + mentorApplication.approvedAt |
| 56 | + ); |
| 57 | + |
| 58 | + private static final ConstructorExpression<MentorApplicationSearchResponse> MENTOR_APPLICATION_SEARCH_RESPONSE_PROJECTION = |
| 59 | + Projections.constructor( |
| 60 | + MentorApplicationSearchResponse.class, |
| 61 | + SITE_USER_RESPONSE_PROJECTION, |
| 62 | + MENTOR_APPLICATION_RESPONSE_PROJECTION |
| 63 | + ); |
| 64 | + |
| 65 | + private final JPAQueryFactory queryFactory; |
| 66 | + |
| 67 | + @Autowired |
| 68 | + public MentorApplicationFilterRepositoryImpl(EntityManager em) { |
| 69 | + this.queryFactory = new JPAQueryFactory(em); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public Page<MentorApplicationSearchResponse> searchMentorApplications(MentorApplicationSearchCondition condition, Pageable pageable) { |
| 74 | + List<MentorApplicationSearchResponse> content = queryFactory |
| 75 | + .select(MENTOR_APPLICATION_SEARCH_RESPONSE_PROJECTION) |
| 76 | + .from(mentorApplication) |
| 77 | + .join(siteUser).on(mentorApplication.siteUserId.eq(siteUser.id)) |
| 78 | + .leftJoin(university).on(mentorApplication.universityId.eq(university.id)) |
| 79 | + .leftJoin(region).on(university.region.eq(region)) |
| 80 | + .leftJoin(country).on(university.country.eq(country)) |
| 81 | + .where( |
| 82 | + verifyMentorStatusEq(condition.mentorApplicationStatus()), |
| 83 | + keywordContains(condition.keyword()), |
| 84 | + createdAtEq(condition.createdAt()) |
| 85 | + ) |
| 86 | + .orderBy(mentorApplication.createdAt.desc()) |
| 87 | + .offset(pageable.getOffset()) |
| 88 | + .limit(pageable.getPageSize()) |
| 89 | + .fetch(); |
| 90 | + |
| 91 | + Long totalCount = createCountQuery(condition).fetchOne(); |
| 92 | + |
| 93 | + return new PageImpl<>(content, pageable, totalCount != null ? totalCount : 0L); |
| 94 | + } |
| 95 | + |
| 96 | + private JPAQuery<Long> createCountQuery(MentorApplicationSearchCondition condition) { |
| 97 | + JPAQuery<Long> query = queryFactory |
| 98 | + .select(mentorApplication.count()) |
| 99 | + .from(mentorApplication); |
| 100 | + |
| 101 | + String keyword = condition.keyword(); |
| 102 | + |
| 103 | + if (hasText(keyword)) { |
| 104 | + query.join(siteUser).on(mentorApplication.siteUserId.eq(siteUser.id)) |
| 105 | + .leftJoin(university).on(mentorApplication.universityId.eq(university.id)) |
| 106 | + .leftJoin(region).on(university.region.eq(region)) |
| 107 | + .leftJoin(country).on(university.country.eq(country)); |
| 108 | + } |
| 109 | + |
| 110 | + return query.where( |
| 111 | + verifyMentorStatusEq(condition.mentorApplicationStatus()), |
| 112 | + keywordContains(condition.keyword()), |
| 113 | + createdAtEq(condition.createdAt()) |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + private BooleanExpression verifyMentorStatusEq(MentorApplicationStatus status) { |
| 118 | + return status != null ? mentorApplication.mentorApplicationStatus.eq(status) : null; |
| 119 | + } |
| 120 | + |
| 121 | + private BooleanExpression keywordContains(String keyword) { |
| 122 | + if (!hasText(keyword)) { |
| 123 | + return null; |
| 124 | + } |
| 125 | + |
| 126 | + return siteUser.nickname.containsIgnoreCase(keyword) |
| 127 | + .or(university.koreanName.containsIgnoreCase(keyword)) |
| 128 | + .or(region.koreanName.containsIgnoreCase(keyword)) |
| 129 | + .or(country.koreanName.containsIgnoreCase(keyword)); |
| 130 | + } |
| 131 | + |
| 132 | + private BooleanExpression createdAtEq(LocalDate createdAt) { |
| 133 | + if (createdAt == null) { |
| 134 | + return null; |
| 135 | + } |
| 136 | + |
| 137 | + LocalDateTime startOfDay = createdAt.atStartOfDay(); |
| 138 | + LocalDateTime endOfDay = createdAt.plusDays(1).atStartOfDay().minusNanos(1); |
| 139 | + |
| 140 | + return mentorApplication.createdAt.between( |
| 141 | + startOfDay.atZone(SYSTEM_ZONE_ID), |
| 142 | + endOfDay.atZone(SYSTEM_ZONE_ID) |
| 143 | + ); |
| 144 | + } |
| 145 | +} |
0 commit comments