Skip to content

Commit

Permalink
feat: post 검색, 추천 조회 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
firefox1234123 committed Aug 6, 2024
2 parents 0559d6f + a66dead commit f4b13c1
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,18 @@ 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);
}

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

// 글 수정
@PatchMapping("/{postId}")
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -29,7 +28,7 @@
import java.util.Optional;
import java.util.stream.Collectors;

@Slf4j

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

Expand Down Expand Up @@ -147,10 +147,42 @@ 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);
}

// 글 추천 조회
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 @@ -6,11 +6,16 @@
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);

@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 f4b13c1

Please sign in to comment.