Skip to content

Commit

Permalink
feat: 글 추천 조회#3
Browse files Browse the repository at this point in the history
  • Loading branch information
firefox1234123 committed Aug 6, 2024
1 parent 75e639b commit a66dead
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ public ResponseEntity<PostListResDto> postFindByInput(@PathVariable("input") Str
return new ResponseEntity<>(postListResDto, HttpStatus.OK);
}

// 글 추천 조회
@GetMapping("/recommend/{recommend}")
public ResponseEntity<PostListResDto> postFindByRecommend(@PathVariable("recommend") String recommend) {
PostListResDto postListResDto = postService.postFindByRecommend(recommend);
return new ResponseEntity<>(postListResDto, HttpStatus.OK);
}

// 글 수정
@PatchMapping("/{postId}")
public ResponseEntity<String> postUpdate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import java.util.Optional;
import java.util.stream.Collectors;

import static java.util.Collections.addAll;

@Service
@Transactional(readOnly = false)
@RequiredArgsConstructor
Expand All @@ -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));

Expand Down Expand Up @@ -157,9 +160,31 @@ public PostListResDto postFindByInput(String input) {
return PostListResDto.from(postInfoResDtoList);
}

// 글 추천 조회
public PostListResDto postFindByRecommend(String recommend) {
List<PostInfoResDto> postInfoResDtoList = new ArrayList<>();
List<PostInfoResDto> collectPostInfoResDtoList = new ArrayList<>();
String recommendWord = "%" + recommend + "%";

List<Post> 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)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ public class Post extends Time {
@JoinColumn(name = "categoryId")
private Category category;

/*@ManyToOne
@JoinColumn(name = "moodId")*/
@ManyToMany
@JoinTable(
name = "post_moods",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ public interface PostRepository extends JpaRepository<Post, Long> {

@Query("select distinct p from Post p where p.title like :input or p.content like :input ")
List<Post> 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<Post> findByRecommend(String recommend);
}

0 comments on commit a66dead

Please sign in to comment.