Skip to content

Commit

Permalink
Merge pull request #62 from Kusitms-29th-Kobaco-A/chore/comment1
Browse files Browse the repository at this point in the history
fix: 댓글 조회
  • Loading branch information
jihyo-j authored Mar 7, 2024
2 parents c09279c + 3d0c29c commit 05c188b
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/comments")
Expand All @@ -29,9 +29,9 @@ public ResponseEntity<CommentCreateRequest> createComment(@RequestBody CommentCr

@Operation(summary = "댓글 조회")
@GetMapping("/{advertiseId}")
public ResponseEntity<List<CommentDetailResponse>> getAllComments(@PathVariable Long advertiseId) {
List<CommentDetailResponse> comments = commentService.getAllComments(advertiseId);
return ApiResponse.success(comments);
public ResponseEntity<Page<CommentDetailResponse>> getAllComments(@PathVariable Long advertiseId, Pageable pageable) {
Page<CommentDetailResponse> commentsPage = commentService.getAllComments(advertiseId, pageable);
return ApiResponse.success(commentsPage);
}

@Operation(summary = "댓글 좋아요")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@

import core.kobaco.domain.user.service.UserReader;
import jakarta.transaction.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;

import java.util.*;
import java.util.stream.Collectors;


@Service
@Transactional
Expand Down Expand Up @@ -43,15 +42,10 @@ public CommentCreateRequest createComment(String content, Long advertiseId) {
return CommentCreateRequest.of(savedCommentEntity.getContent());
}

public List<CommentDetailResponse> getAllComments(Long advertiseId) {
List<Comment> comments = commentRepository.findAllByAdvertiseId(advertiseId);
return comments.stream()
.map(comment -> {
String userEmail = getUserEmail(comment.getCommenterId());
return CommentDetailResponse.of(comment.getCommentId(), comment.getContent(), userEmail);
})
.collect(Collectors.toList());
}
public Page<CommentDetailResponse> getAllComments(Long advertiseId, Pageable pageable) {
Page<Comment> commentsPage = commentRepository.findAllByAdvertiseId(advertiseId, pageable);
return commentsPage.map(comment -> CommentDetailResponse.of(comment.getCommentId(), comment.getContent(), getUserEmail(comment.getCommenterId())));
}

private String getUserEmail(Long userId) {
return userReader.read(userId).getEmail();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package core.kobaco.domain.comment;

import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.Optional;

public interface CommentRepository {
Optional<Comment> findById(Long commentId);
Comment save(Comment comment, Long advertiseId);
List<Comment> findAllByAdvertiseId(Long advertiseId);
Page<Comment> findAllByAdvertiseId(Long advertiseId, Pageable pageable);

}

Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package core.kobaco.infra.comment;

import core.kobaco.infra.jpa.comment.entity.CommentEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface CommentJpaRepository extends JpaRepository<CommentEntity,Long> {
List<CommentEntity> findAllByAdvertiseId(Long advertiseId);
public interface CommentJpaRepository extends JpaRepository<CommentEntity, Long> {
Page<CommentEntity> findAllByAdvertiseId(Long advertiseId, Pageable pageable);
}


Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import core.kobaco.infra.comment.CommentJpaRepository;
import core.kobaco.infra.jpa.comment.entity.CommentEntity;
import org.springframework.context.annotation.Primary;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
Expand Down Expand Up @@ -36,9 +37,9 @@ public Comment save(Comment comment, Long advertiseId) {
}

@Override
public List<Comment> findAllByAdvertiseId(Long advertiseId) {
List<CommentEntity> commentEntities = commentJpaRepository.findAllByAdvertiseId(advertiseId);
return commentMapper.toDomainList(commentEntities);
public Page<Comment> findAllByAdvertiseId(Long advertiseId, Pageable pageable) {
Page<CommentEntity> commentEntitiesPage = commentJpaRepository.findAllByAdvertiseId(advertiseId, pageable);
return commentEntitiesPage.map(commentMapper::toDomain);
}

}
Expand Down

0 comments on commit 05c188b

Please sign in to comment.