Skip to content

Commit

Permalink
Feat: querydsl 사용 쿼리 정렬 기능 추가 (#162)
Browse files Browse the repository at this point in the history
* #153 - 좋아요 도메인에 BaseEntity 추가

정렬을 하기 위해 필요하다

* #153 - feat: 내가 좋아요한 정보들 요청시 정렬 추가

* #153 - test: 구현한 기능 테스트 코드 작성
  • Loading branch information
GGHDMS authored Feb 19, 2024
1 parent 9ec2e10 commit e468aed
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 37 deletions.
3 changes: 2 additions & 1 deletion src/main/java/cotato/bookitlist/book/domain/BookLike.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cotato.bookitlist.book.domain;

import cotato.bookitlist.common.domain.BaseEntity;
import cotato.bookitlist.member.domain.Member;
import jakarta.persistence.*;
import lombok.AccessLevel;
Expand All @@ -9,7 +10,7 @@
@Getter
@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class BookLike {
public class BookLike extends BaseEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public interface BookRepository extends JpaRepository<Book, Long> {
@Query("select b from Book b where LOWER(b.title) like LOWER(CONCAT('%', :keyword, '%')) or LOWER(b.author) like LOWER(CONCAT('%', :keyword, '%')) or LOWER(b.description) like LOWER(CONCAT('%', :keyword, '%'))")
Page<Book> findAllByKeyword(@Param("keyword") String keyword, Pageable pageable);

@Query("select b from BookLike l join l.book b join l.member m where m.id = :memberId")
@Query("select b from BookLike bl join bl.book b where bl.member.id = :memberId order by bl.createdAt desc")
Page<Book> findLikeBookByMemberId(Long memberId, Pageable pageable);

@Query(value = "select * from book b order by RAND() LIMIT :count", nativeQuery = true)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cotato.bookitlist.post.domain.entity;

import cotato.bookitlist.common.domain.BaseEntity;
import cotato.bookitlist.member.domain.Member;
import jakarta.persistence.*;
import lombok.AccessLevel;
Expand All @@ -9,7 +10,7 @@
@Getter
@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class PostLike {
public class PostLike extends BaseEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.util.List;
import java.util.Optional;

import static cotato.bookitlist.member.domain.QMember.member;
import static cotato.bookitlist.post.domain.entity.QPost.post;
import static cotato.bookitlist.post.domain.entity.QPostLike.postLike;

Expand Down Expand Up @@ -51,16 +50,15 @@ public Page<PostDto> findPublicPostWithLikedByIsbn13(String isbn13, Long memberI
)
)
.from(post)
.join(post.member, member)
.where(isbnEq(isbn13), memberIdEq(memberId), post.status.eq(PostStatus.PUBLIC), post.member.status.eq(ProfileStatus.PUBLIC))
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.orderBy(post.createdAt.desc())
.fetch();

JPAQuery<Long> countQuery = queryFactory
.select(post.count())
.from(post)
.join(post.member, member)
.where(isbnEq(isbn13), memberIdEq(memberId));

return PageableExecutionUtils.getPage(result, pageable, countQuery::fetchOne);
Expand Down Expand Up @@ -91,7 +89,6 @@ public Optional<PostDetailDto> findPostDetailByPostId(Long postId, Long memberId
)
)
.from(post)
.join(post.member, member)
.where(post.id.eq(postId), buildPostAccessCondition(post.member.id, memberId))
.fetchOne());
}
Expand All @@ -116,17 +113,15 @@ public Page<PostDto> findLikePostByMemberId(Long memberId, Pageable pageable) {
)
.from(postLike)
.join(postLike.post, post)
.join(postLike.member, member)
.where(postLike.member.id.eq(memberId))
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.orderBy(postLike.createdAt.desc())
.fetch();

JPAQuery<Long> countQuery = queryFactory
.select(post.count())
.from(postLike)
.join(postLike.post, post)
.join(postLike.member, member)
.where(postLike.member.id.eq(memberId));

return PageableExecutionUtils.getPage(result, pageable, countQuery::fetchOne);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cotato.bookitlist.review.domain.entity;

