Skip to content

Commit

Permalink
Merge pull request #289 from WE-ARE-RACCOONS/RAC-400
Browse files Browse the repository at this point in the history
RAC-400 feat : 기존 페이징 처리 최적화
  • Loading branch information
ywj9811 authored Jun 20, 2024
2 parents 22044bd + 1679342 commit 620a238
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
import static com.postgraduate.domain.senior.domain.entity.constant.Status.WAITING;

@Entity
@Table(indexes = {
@Index(name = "senior_total_info_index", columnList = "totalInfo"),
@Index(name = "senior_hit_index", columnList = "hit"),
@Index(name = "senior_field_index", columnList = "field"),
@Index(name = "senior_etc_field_index", columnList = "etcField"),
@Index(name = "senior_postgradu_index", columnList = "postgradu"),
@Index(name = "senior_etc_postgradu_index", columnList = "etcPostgradu")
})
@Builder
@AllArgsConstructor
@NoArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.postgraduate.domain.senior.domain.repository;

import com.postgraduate.domain.salary.application.dto.SeniorAndAccount;
import com.postgraduate.domain.senior.domain.entity.Senior;
import com.postgraduate.domain.user.domain.entity.User;
import org.springframework.data.domain.Page;
Expand All @@ -10,10 +9,8 @@
import java.util.Optional;

