From 3030ff37860a46b2f0bc3b3d1ed5a3d6b0827fca Mon Sep 17 00:00:00 2001 From: hangyeol Date: Mon, 27 Nov 2023 01:46:01 +0900 Subject: [PATCH 1/5] =?UTF-8?q?feat:=20QueryDSL=EC=9D=84=20=EC=82=AC?= =?UTF-8?q?=EC=9A=A9=ED=95=9C=20Repository=EC=97=90=EC=84=9C=20orderBy=20?= =?UTF-8?q?=EC=A0=95=EB=A0=AC=20=EA=B0=80=EB=8A=A5=ED=95=98=EA=B2=8C=20Que?= =?UTF-8?q?ryDslUtil=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../backend/global/util/QueryDslUtil.java | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 backend/src/main/java/com/graphy/backend/global/util/QueryDslUtil.java 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..3a8c3bb4 --- /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; + } +} From 82f2768d822021afd7c538b251b039d1e6c17423 Mon Sep 17 00:00:00 2001 From: hangyeol Date: Mon, 27 Nov 2023 01:46:45 +0900 Subject: [PATCH 2/5] =?UTF-8?q?fix:=20project=EC=99=80=20recruitment=20?= =?UTF-8?q?=EB=A6=AC=EC=8A=A4=ED=8A=B8=20=EC=A1=B0=ED=9A=8C=20repository?= =?UTF-8?q?=EC=97=90=20=EC=A0=95=EB=A0=AC=EA=B0=80=EB=8A=A5=ED=95=98?= =?UTF-8?q?=EA=B2=8C=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/custom/ProjectCustomRepositoryImpl.java | 4 ++++ .../repository/custom/RecruitmentCustomRepositoryImpl.java | 6 ++++++ 2 files changed, 10 insertions(+) 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..e5a16da1 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 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..3714102f 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(); } From 22c45a8f09cf2daf1ca5f484689108c52d3c282f Mon Sep 17 00:00:00 2001 From: hangyeol Date: Mon, 27 Nov 2023 10:51:37 +0900 Subject: [PATCH 3/5] =?UTF-8?q?fix:=20=EB=B3=80=EC=88=98=EB=AA=85=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/graphy/backend/global/util/QueryDslUtil.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 index 3a8c3bb4..360b358e 100644 --- a/backend/src/main/java/com/graphy/backend/global/util/QueryDslUtil.java +++ b/backend/src/main/java/com/graphy/backend/global/util/QueryDslUtil.java @@ -25,7 +25,7 @@ public static OrderSpecifier getSortedColumn(Order order, Path parent, Str public static List getAllOrderSpecifiers(Pageable pageable, String entityType) { - List ORDERS = new ArrayList<>(); + List orders = new ArrayList<>(); if (!isEmpty(pageable.getSort())) { for (Sort.Order order : pageable.getSort()) { @@ -43,11 +43,11 @@ public static List getAllOrderSpecifiers(Pageable pageable, Stri throw new IllegalArgumentException("Entity의 타입이 아닙니다."); } OrderSpecifier orderSpecifier = QueryDslUtil.getSortedColumn(direction, path, "createdAt"); - ORDERS.add(orderSpecifier); + orders.add(orderSpecifier); } } } - return ORDERS; + return orders; } } From 914c1aec09903f72f8fd4899874fcaccc7fbc566 Mon Sep 17 00:00:00 2001 From: hangyeol Date: Mon, 27 Nov 2023 10:52:52 +0900 Subject: [PATCH 4/5] =?UTF-8?q?fix:=20repository=EC=97=90=20=EC=9E=88?= =?UTF-8?q?=EB=8A=94=20=EB=B3=80=EC=88=98=20=EB=AA=85=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/custom/ProjectCustomRepositoryImpl.java | 4 ++-- .../repository/custom/RecruitmentCustomRepositoryImpl.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) 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 e5a16da1..4a9d257b 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 @@ -27,12 +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 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)) + .orderBy(orders.stream().toArray(OrderSpecifier[]::new)) .fetch(); JPQLQuery count = jpaQueryFactory 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 3714102f..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 @@ -31,7 +31,7 @@ public List findRecruitments(List positions, Boolean isRecruiting, Pageable pageable) { - List ORDERS = QueryDslUtil.getAllOrderSpecifiers(pageable, recruitment.getMetadata().getName()); + List orders = QueryDslUtil.getAllOrderSpecifiers(pageable, recruitment.getMetadata().getName()); return jpaQueryFactory .selectFrom(recruitment) @@ -46,7 +46,7 @@ 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)) + .orderBy(orders.stream().toArray(OrderSpecifier[]::new)) .limit(pageable.getPageSize()) .fetch(); From 633fe636f665e4451137a5eefd8dcfeb287cf7d3 Mon Sep 17 00:00:00 2001 From: hangyeol Date: Mon, 27 Nov 2023 10:53:57 +0900 Subject: [PATCH 5/5] =?UTF-8?q?feat:=20findFollowingProjects=20=EB=A9=94?= =?UTF-8?q?=EC=84=9C=EB=93=9C=EC=97=90=EB=8F=84=20=EC=A0=95=EB=A0=AC=20?= =?UTF-8?q?=EB=B6=80=EB=B6=84=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../project/repository/custom/ProjectCustomRepositoryImpl.java | 2 ++ 1 file changed, 2 insertions(+) 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 4a9d257b..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 @@ -45,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())