-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* #70 - feat: `QueryDsl` 설정 추가 게시글 조회 시 좋아요 유무를 알아야 하는데 이때 QueryDsl 을 사용하여 조회한다. * #70 - feat: LAZY 설정 빠진 부분 추가 * #70 - feat: 게시글 조회시 좋아요 여부를 확인하는 api 기능 추가 * #70 - test: 추가된 로직 테스트 코드 작성
- Loading branch information
Showing
13 changed files
with
152 additions
and
13 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
19 changes: 19 additions & 0 deletions
19
src/main/java/cotato/bookitlist/config/QueryDslConfig.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,19 @@ | ||
package cotato.bookitlist.config; | ||
|
||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
import jakarta.persistence.EntityManager; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
@RequiredArgsConstructor | ||
public class QueryDslConfig { | ||
|
||
private final EntityManager em; | ||
|
||
@Bean | ||
public JPAQueryFactory queryFactory() { | ||
return new JPAQueryFactory(em); | ||
} | ||
} |
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
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
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
3 changes: 2 additions & 1 deletion
3
src/main/java/cotato/bookitlist/post/repository/PostRepository.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
9 changes: 9 additions & 0 deletions
9
src/main/java/cotato/bookitlist/post/repository/querydsl/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,9 @@ | ||
package cotato.bookitlist.post.repository.querydsl; | ||
|
||
import cotato.bookitlist.post.dto.PostDto; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
public interface PostRepositoryCustom { | ||
Page<PostDto> findWithLikedByIsbn13(String isbn13, Long memberId, Pageable pageable); | ||
} |
68 changes: 68 additions & 0 deletions
68
src/main/java/cotato/bookitlist/post/repository/querydsl/PostRepositoryCustomImpl.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,68 @@ | ||
package cotato.bookitlist.post.repository.querydsl; | ||
|
||
import com.querydsl.core.types.Projections; | ||
import com.querydsl.core.types.dsl.BooleanExpression; | ||
import com.querydsl.core.types.dsl.Expressions; | ||
import com.querydsl.core.types.dsl.NumberPath; | ||
import com.querydsl.jpa.JPAExpressions; | ||
import com.querydsl.jpa.impl.JPAQuery; | ||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
import cotato.bookitlist.post.dto.PostDto; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.support.PageableExecutionUtils; | ||
|
||
import java.util.List; | ||
|
||
import static cotato.bookitlist.post.domain.QPost.post; | ||
import static cotato.bookitlist.post.domain.QPostLike.postLike; | ||
|
||
@RequiredArgsConstructor | ||
public class PostRepositoryCustomImpl implements PostRepositoryCustom { | ||
|
||
private final JPAQueryFactory queryFactory; | ||
|
||
@Override | ||
public Page<PostDto> findWithLikedByIsbn13(String isbn13, Long memberId, Pageable pageable) { | ||
List<PostDto> result = queryFactory | ||
.select( | ||
Projections.constructor( | ||
PostDto.class, | ||
post.id, | ||
post.member.id, | ||
post.book.id, | ||
post.title, | ||
post.content, | ||
post.likeCount, | ||
post.viewCount, | ||
Expressions.cases() | ||
.when(isLikedByMember(memberId, post.id)) | ||
.then(true) | ||
.otherwise(false) | ||
.as("liked") | ||
) | ||
) | ||
.from(post) | ||
.where(post.book.isbn13.eq(isbn13)) | ||
.offset(pageable.getOffset()) | ||
.limit(pageable.getPageSize()) | ||
.fetch(); | ||
|
||
JPAQuery<Long> countQuery = queryFactory | ||
.select(post.count()) | ||
.from(post) | ||
.where(post.book.isbn13.eq(isbn13)) | ||
.from(post); | ||
|
||
return PageableExecutionUtils.getPage(result, pageable, countQuery::fetchOne); | ||
} | ||
|
||
private BooleanExpression isLikedByMember(Long memberId, NumberPath<Long> postId) { | ||
return JPAExpressions.selectOne() | ||
.from(postLike) | ||
.where(postLike.member.id.eq(memberId) | ||
.and(postLike.post.id.eq(postId))) | ||
.exists(); | ||
} | ||
} |
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
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 |
---|---|---|
|
@@ -23,8 +23,8 @@ VALUES ('[email protected]', 'test', 'test', 'KAKAO', 0, false, CURRENT_TIMESTAMP, | |
('[email protected]', 'test2', 'test2', 'KAKAO', 0, false, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP); | ||
|
||
INSERT INTO post (member_id, book_id, title, content, like_count, view_count, deleted) | ||
VALUES (1, 1, 'postTitle', 'postContent', 1, 0, false), | ||
(2, 1, 'postTitle1', 'Content', 0, 0, false), | ||
VALUES (1, 1, 'postTitle', 'postContent', 0, 0, false), | ||
(2, 1, 'postTitle1', 'Content', 2, 0, false), | ||
(2, 1, 'postTitle2', '제목', 0, 0, false), | ||
(2, 1, 'postTitle3', 'post', 0, 0, false), | ||
(2, 2, 'posTitle', 'ptent', 0, 0, false), | ||
|