diff --git a/backend/src/main/java/com/graphy/backend/domain/project/repository/custom/ProjectCustomRepositoryImpl.java b/backend/src/main/java/com/graphy/backend/domain/project/repository/custom/ProjectCustomRepositoryImpl.java index efd68ae8..835cde37 100644 --- a/backend/src/main/java/com/graphy/backend/domain/project/repository/custom/ProjectCustomRepositoryImpl.java +++ b/backend/src/main/java/com/graphy/backend/domain/project/repository/custom/ProjectCustomRepositoryImpl.java @@ -2,6 +2,8 @@ import com.graphy.backend.domain.project.domain.Project; import com.graphy.backend.domain.project.repository.ProjectCustomRepository; +import com.graphy.backend.global.util.QueryDslUtil; +import com.querydsl.core.types.OrderSpecifier; import com.querydsl.core.types.dsl.BooleanExpression; import com.querydsl.jpa.JPQLQuery; import com.querydsl.jpa.impl.JPAQuery; @@ -25,10 +27,12 @@ public class ProjectCustomRepositoryImpl implements ProjectCustomRepository { @Override public Page searchProjectsWith(Pageable pageable, String projectName, String content) { + List orders = QueryDslUtil.getAllOrderSpecifiers(pageable, project.getMetadata().getName()); List fetch = jpaQueryFactory .selectFrom(project).where(projectNameLike(projectName), contentLike(content)) .offset(pageable.getOffset()) .limit(pageable.getPageSize()) + .orderBy(orders.stream().toArray(OrderSpecifier[]::new)) .fetch(); JPQLQuery count = jpaQueryFactory @@ -41,12 +45,14 @@ public Page searchProjectsWith(Pageable pageable, String projectName, S @Override public Page findFollowingProjects(Pageable pageable, Long fromId) { + List orders = QueryDslUtil.getAllOrderSpecifiers(pageable, project.getMetadata().getName()); List fetch = jpaQueryFactory.selectFrom(project) .where(project.member.id.in( select(follow.toId).from(follow).where(follow.fromId.eq(fromId) ))) .offset(pageable.getOffset()) .limit(pageable.getPageSize()) + .orderBy(orders.stream().toArray(OrderSpecifier[]::new)) .fetch(); JPAQuery count = jpaQueryFactory .select(project.count()) diff --git a/backend/src/main/java/com/graphy/backend/domain/recruitment/repository/custom/RecruitmentCustomRepositoryImpl.java b/backend/src/main/java/com/graphy/backend/domain/recruitment/repository/custom/RecruitmentCustomRepositoryImpl.java index 18c91506..15c779d5 100644 --- a/backend/src/main/java/com/graphy/backend/domain/recruitment/repository/custom/RecruitmentCustomRepositoryImpl.java +++ b/backend/src/main/java/com/graphy/backend/domain/recruitment/repository/custom/RecruitmentCustomRepositoryImpl.java @@ -3,7 +3,9 @@ import com.graphy.backend.domain.recruitment.domain.Position; import com.graphy.backend.domain.recruitment.domain.Recruitment; import com.graphy.backend.domain.recruitment.repository.RecruitmentCustomRepository; +import com.graphy.backend.global.util.QueryDslUtil; import com.querydsl.core.BooleanBuilder; +import com.querydsl.core.types.OrderSpecifier; import com.querydsl.core.types.dsl.BooleanExpression; import com.querydsl.jpa.impl.JPAQueryFactory; import lombok.RequiredArgsConstructor; @@ -29,6 +31,8 @@ public List findRecruitments(List positions, Boolean isRecruiting, Pageable pageable) { + List orders = QueryDslUtil.getAllOrderSpecifiers(pageable, recruitment.getMetadata().getName()); + return jpaQueryFactory .selectFrom(recruitment) .distinct() @@ -42,7 +46,9 @@ public List findRecruitments(List positions, .leftJoin(recruitmentTag).on(recruitmentTag.recruitment.eq(recruitment)) .leftJoin(recruitmentTag.tag, tag) .offset(pageable.getOffset()) + .orderBy(orders.stream().toArray(OrderSpecifier[]::new)) .limit(pageable.getPageSize()) + .fetch(); } diff --git a/backend/src/main/java/com/graphy/backend/global/util/QueryDslUtil.java b/backend/src/main/java/com/graphy/backend/global/util/QueryDslUtil.java new file mode 100644 index 00000000..360b358e --- /dev/null +++ b/backend/src/main/java/com/graphy/backend/global/util/QueryDslUtil.java @@ -0,0 +1,53 @@ +package com.graphy.backend.global.util; + +import static org.springframework.util.ObjectUtils.isEmpty; + +import com.graphy.backend.domain.project.domain.QProject; +import com.graphy.backend.domain.recruitment.domain.QRecruitment; +import com.querydsl.core.types.Order; +import com.querydsl.core.types.OrderSpecifier; +import com.querydsl.core.types.Path; +import com.querydsl.core.types.dsl.DateTimePath; +import com.querydsl.core.types.dsl.Expressions; +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; + +public class QueryDslUtil { + + public static OrderSpecifier getSortedColumn(Order order, Path parent, String fieldName) { + Path fieldPath = Expressions.path(Object.class, parent, fieldName); + + return new OrderSpecifier(order, fieldPath); + } + + public static List getAllOrderSpecifiers(Pageable pageable, String entityType) { + + List orders = new ArrayList<>(); + + if (!isEmpty(pageable.getSort())) { + for (Sort.Order order : pageable.getSort()) { + if ("createdAt".equals(order.getProperty())) { + Order direction = order.getDirection().isAscending() ? Order.ASC : Order.DESC; + DateTimePath path; + switch (entityType) { + case "recruitment": + path = QRecruitment.recruitment.createdAt; + break; + case "project": + path = QProject.project.createdAt; + break; + default: + throw new IllegalArgumentException("Entity의 타입이 아닙니다."); + } + OrderSpecifier orderSpecifier = QueryDslUtil.getSortedColumn(direction, path, "createdAt"); + orders.add(orderSpecifier); + } + } + } + + return orders; + } +}