-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Be/#307 QueryDSL을 사용한 API에서 createdAt으로 정렬안되는 오류 수정
- Loading branch information
Showing
3 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
backend/src/main/java/com/graphy/backend/global/util/QueryDslUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |