Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public PostListResponse getPopularPosts(
public PostSearchResponse search(
@RequestParam String keyword,
@PageableDefault Pageable pageable) {
return mainService.searchPosts(keyword, pageable);
Long userId = SecurityUtil.getCurrentUserIdOrNull();
return mainService.searchPosts(keyword, pageable, userId);
}

@GetMapping("/categories")
Expand Down
28 changes: 3 additions & 25 deletions src/main/java/com/feesh/domain/main/service/MainService.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,33 +58,11 @@ public List<CategoryResponse> getCategories() {
.toList();
}

public PostSearchResponse searchPosts(String keyword, Pageable pageable) {
Page<Post> posts = postRepository.findByTitleContaining(keyword, pageable);

List<PostSummaryResponse> summaries = posts.getContent().stream()
.map(post -> PostSummaryResponse.builder()
.id(post.getId())
.title(post.getTitle())
.build())
.toList();
public PostSearchResponse searchPosts(String keyword, Pageable pageable, Long userId) {
Page<PostSummaryResponse> posts = postRepository.searchPostSummariesByTitle(keyword, pageable, userId);

return PostSearchResponse.builder()
.posts(summaries)
.totalPages(posts.getTotalPages())
.totalElements(posts.getTotalElements())
.build();
}

private PostListResponse toPostListResponse(Page<Post> posts) {
List<PostSummaryResponse> summaries = posts.getContent().stream()
.map(post -> PostSummaryResponse.builder()
.id(post.getId())
.title(post.getTitle())
.build())
.toList();

return PostListResponse.builder()
.posts(summaries)
.posts(posts.getContent())
.totalPages(posts.getTotalPages())
.totalElements(posts.getTotalElements())
.build();
Expand Down
32 changes: 25 additions & 7 deletions src/main/java/com/feesh/domain/post/repository/PostRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@

public interface PostRepository extends JpaRepository<Post, Long> {

Page<Post> findAllByOrderByCreatedAtDesc(Pageable pageable);

Page<Post> findAllByOrderByLikeCountDesc(Pageable pageable);

Page<Post> findByTitleContaining(String keyword, Pageable pageable);

Page<Post> findByAuthorId(Long authorId, Pageable pageable);

void deleteAllByAuthor_Id(Long authorId);
Expand Down Expand Up @@ -87,4 +81,28 @@ Page<PostSummaryResponse> findPopularPostSummaries(
@Param("category") Category category,
Pageable pageable,
@Param("userId") Long userId);
}
@Query(value = """
SELECT new com.feesh.domain.main.dto.PostSummaryResponse(
p.id, p.title, p.category, p.price, p.content,
author.nickname, author.profileImageUrl, p.likeCount,
(SELECT COUNT(c) FROM Comment c WHERE c.post = p AND c.isDeleted = false),
p.createdAt,
CASE WHEN EXISTS (
SELECT 1 FROM PostLike pl WHERE pl.post = p AND pl.user.id = :userId
) THEN true ELSE false END,
p.viewCount
)
FROM Post p
JOIN p.author author
WHERE p.title LIKE CONCAT('%', :keyword, '%')
ORDER BY p.createdAt DESC
""",
countQuery = """
SELECT COUNT(p) FROM Post p
WHERE p.title LIKE CONCAT('%', :keyword, '%')
""")
Page<PostSummaryResponse> searchPostSummariesByTitle(
@Param("keyword") String keyword,
Pageable pageable,
@Param("userId") Long userId);
}
Loading