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 @@ -7,6 +7,7 @@ public record CategoryResponse(
String name,
String description
) {

public static CategoryResponse from(CategoryEntity entity) {
return new CategoryResponse(
entity.getId(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,17 @@ public ApiResponse<PromptLikeResponse> likePrompt(@PathVariable Long promptId, @
PromptLikeResponse likePrompt = promptService.likePrompt(promptId, userId);
return ApiResponse.success(likePrompt);
}

/**
* 프롬프트 제목 기준 검색 API
* @param keyword
* @param pageable
* @return
*/
@GetMapping("/search/{keyword}")
public ApiResponse<PromptListResponse> searchPrompts(@PathVariable("keyword") String keyword, Pageable pageable) {
PromptListResponse promptListResponse = promptService.searchPromptsByTitle(keyword, pageable);
return ApiResponse.success(promptListResponse);
}
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.progbe.domain.prompt.dto;

import com.progbe.domain.category.dto.CategoryResponse;
import com.progbe.domain.prompt.entity.PromptEntity;

import java.time.LocalDateTime;

Expand All @@ -11,5 +12,14 @@ public record PromptSummaryResponse(
LocalDateTime createdAt,
LocalDateTime updatedAt
) {
public static PromptSummaryResponse of(PromptEntity promptEntity) {
return new PromptSummaryResponse(
promptEntity.getId(),
CategoryResponse.from(promptEntity.getCategory()),
promptEntity.getTitle(),
promptEntity.getCreatedAt(),
promptEntity.getUpdatedAt()
);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ public interface PromptRepository extends JpaRepository<PromptEntity, Long> {
"ORDER BY COUNT(pl) DESC, p.createdAt DESC")
Page<PromptEntity> findPromptSortedLikeCount(Pageable pageable);

@Query("SELECT p FROM PromptEntity p " +
"JOIN FETCH p.category " +
"WHERE p.title LIKE %:keyword% " +
"AND p.deletedAt IS NULL " +
"ORDER BY p.createdAt DESC")
Page<PromptEntity> findByTitleContaining(@Param("keyword") String keyword, Pageable pageable);


@Query("SELECT p FROM PromptEntity p " +
"JOIN FETCH p.category " +
"LEFT JOIN PromptLikeEntity pl ON pl.prompt = p " +
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/progbe/domain/prompt/service/PromptService.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,16 @@ public PromptLikeResponse likePrompt(Long promptId, Long userId) {
promptLikeRepository.save(newLike);
return PromptLikeResponse.of(LikeStatus.LIKE);
}

// 프롬프트 검색 로직 (#33)
public PromptListResponse searchPromptsByTitle(String keyword, Pageable pageable) {
Page<PromptEntity> searchResult = promptRepository.findByTitleContaining(keyword, pageable);

return new PromptListResponse(
searchResult.getContent().stream()
.map(PromptSummaryResponse::of)
.toList(),
searchResult.getTotalElements()
);
}
}