-
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.
feat: search condition & paging with Querydsl (#5)
Querydsl을 활용하여 검색 조건에 맞게 검색 후 페이징 처리 - dto로 조회하여 최적화
- Loading branch information
Showing
5 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
src/main/java/com/api/TaveShot/domain/Post/dto/PostListResponse.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,17 @@ | ||
package com.api.TaveShot.domain.Post.dto; | ||
|
||
import java.util.List; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@Builder | ||
@AllArgsConstructor | ||
public class PostListResponse { | ||
private List<PostResponse> postResponses; | ||
private Integer totalPage; | ||
private Long totalElements; | ||
private Boolean isFirst; | ||
private Boolean isLast; | ||
} |
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
14 changes: 14 additions & 0 deletions
14
src/main/java/com/api/TaveShot/domain/Post/dto/PostSearchCondition.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,14 @@ | ||
package com.api.TaveShot.domain.Post.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@Builder | ||
@AllArgsConstructor | ||
public class PostSearchCondition { | ||
private String writer; | ||
private String content; | ||
private String title; | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/api/TaveShot/domain/Post/repository/PostRepositoryCustom.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,11 @@ | ||
package com.api.TaveShot.domain.Post.repository; | ||
|
||
import com.api.TaveShot.domain.Post.dto.PostResponse; | ||
import com.api.TaveShot.domain.Post.dto.PostSearchCondition; | ||
import java.util.List; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
public interface PostRepositoryCustom { | ||
List<PostResponse> searchPagePost(PostSearchCondition condition, Pageable pageable); | ||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/com/api/TaveShot/domain/Post/repository/PostRepositoryImpl.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,60 @@ | ||
package com.api.TaveShot.domain.Post.repository; | ||
|
||
|
||
import static com.api.TaveShot.domain.Post.domain.QPost.post; | ||
|
||
import com.api.TaveShot.domain.Post.dto.PostResponse; | ||
import com.api.TaveShot.domain.Post.dto.PostSearchCondition; | ||
import com.api.TaveShot.domain.Post.dto.QPostResponse; | ||
import com.api.TaveShot.global.config.DataBaseConfig; | ||
import com.querydsl.core.types.dsl.BooleanExpression; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.util.StringUtils; | ||
|
||
@RequiredArgsConstructor | ||
public class PostRepositoryImpl implements PostRepositoryCustom { | ||
|
||
private final DataBaseConfig dataBaseConfig; | ||
|
||
@Override | ||
public List<PostResponse> searchPagePost(PostSearchCondition condition, Pageable pageable) { | ||
|
||
return dataBaseConfig.jpaQueryFactory() | ||
.select( | ||
new QPostResponse(post.id, post.title, post.content, | ||
post.writer, post.viewCount, post.member.id)) | ||
.from(post) | ||
.where( | ||
containTitle(condition.getTitle()), | ||
containContent(condition.getContent()), | ||
containWriter(condition.getWriter()) | ||
) | ||
.limit(pageable.getPageSize()) | ||
.offset(pageable.getOffset()) | ||
.fetch(); | ||
} | ||
|
||
private BooleanExpression containTitle(String title) { | ||
if (StringUtils.hasText(title)) { | ||
return post.title.contains(title); | ||
} | ||
return null; | ||
} | ||
|
||
private BooleanExpression containContent(String content) { | ||
if (StringUtils.hasText(content)) { | ||
return post.content.contains(content); | ||
} | ||
return null; | ||
} | ||
|
||
private BooleanExpression containWriter(String writer) { | ||
if (StringUtils.hasText(writer)) { | ||
return post.writer.contains(writer); | ||
} | ||
return null; | ||
} | ||
|
||
} |