Skip to content

Commit

Permalink
feat: search condition & paging with Querydsl (#5)
Browse files Browse the repository at this point in the history
Querydsl을 활용하여 검색 조건에 맞게 검색 후 페이징 처리 - dto로 조회하여 최적화
  • Loading branch information
toychip committed Dec 14, 2023
1 parent 48dd1cc commit 713597d
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 0 deletions.
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;
}
12 changes: 12 additions & 0 deletions src/main/java/com/api/TaveShot/domain/Post/dto/PostResponse.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.api.TaveShot.domain.Post.dto;

import com.api.TaveShot.domain.Comment.dto.CommentDto;
import com.api.TaveShot.domain.Comment.dto.CommentDto.Response;
import com.querydsl.core.annotations.QueryProjection;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand All @@ -18,4 +20,14 @@ public class PostResponse {
private int view;
private Long writerId;
private List<CommentDto.Response> comments;

@QueryProjection
public PostResponse(Long postId, String title, String content, String writer, int view, Long writerId) {
this.postId = postId;
this.title = title;
this.content = content;
this.writer = writer;
this.view = view;
this.writerId = writerId;
}
}
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;
}
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);
}
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;
}

}

0 comments on commit 713597d

Please sign in to comment.