Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] 게시글 검색 API #32

Merged
merged 4 commits into from
Mar 3, 2024
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 @@ -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 Down Expand Up @@ -40,4 +41,10 @@ public ApiResponse<?> deletePost(@MemberId Long memberId, @PathVariable Long pos
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));
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package org.harang.server.repository;

import java.util.List;
import org.harang.server.domain.Post;
import org.springframework.data.jpa.repository.JpaRepository;
import org.harang.server.dto.type.ErrorMessage;
import org.harang.server.exception.CustomException;
import org.springframework.data.jpa.repository.Query;

public interface PostRepository extends JpaRepository<Post, Long> {
default Post findByIdOrThrow(Long id) {
return findById(id).orElseThrow(() -> new CustomException(ErrorMessage.POST_NOT_FOUND));
}

List<Post> findByTitleIsContaining(String title);
}
11 changes: 9 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,6 +77,13 @@ public List<PostResponse> getAllPosts() {
.collect(Collectors.toList());
}

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

@Transactional
public void deletePost(Long memberId, Long postId) {
Post post = postRepository.findByIdOrThrow(postId);
Expand Down
Loading