From ef4e87191cf7d4df6d95c3373b3acb774a0a8552 Mon Sep 17 00:00:00 2001 From: firefox1234123 Date: Mon, 5 Aug 2024 20:11:02 +0900 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20=EA=B8=80=20=EA=B2=80=EC=83=89=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=20=EA=B5=AC=ED=98=84#3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../post/api/dto/PostController.java | 6 +++++- .../post/application/PostService.java | 11 +++++++++++ .../post/domain/repository/PostRepository.java | 6 ++++-- 3 files changed, 20 insertions(+), 3 deletions(-) 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 a2bfa10..c6e367f 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 @@ -78,7 +78,11 @@ 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); + } // 글 수정 @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 5ef41eb..f5cce66 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/post/application/PostService.java +++ b/src/main/java/net/skhu/likelion12thteam03be/post/application/PostService.java @@ -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; @@ -145,6 +146,16 @@ 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); + } // 글 수정 @Transactional 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..5fc93e0 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,13 @@ 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); } From 75e639b37e45ed30c49cba2978fee84a7570834e Mon Sep 17 00:00:00 2001 From: firefox1234123 Date: Mon, 5 Aug 2024 20:11:19 +0900 Subject: [PATCH 2/3] =?UTF-8?q?feat:=20=EA=B8=80=20=EA=B2=80=EC=83=89=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=20=EA=B5=AC=ED=98=84#3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../post/domain/repository/PostRepository.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 5fc93e0..4106c52 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 @@ -13,6 +13,6 @@ public interface PostRepository extends JpaRepository { @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") + @Query("select distinct p from Post p where p.title like :input or p.content like :input ") List findByInput(String input); } From a66deada1d08028a1820a261d740bad6449999e1 Mon Sep 17 00:00:00 2001 From: firefox1234123 Date: Tue, 6 Aug 2024 18:28:46 +0900 Subject: [PATCH 3/3] =?UTF-8?q?feat:=20=EA=B8=80=20=EC=B6=94=EC=B2=9C=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C#3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../post/api/dto/PostController.java | 7 +++++ .../post/application/PostService.java | 29 +++++++++++++++++-- .../post/domain/Post.java | 2 -- .../domain/repository/PostRepository.java | 3 ++ 4 files changed, 37 insertions(+), 4 deletions(-) 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 c6e367f..10d540e 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 @@ -84,6 +84,13 @@ public ResponseEntity postFindByInput(@PathVariable("input") Str 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}") public ResponseEntity postUpdate( 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 f5cce66..4007b6b 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/post/application/PostService.java +++ b/src/main/java/net/skhu/likelion12thteam03be/post/application/PostService.java @@ -29,6 +29,8 @@ import java.util.Optional; import java.util.stream.Collectors; +import static java.util.Collections.addAll; + @Service @Transactional(readOnly = false) @RequiredArgsConstructor @@ -42,9 +44,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)); @@ -157,9 +160,31 @@ public PostListResDto postFindByInput(String input) { 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 4106c52..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 @@ -15,4 +15,7 @@ public interface PostRepository extends JpaRepository { @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); }