Skip to content

Commit

Permalink
[feat] #21 게시글 검색 결과 조회 API
Browse files Browse the repository at this point in the history
  • Loading branch information
nykoh2001 committed Feb 17, 2024
1 parent 0179c61 commit 0fd95a8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.harang.server.dto.response.PostResponse;
import org.harang.server.dto.type.SuccessMessage;
import org.harang.server.service.PostService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
Expand All @@ -34,4 +35,10 @@ public ApiResponse<?> createPost(@MemberId Long memberId, @Valid @RequestBody Po
public ApiResponse<List<PostResponse>> getAllPosts() {
return ApiResponse.success(postService.getAllPosts());
}

@Transactional(readOnly = true)
@GetMapping("/search")
public ApiResponse<?> getSearchResults(@RequestParam(name = "title") String title) {
return ApiResponse.success(postService.getSearchResults(title));
}
}
10 changes: 8 additions & 2 deletions src/main/java/org/harang/server/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ public Post createPost(Long memberId, PostRequest request) {
.build()
);

for (String categoryName: request.categoryList()) {
for (String categoryName : request.categoryList()) {
Category category = categoryRepository.findByName(categoryName);
if(category != null) {
if (category != null) {
PostCategory savedPostCategory =
PostCategory.builder()
.post(savedPost)
Expand All @@ -77,4 +77,10 @@ public List<PostResponse> getAllPosts() {
.collect(Collectors.toList());
}

public List<PostResponse> getSearchResults(String title) {
return postRepository.findByTitleIsContaining(title)
.stream()
.map(p -> PostResponse.of(p))
.toList();
}
}

0 comments on commit 0fd95a8

Please sign in to comment.