Skip to content

Commit

Permalink
feat: 글 검색 조회 구현#3
Browse files Browse the repository at this point in the history
  • Loading branch information
firefox1234123 committed Aug 5, 2024
1 parent 994eee1 commit ef4e871
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ public ResponseEntity<PostListResDto> postFindByUserId(@PathVariable("userId") L
}

// 글 검색 조회

@GetMapping("/search/{input}")
public ResponseEntity<PostListResDto> postFindByInput(@PathVariable("input") String input) {
PostListResDto postListResDto = postService.postFindByInput(input);
return new ResponseEntity<>(postListResDto, HttpStatus.OK);
}

// 글 수정
@PatchMapping("/{postId}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import net.skhu.likelion12thteam03be.s3.S3Service;
import net.skhu.likelion12thteam03be.user.domain.User;
import net.skhu.likelion12thteam03be.user.domain.repository.UserRepository;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestPart;
Expand Down Expand Up @@ -145,6 +146,16 @@ public PostListResDto postFindByUserId(Long userId) {
}

// 글 검색 조회
public PostListResDto postFindByInput(String input) {
String searchInput = "%" + input + "%";
List<Post> posts = postRepository.findByInput(searchInput);

List<PostInfoResDto> postInfoResDtoList = posts.stream()
.map(PostInfoResDto::from)
.toList();

return PostListResDto.from(postInfoResDtoList);
}

// 글 수정
@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;


@Repository
public interface PostRepository extends JpaRepository<Post, Long> {
@Query("select p from Post p join fetch p.moods m where m.moodId = :moodId ")
@Query("select distinct p from Post p join fetch p.moods m where m.moodId = :moodId ")
List<Post> findByMoodId(Long moodId);

@Query("select distinct p from Post p where p.title like :input or p.content like :input")
List<Post> findByInput(String input);
}

0 comments on commit ef4e871

Please sign in to comment.