Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Be/#307 QueryDSL을 사용한 API에서 createdAt으로 정렬안되는 오류 수정 #308

Merged
merged 5 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,10 +27,12 @@ public class ProjectCustomRepositoryImpl implements ProjectCustomRepository {

@Override
public Page<Project> searchProjectsWith(Pageable pageable, String projectName, String content) {
List<OrderSpecifier> orders = QueryDslUtil.getAllOrderSpecifiers(pageable, project.getMetadata().getName());
List<Project> fetch = jpaQueryFactory
.selectFrom(project).where(projectNameLike(projectName), contentLike(content))
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.orderBy(orders.stream().toArray(OrderSpecifier[]::new))
.fetch();

JPQLQuery<Project> count = jpaQueryFactory
Expand All @@ -41,12 +45,14 @@ public Page<Project> searchProjectsWith(Pageable pageable, String projectName, S

@Override
public Page<Project> findFollowingProjects(Pageable pageable, Long fromId) {
List<OrderSpecifier> orders = QueryDslUtil.getAllOrderSpecifiers(pageable, project.getMetadata().getName());
List<Project> 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<Long> count = jpaQueryFactory
.select(project.count())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,6 +31,8 @@ public List<Recruitment> findRecruitments(List<Position> positions,
Boolean isRecruiting,
Pageable pageable) {

List<OrderSpecifier> orders = QueryDslUtil.getAllOrderSpecifiers(pageable, recruitment.getMetadata().getName());

return jpaQueryFactory
.selectFrom(recruitment)
.distinct()
Expand All @@ -42,7 +46,9 @@ public List<Recruitment> findRecruitments(List<Position> 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();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<Object> fieldPath = Expressions.path(Object.class, parent, fieldName);

return new OrderSpecifier(order, fieldPath);
}

public static List<OrderSpecifier> getAllOrderSpecifiers(Pageable pageable, String entityType) {

List<OrderSpecifier> 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<LocalDateTime> 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;
}
}
Loading