From 3db37f3c2412d39aa9ace82011b51310eb9986f7 Mon Sep 17 00:00:00 2001 From: gimchaewon Date: Mon, 4 Aug 2025 11:06:31 +0900 Subject: [PATCH] =?UTF-8?q?refactor=20:=20=EC=A2=8B=EC=95=84=EC=9A=94=20?= =?UTF-8?q?=EB=B6=81=EB=A7=88=ED=81=AC=20=EB=A1=9C=EC=A7=81=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../goodNews/controller/PostController.java | 20 ++++++++++++------- .../domain/goodNews/dto/GoodnewsDto.java | 2 ++ .../repository/PostLikeRepository.java | 5 +++++ .../goodNews/service/CommentService.java | 1 - .../domain/goodNews/service/PostService.java | 15 ++++++++++++-- .../place/controller/PlaceController.java | 7 +++++-- .../domain/place/service/PlaceService.java | 18 ++++++++++++----- 7 files changed, 51 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/draconist/goodluckynews/domain/goodNews/controller/PostController.java b/src/main/java/com/draconist/goodluckynews/domain/goodNews/controller/PostController.java index 0c628eb..89690b8 100644 --- a/src/main/java/com/draconist/goodluckynews/domain/goodNews/controller/PostController.java +++ b/src/main/java/com/draconist/goodluckynews/domain/goodNews/controller/PostController.java @@ -28,17 +28,23 @@ public ResponseEntity createPost( }//희소식 생성 @GetMapping("/{postId}") - public ResponseEntity getPostById(@PathVariable Long postId) { - return postService.getPostById(postId); - }//희소식 상세 조회 + public ResponseEntity getPostById( + @PathVariable Long postId, + @AuthenticationPrincipal String email + ) { + return postService.getPostById(postId, email); + } +//희소식 상세 조회 @GetMapping public ResponseEntity getAllPosts( - @RequestParam(defaultValue = "0") int page, // 기본값 0 - @RequestParam(defaultValue = "10") int size // 기본값 10 + @RequestParam(defaultValue = "0") int page, + @RequestParam(defaultValue = "10") int size, + @AuthenticationPrincipal String email // 로그인 사용자 email 주입 ) { - return postService.getAllPosts(page, size); - }//희소식 전체 조회 (페이지네이션) + return postService.getAllPosts(page, size, email); + } +//희소식 전체 조회 (페이지네이션) @PostMapping("/{postId}/like") public ResponseEntity togglePostLike( diff --git a/src/main/java/com/draconist/goodluckynews/domain/goodNews/dto/GoodnewsDto.java b/src/main/java/com/draconist/goodluckynews/domain/goodNews/dto/GoodnewsDto.java index 5150319..1586b9b 100644 --- a/src/main/java/com/draconist/goodluckynews/domain/goodNews/dto/GoodnewsDto.java +++ b/src/main/java/com/draconist/goodluckynews/domain/goodNews/dto/GoodnewsDto.java @@ -34,6 +34,7 @@ public static class GoodnewsResponseDto { private LocalDateTime createdAt; private LocalDateTime updatedAt; private String placeName; + private WriterInfoDto writer; //작성자 정보 추가 public static GoodnewsResponseDto from(Post post, WriterInfoDto writer) { @@ -69,6 +70,7 @@ public static class PostDto { private int likeCount; // 좋아요 개수 추가 private int commentCount; // 댓글 개수 추가 private WriterInfoDto writer;// 작성자 정보 필드 추가 + private boolean liked; // 좋아요 여부 }//값 반환 dto diff --git a/src/main/java/com/draconist/goodluckynews/domain/goodNews/repository/PostLikeRepository.java b/src/main/java/com/draconist/goodluckynews/domain/goodNews/repository/PostLikeRepository.java index f538201..d72b5e9 100644 --- a/src/main/java/com/draconist/goodluckynews/domain/goodNews/repository/PostLikeRepository.java +++ b/src/main/java/com/draconist/goodluckynews/domain/goodNews/repository/PostLikeRepository.java @@ -6,7 +6,12 @@ import java.util.Optional; public interface PostLikeRepository extends JpaRepository { + Optional findByUserIdAndPostId(Long userId, Long postId); + + boolean existsByUserIdAndPostId(Long userId, Long postId); // ✅ 추가 + int countByPostId(Long postId); // 특정 게시글의 좋아요 개수 조회 + void deleteByPostId(Long postId); } diff --git a/src/main/java/com/draconist/goodluckynews/domain/goodNews/service/CommentService.java b/src/main/java/com/draconist/goodluckynews/domain/goodNews/service/CommentService.java index 1fa48d9..0a1e506 100644 --- a/src/main/java/com/draconist/goodluckynews/domain/goodNews/service/CommentService.java +++ b/src/main/java/com/draconist/goodluckynews/domain/goodNews/service/CommentService.java @@ -128,7 +128,6 @@ private CommentDto.CommentResultDto toCommentResultDtoWithReplies(Comment commen } - //사용자의 댓글 조회 public ResponseEntity getMyComments(String email) { Member member = memberRepository.findMemberByEmail(email) diff --git a/src/main/java/com/draconist/goodluckynews/domain/goodNews/service/PostService.java b/src/main/java/com/draconist/goodluckynews/domain/goodNews/service/PostService.java index 142f50d..d59ff96 100644 --- a/src/main/java/com/draconist/goodluckynews/domain/goodNews/service/PostService.java +++ b/src/main/java/com/draconist/goodluckynews/domain/goodNews/service/PostService.java @@ -88,11 +88,16 @@ public ResponseEntity createPost(GoodnewsDto.GoodnewsCreateDto goodnewsCreate } }//게시글 생성 - public ResponseEntity getPostById(Long postId) { + public ResponseEntity getPostById(Long postId, String email) { + Member member = memberRepository.findMemberByEmail(email) + .orElseThrow(() -> new GeneralException(ErrorStatus.MEMBER_NOT_FOUND)); + // 1. 게시글 조회 (없으면 예외 발생) Post post = postRepository.findById(postId) .orElseThrow(() -> new RuntimeException(ErrorStatus.POST_NOT_FOUND.getMessage())); + boolean liked = postLikeRepository.findByUserIdAndPostId(member.getId(), postId).isPresent(); + // 2. 조회된 게시글을 DTO로 변환 GoodnewsDto.PostDto postDto = GoodnewsDto.PostDto.builder() .postId(post.getId()) @@ -106,6 +111,7 @@ public ResponseEntity getPostById(Long postId) { .likeCount(postLikeRepository.countByPostId(post.getId())) .commentCount(commentRepository.countByPostId(post.getId())) .writer(mapToWriterDto(post.getUserId())) //작성자 추가 + .liked(liked) .build(); // 3. ApiResponse로 감싸서 반환 (POST_DETAIL_SUCCESS 사용) @@ -117,7 +123,11 @@ public ResponseEntity getPostById(Long postId) { } - public ResponseEntity getAllPosts(int page, int size) { + public ResponseEntity getAllPosts(int page, int size, String email) { + // 1. 사용자 정보 조회 + Member user = memberRepository.findMemberByEmail(email) + .orElseThrow(() -> new GeneralException(ErrorStatus.MEMBER_NOT_FOUND)); + // 1. 페이지네이션 객체 생성 Pageable pageable = PageRequest.of(page, size); @@ -137,6 +147,7 @@ public ResponseEntity getAllPosts(int page, int size) { .updatedAt(post.getUpdatedAt()) .likeCount(postLikeRepository.countByPostId(post.getId())) .commentCount(commentRepository.countByPostId(post.getId())) + .liked(postLikeRepository.findByUserIdAndPostId(user.getId(), post.getId()).isPresent()) .writer(mapToWriterDto(post.getUserId()))//작성자 추가 .build()) .collect(Collectors.toList()); diff --git a/src/main/java/com/draconist/goodluckynews/domain/place/controller/PlaceController.java b/src/main/java/com/draconist/goodluckynews/domain/place/controller/PlaceController.java index 1206f58..d4fac95 100644 --- a/src/main/java/com/draconist/goodluckynews/domain/place/controller/PlaceController.java +++ b/src/main/java/com/draconist/goodluckynews/domain/place/controller/PlaceController.java @@ -43,8 +43,11 @@ public ResponseEntity getAllPlaces( @GetMapping("/{placeId}") - public ResponseEntity getPlaceById(@PathVariable Long placeId) { - return placeService.getPlaceById(placeId); + public ResponseEntity getPlaceDetail( + @PathVariable Long id, + @AuthenticationPrincipal String email // JWT 인증 객체 등으로부터 유저 식별 + ) { + return placeService.getPlaceById(id, email); }//특정 플레이스 상세 조회 @PatchMapping("/{placeId}") diff --git a/src/main/java/com/draconist/goodluckynews/domain/place/service/PlaceService.java b/src/main/java/com/draconist/goodluckynews/domain/place/service/PlaceService.java index d17c7d2..58391cf 100644 --- a/src/main/java/com/draconist/goodluckynews/domain/place/service/PlaceService.java +++ b/src/main/java/com/draconist/goodluckynews/domain/place/service/PlaceService.java @@ -147,27 +147,35 @@ public ResponseEntity findAllWithPagination(int page, int size, String email) } //플레이스 전체 조회 ( 페이지네이션 ) - public ResponseEntity getPlaceById(Long placeId) { + public ResponseEntity getPlaceById(Long placeId, String email) { // 0. placeId 유효성 검사 if (placeId == null || placeId <= 0) { throw new GeneralException(ErrorStatus.INVALID_PLACE_ID); } + // 1. 회원 조회 + Member member = memberRepository.findMemberByEmail(email) + .orElseThrow(() -> new GeneralException(ErrorStatus.MEMBER_NOT_FOUND)); // 1. placeId로 Place 조회 (없으면 예외 발생) Place place = placeRepository.findById(placeId) .orElseThrow(() -> new GeneralException(ErrorStatus.PLACE_NOT_FOUND)); - // 2. DTO로 변환 + // 2. 좋아요 수 및 여부 확인 + int likeCount = placeLikeRepository.countByPlaceId(place.getId()); + boolean isBookmarked = placeLikeRepository.existsByPlaceIdAndUserId(place.getId(), member.getId()); + + // 3. DTO로 변환 PlaceDTO placeDTO = PlaceDTO.builder() - .placeId(place.getId()) // 🔹 placeId 추가 + .placeId(place.getId()) // placeId 추가 .placeName(place.getPlaceName()) .placeDetails(place.getPlaceDetails()) .placeImg(place.getPlaceImg()) + .likeCount(likeCount) + .isBookmark(isBookmarked) .build(); - - // 3. 성공 응답 반환 + // 4. 성공 응답 반환 return ResponseEntity.ok(ApiResponse.onSuccess( SuccessStatus._PLACE_DETAIL_SUCCESS.getMessage(), placeDTO