import cotato.bookitlist.common.domain.BaseEntity;
import cotato.bookitlist.member.domain.Member;
import jakarta.persistence.*;
import lombok.AccessLevel;
Expand All @@ -9,7 +10,7 @@
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class ReviewLike {
public class ReviewLike extends BaseEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public Page<ReviewDto> findPublicReviewWithLikedByIsbn13(String isbn13, Long mem
.where(review.book.isbn13.eq(isbn13), review.status.eq(ReviewStatus.PUBLIC), review.member.status.eq(ProfileStatus.PUBLIC))
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.orderBy(review.createdAt.desc())
.fetch();

JPAQuery<Long> countQuery = queryFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ void givenLoginMember_whenGettingLikePosts_thenReturnBookListResponse() throws E
mockMvc.perform(get("/books/likes/all"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.totalResults").value(2))
.andExpect(jsonPath("$.bookList[0].isbn13").value("9788931514810"))
;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ void givenLogin_whenSearchingLikePost_thenReturnPostListResponse() throws Except
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.totalResults").value(3))
.andExpect(jsonPath("$.postList[0].postId").value(7))
;
}

Expand Down
68 changes: 42 additions & 26 deletions src/test/resources/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,39 @@ VALUES ('[email protected]', 'test', 'test', 'KAKAO', 'PUBLIC', 'profile', false, T
('[email protected]', 'test2', 'test4', 'KAKAO', 'PRIVATE', 'profile', false, TIMESTAMP '2024-02-11 00:00:00',
CURRENT_TIMESTAMP);

INSERT INTO post (member_id, book_id, title, content, status, template, like_count, view_count, deleted, created_at, modified_at)
VALUES (1, 1, 'posttitle', 'postcontent', 'PUBLIC', 'NON', 2, 0, false, TIMESTAMP '2024-02-15 00:00:00', CURRENT_TIMESTAMP),
(2, 1, 'postTitle1', 'Content', 'PUBLIC', 'NON', 3, 0, false, TIMESTAMP '2024-02-14 00:00:00', CURRENT_TIMESTAMP),
INSERT INTO post (member_id, book_id, title, content, status, template, like_count, view_count, deleted, created_at,
modified_at)
VALUES (1, 1, 'posttitle', 'postcontent', 'PUBLIC', 'NON', 2, 0, false, TIMESTAMP '2024-02-15 00:00:00',
CURRENT_TIMESTAMP),
(2, 1, 'postTitle1', 'Content', 'PUBLIC', 'NON', 3, 0, false, TIMESTAMP '2024-02-14 00:00:00',
CURRENT_TIMESTAMP),
(2, 1, 'postTitle2', '제목', 'PUBLIC', 'NON', 0, 0, false, TIMESTAMP '2024-02-13 00:00:00', CURRENT_TIMESTAMP),
(2, 1, 'postTitle3', 'post', 'PUBLIC', 'NON', 0, 0, false, TIMESTAMP '2024-02-12 00:00:00', CURRENT_TIMESTAMP),
(2, 1, 'privateTitle', 'privateContent', 'PRIVATE', 'NON', 0, 0, false, TIMESTAMP '2024-02-12 00:00:00', CURRENT_TIMESTAMP),
(2, 1, 'privateTitle', 'privateContent', 'PRIVATE', 'NON', 0, 0, false, TIMESTAMP '2024-02-12 00:00:00',
CURRENT_TIMESTAMP),
(2, 2, 'posTitle', 'ptent', 'PUBLIC', 'NON', 1, 0, false, TIMESTAMP '2024-02-12 00:00:00', CURRENT_TIMESTAMP),
(2, 2, 'positle', 'postent', 'PUBLIC', 'NON', 1, 0, false, TIMESTAMP '2024-02-12 00:00:00', CURRENT_TIMESTAMP),
(2, 2, 'privateTitle', 'privateContent', 'PRIVATE', 'NON', 0, 0, false, TIMESTAMP '2024-02-12 00:00:00', CURRENT_TIMESTAMP),
(2, 2, 'privateTitle', 'privateContent', 'PRIVATE', 'NON', 0, 0, false, TIMESTAMP '2024-02-12 00:00:00',
CURRENT_TIMESTAMP),
(2, 2, 'privateTitle',
'first<============================>second<============================>third<============================>fourth',
'PUBLIC', 'TEMPLATE', 0, 0, false, TIMESTAMP '2024-02-12 00:00:00', CURRENT_TIMESTAMP),
(1, 2, 'posttitle', 'postcontent', 'PUBLIC', 'NON', 0, 0, false, TIMESTAMP '2024-02-12 00:00:00', CURRENT_TIMESTAMP),
(1, 2, 'posttitle', 'postcontent', 'PRIVATE', 'NON', 0, 0, false, TIMESTAMP '2024-02-12 00:00:00', CURRENT_TIMESTAMP),
(4, 2, 'posttitle', 'postcontent', 'PUBLIC', 'NON', 0, 0, false, TIMESTAMP '2024-02-12 00:00:00', CURRENT_TIMESTAMP),
(4, 2, 'posttitle', 'postcontent', 'PRIVATE', 'NON', 0, 0, false, TIMESTAMP '2024-02-12 00:00:00', CURRENT_TIMESTAMP);
(1, 2, 'posttitle', 'postcontent', 'PUBLIC', 'NON', 0, 0, false, TIMESTAMP '2024-02-12 00:00:00',
CURRENT_TIMESTAMP),
(1, 2, 'posttitle', 'postcontent', 'PRIVATE', 'NON', 0, 0, false, TIMESTAMP '2024-02-12 00:00:00',
CURRENT_TIMESTAMP),
(4, 2, 'posttitle', 'postcontent', 'PUBLIC', 'NON', 0, 0, false, TIMESTAMP '2024-02-12 00:00:00',
CURRENT_TIMESTAMP),
(4, 2, 'posttitle', 'postcontent', 'PRIVATE', 'NON', 0, 0, false, TIMESTAMP '2024-02-12 00:00:00',
CURRENT_TIMESTAMP);


VALUES (2, 1, CURRENT_TIMESTAMP),
(3, 1, CURRENT_TIMESTAMP + 1),
(1, 7, CURRENT_TIMESTAMP + 2),
(2, 2, CURRENT_TIMESTAMP + 3),
(1, 6, CURRENT_TIMESTAMP + 4),
(1, 2, CURRENT_TIMESTAMP + 5);

INSERT INTO review (member_id, book_id, content, status, like_count, view_count, deleted, created_at, modified_at)
VALUES (1, 1, 'reviewContent', 'PUBLIC', 2, 0, false, TIMESTAMP '2024-02-15 00:00:00', CURRENT_TIMESTAMP),
Expand All @@ -60,22 +76,22 @@ VALUES (1, 1, 'reviewContent', 'PUBLIC', 2, 0, false, TIMESTAMP '2024-02-15 00:0
(1, 2, 'rContent', 'PRIVATE', 0, 0, false, TIMESTAMP '2024-02-12 00:00:00', CURRENT_TIMESTAMP);


INSERT INTO post_like (member_id, post_id)
VALUES (2, 1),
(3, 1),
(1, 2),
(2, 2),
(1, 6),
(1, 7),
(5, 2);
INSERT INTO post_like (member_id, post_id, created_at)
VALUES (2, 1, current_timestamp),
(3, 1, current_timestamp + 1),
(1, 2, current_timestamp + 2),
(2, 2, current_timestamp + 3),
(1, 6, current_timestamp + 4),
(1, 7, current_timestamp + 5),
(5, 2, current_timestamp + 6);

INSERT INTO review_like (member_id, review_id)
VALUES (2, 1),
(3, 1),
(1, 2),
(2, 2),
(5, 2);
INSERT INTO review_like (member_id, review_id, created_at)
VALUES (2, 1, current_timestamp),
(3, 1, current_timestamp + 1),
(1, 2, current_timestamp + 2),
(2, 2, current_timestamp + 3),
(5, 2, current_timestamp + 4);

INSERT INTO book_like (book_id, member_id)
VALUES (1, 1),
(2, 1);
INSERT INTO book_like (book_id, member_id, created_at)
VALUES (2, 1, CURRENT_TIMESTAMP),
(1, 1, CURRENT_TIMESTAMP + 1);

0 comments on commit e468aed

Please sign in to comment.