diff --git a/src/main/java/net/skhu/likelion12thteam03be/post/api/dto/PostController.java b/src/main/java/net/skhu/likelion12thteam03be/post/api/dto/PostController.java index 1e7cf2c..c079f46 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/post/api/dto/PostController.java +++ b/src/main/java/net/skhu/likelion12thteam03be/post/api/dto/PostController.java @@ -77,7 +77,18 @@ public ResponseEntity postFindByUserId(@PathVariable("userId") L } // 글 검색 조회 + @GetMapping("/search/{input}") + public ResponseEntity postFindByInput(@PathVariable("input") String input) { + PostListResDto postListResDto = postService.postFindByInput(input); + return new ResponseEntity<>(postListResDto, HttpStatus.OK); + } + // 글 추천 조회 + @GetMapping("/recommend/{recommend}") + public ResponseEntity postFindByRecommend(@PathVariable("recommend") String recommend) { + PostListResDto postListResDto = postService.postFindByRecommend(recommend); + return new ResponseEntity<>(postListResDto, HttpStatus.OK); + } // 글 수정 @PatchMapping("/{postId}") diff --git a/src/main/java/net/skhu/likelion12thteam03be/post/application/PostService.java b/src/main/java/net/skhu/likelion12thteam03be/post/application/PostService.java index 136e61e..dc6fdda 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/post/application/PostService.java +++ b/src/main/java/net/skhu/likelion12thteam03be/post/application/PostService.java @@ -1,7 +1,6 @@ package net.skhu.likelion12thteam03be.post.application; import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; import net.skhu.likelion12thteam03be.category.domain.Category; import net.skhu.likelion12thteam03be.category.domain.repository.CategoryRepository; import net.skhu.likelion12thteam03be.location.domain.Location; @@ -29,7 +28,7 @@ import java.util.Optional; import java.util.stream.Collectors; -@Slf4j + @Service @Transactional(readOnly = false) @RequiredArgsConstructor @@ -43,9 +42,10 @@ public class PostService { @Transactional public void postSave(PostSaveReqDto postSaveReqDto, @RequestPart(required = false) MultipartFile multipartFile, Principal principal) throws IOException { - String imgUrl = s3Service.upload(multipartFile, "post"); String loginId = principal.getName(); + String imgUrl = s3Service.upload(multipartFile, "post"); + User user = userRepository.findByLoginId(loginId) .orElseThrow(() -> new IllegalArgumentException("해당 유저가 존재하지 않습니다. loginId = " + loginId)); @@ -147,10 +147,42 @@ public PostListResDto postFindByUserId(Long userId) { } // 글 검색 조회 + public PostListResDto postFindByInput(String input) { + String searchInput = "%" + input + "%"; + List posts = postRepository.findByInput(searchInput); + + List postInfoResDtoList = posts.stream() + .map(PostInfoResDto::from) + .toList(); + + return PostListResDto.from(postInfoResDtoList); + } + + // 글 추천 조회 + public PostListResDto postFindByRecommend(String recommend) { + List postInfoResDtoList = new ArrayList<>(); + List collectPostInfoResDtoList = new ArrayList<>(); + String recommendWord = "%" + recommend + "%"; + + List posts = postRepository.findByInput(recommendWord); + collectPostInfoResDtoList = posts.stream() + .map(PostInfoResDto::from) + .toList(); + postInfoResDtoList.addAll(collectPostInfoResDtoList); + + posts = postRepository.findByRecommend(recommendWord); + collectPostInfoResDtoList = posts.stream() + .map(PostInfoResDto::from) + .toList(); + + postInfoResDtoList.addAll(collectPostInfoResDtoList); + + return PostListResDto.from(postInfoResDtoList); + } // 글 수정 @Transactional - public void postUpdate(Long postId, PostUpdateReqDto postUpdateReqDto, MultipartFile multipartFile, Principal principal) throws IOException { + public void postUpdate(Long postId, PostUpdateReqDto postUpdateReqDto,@RequestPart(required = false) MultipartFile multipartFile, Principal principal) throws IOException { Post post = postRepository.findById(postId).orElseThrow( () -> new IllegalArgumentException("해당 글을 수정할 수 없습니다. postId = " + postId) ); diff --git a/src/main/java/net/skhu/likelion12thteam03be/post/domain/Post.java b/src/main/java/net/skhu/likelion12thteam03be/post/domain/Post.java index d9b1c02..4783ba2 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/post/domain/Post.java +++ b/src/main/java/net/skhu/likelion12thteam03be/post/domain/Post.java @@ -37,8 +37,6 @@ public class Post extends Time { @JoinColumn(name = "categoryId") private Category category; - /*@ManyToOne - @JoinColumn(name = "moodId")*/ @ManyToMany @JoinTable( name = "post_moods", diff --git a/src/main/java/net/skhu/likelion12thteam03be/post/domain/repository/PostRepository.java b/src/main/java/net/skhu/likelion12thteam03be/post/domain/repository/PostRepository.java index 436f446..0c09aaf 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/post/domain/repository/PostRepository.java +++ b/src/main/java/net/skhu/likelion12thteam03be/post/domain/repository/PostRepository.java @@ -6,11 +6,16 @@ import org.springframework.stereotype.Repository; import java.util.List; -import java.util.Optional; @Repository public interface PostRepository extends JpaRepository { - @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 findByMoodId(Long moodId); + + @Query("select distinct p from Post p where p.title like :input or p.content like :input ") + List findByInput(String input); + + @Query("select distinct p from Post p join fetch p.moods m where p.category.name like :recommend or m.name like :recommend ") + List findByRecommend(String recommend); }