public interface SeniorDslRepository {
Page<Senior> findAllBySearchSeniorWithAdmin(String search, Pageable pageable);
Optional<Senior> findBySeniorId(Long seniorId);
Optional<Senior> findByUserWithAll(User user);
List<SeniorAndAccount> findAllSeniorAndAccount();
List<Senior> findAllSenior();
Page<Senior> findAllByFieldSenior(String field, String postgradu, Pageable pageable);
Page<Senior> findAllBySearchSenior(String search, String sort, Pageable pageable);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.postgraduate.domain.senior.domain.repository;

import com.postgraduate.domain.account.domain.entity.Account;
import com.postgraduate.domain.salary.application.dto.SeniorAndAccount;
import com.postgraduate.domain.senior.domain.entity.Senior;
import com.postgraduate.domain.user.domain.entity.User;
import com.querydsl.core.Tuple;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;
Expand All @@ -13,13 +12,13 @@
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import static com.postgraduate.domain.account.domain.entity.QAccount.account;
import static com.postgraduate.domain.senior.domain.entity.QSenior.senior;
import static com.postgraduate.domain.user.domain.entity.QUser.user;
import static com.querydsl.core.types.Order.ASC;
Expand All @@ -38,21 +37,36 @@ public class SeniorDslRepositoryImpl implements SeniorDslRepository{

@Override
public Page<Senior> findAllBySearchSenior(String search, String sort, Pageable pageable) {
List<Senior> seniors = queryFactory.selectFrom(senior)
List<Tuple> results = queryFactory.select(senior.seniorId, senior.user.nickName, senior.hit)
.from(senior)
.distinct()
.leftJoin(senior.user, user)
.fetchJoin()
.where(
senior.info.totalInfo.like("%" + search + "%")
.or(senior.user.nickName.like("%" + search + "%")),
senior.user.isDelete.eq(FALSE)
)
.orderBy(orderSpecifier(sort))
.orderBy(senior.user.nickName.asc()).
offset(pageable.getOffset())
.orderBy(senior.user.nickName.asc())
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.fetch();

if (CollectionUtils.isEmpty(results)) {
return new PageImpl<>(new ArrayList<>(), pageable, 0);
}

List<Senior> seniors = queryFactory.selectFrom(senior)
.where(senior.seniorId.in(results.stream()
.map(tuple -> tuple.get(senior.seniorId))
.toList()))
.leftJoin(senior.user, user)
.fetchJoin()
.orderBy(orderSpecifier(sort))
.orderBy(senior.user.nickName.asc())
.fetch();


Long total = queryFactory.select(senior.count())
.from(senior)
.distinct()
Expand All @@ -64,7 +78,6 @@ public Page<Senior> findAllBySearchSenior(String search, String sort, Pageable p
)
.fetchOne();


return new PageImpl<>(seniors, pageable, total);
}

Expand All @@ -78,10 +91,10 @@ private OrderSpecifier<?> orderSpecifier(String sort) {

@Override
public Page<Senior> findAllByFieldSenior(String field, String postgradu, Pageable pageable) {
List<Senior> seniors = queryFactory.selectFrom(senior)
List<Tuple> results = queryFactory.select(senior.seniorId, senior.user.nickName, senior.hit)
.from(senior)
.distinct()
.leftJoin(senior.user, user)
.fetchJoin()
.where(
fieldSpecifier(field),
postgraduSpecifier(postgradu),
Expand All @@ -93,6 +106,20 @@ public Page<Senior> findAllByFieldSenior(String field, String postgradu, Pageabl
.limit(pageable.getPageSize())
.fetch();

if (CollectionUtils.isEmpty(results)) {
return new PageImpl<>(new ArrayList<>(), pageable, 0);
}

List<Senior> seniors = queryFactory.selectFrom(senior)
.where(senior.seniorId.in(results.stream()
.map(tuple -> tuple.get(senior.seniorId))
.toList()))
.leftJoin(senior.user, user)
.fetchJoin()
.orderBy(senior.hit.desc())
.orderBy(senior.user.nickName.asc())
.fetch();

Long total = queryFactory.select(senior.count())
.from(senior)
.distinct()
Expand Down Expand Up @@ -142,43 +169,6 @@ private BooleanExpression postgraduSpecifier(String postgradu) {
.orElse(FALSE);
}

@Override
public Page<Senior> findAllBySearchSeniorWithAdmin(String search, Pageable pageable) {
List<Senior> seniors = queryFactory.selectFrom(senior)
.where(
searchLike(search),
senior.user.isDelete.eq(FALSE)
)
.distinct()
.innerJoin(senior.user, user)
.fetchJoin()
.orderBy(senior.createdAt.desc())
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.fetch();

Long total = queryFactory.select(senior.count())
.from(senior)
.where(
searchLike(search),
senior.user.isDelete.eq(FALSE)
)
.distinct()
.innerJoin(senior.user, user)
.fetchOne();

return new PageImpl<>(seniors, pageable, total);
}

private BooleanExpression searchLike(String search) {
if (StringUtils.hasText(search)) {
return senior.user.phoneNumber.contains(search)
.or(senior.user.nickName.contains(search))
.and(senior.profile.isNotNull());
}
return null;
}

@Override
public Optional<Senior> findByUserWithAll(User seniorUser) {
return ofNullable(queryFactory.selectFrom(senior)
Expand All @@ -190,31 +180,6 @@ public Optional<Senior> findByUserWithAll(User seniorUser) {
);
}

@Override
public List<SeniorAndAccount> findAllSeniorAndAccount() {
List<Senior> seniors = queryFactory.selectFrom(senior)
.where(senior.user.isDelete.isFalse())
.fetch();
List<Account> accounts = queryFactory.selectFrom(account)
.distinct()
.where(account.senior.in(seniors))
.leftJoin(account.senior, senior)
.fetchJoin()
.fetch();

return seniors.stream()
.map(senior -> {
Account account = accounts.stream()
.filter(a -> a.getSenior().getSeniorId()
.equals(senior.getSeniorId())
)
.findFirst()
.orElse(null);
return new SeniorAndAccount(senior, account);
})
.toList();
}

@Override
public List<Senior> findAllSenior() {
return queryFactory.selectFrom(senior)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.postgraduate.domain.senior.domain.service;

import com.postgraduate.domain.salary.application.dto.SeniorAndAccount;
import com.postgraduate.domain.senior.domain.entity.Senior;
import com.postgraduate.domain.senior.domain.repository.SeniorRepository;
import com.postgraduate.domain.senior.exception.NoneSeniorException;
Expand All @@ -20,11 +19,6 @@
public class SeniorGetService {
private final SeniorRepository seniorRepository;
private static final int SENIOR_PAGE_SIZE = 10;
private static final int ADMIN_PAGE_SIZE = 15;

public List<SeniorAndAccount> findAllSeniorAndAccount() {
return seniorRepository.findAllSeniorAndAccount();
}

public Senior byUser(User user) {
return seniorRepository.findByUser(user).orElseThrow(NoneSeniorException::new);
Expand All @@ -40,12 +34,6 @@ public Senior bySeniorNickName(String nickName) {
.orElseThrow(NoneSeniorException::new);
}

public Page<Senior> all(Integer page, String search) {
page = page == null ? 1 : page;
Pageable pageable = PageRequest.of(page - 1, ADMIN_PAGE_SIZE);
return seniorRepository.findAllBySearchSeniorWithAdmin(search, pageable);
}

public List<Senior> allSeniorId() {
return seniorRepository.findAllByUser_IsDelete(FALSE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.postgraduate.domain.user.domain.entity.constant.Role;
import jakarta.persistence.*;
import jakarta.persistence.Index;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -11,6 +13,10 @@
import java.time.LocalDateTime;

@Entity
@Table(indexes = {
@Index(name = "user_nick_name_index", columnList = "nickName"),
@Index(name = "user_is_delete", columnList = "isDelete")
})
@Builder
@AllArgsConstructor
@NoArgsConstructor
Expand Down

0 comments on commit 620a238

Please sign in to comment.