Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
Expand All @@ -37,4 +40,12 @@ public RsData<CommentResponseDto> createComment(
return RsData.successOf(commentService.createComment(postId, reqBody)); // code=200, message="success"
}

@GetMapping
@Operation(summary = "댓글 다건 조회")
public RsData<List<CommentResponseDto>> getComments(
@PathVariable Long postId,
@RequestParam(required = false) Long lastId
) {
return RsData.successOf(commentService.getComments(postId, lastId)); // code=200, message="success"
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.back.domain.post.comment.repository;

import com.back.domain.post.comment.entity.Comment;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface CommentRepository extends JpaRepository<Comment, Long> {

List<Comment> findTop10ByPostIdOrderByIdDesc(Long postId);

List<Comment> findTop10ByPostIdAndIdLessThanOrderByIdDesc(Long postId, Long lastId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.back.domain.post.post.repository.PostRepository;
import com.back.domain.user.entity.User;
import com.back.global.rq.Rq;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -36,4 +37,20 @@ public CommentResponseDto createComment(Long postId, CommentCreateRequestDto req

return new CommentResponseDto(commentRepository.save(comment));
}

// 댓글 다건 조회 로직 (무한스크롤)
@Transactional(readOnly = true)
public List<CommentResponseDto> getComments(Long postId, Long lastId) {
if (lastId == null) {
return commentRepository.findTop10ByPostIdOrderByIdDesc(postId)
.stream()
.map(CommentResponseDto::new)
.toList();
} else {
return commentRepository.findTop10ByPostIdAndIdLessThanOrderByIdDesc(postId, lastId)
.stream()
.map(CommentResponseDto::new)
.toList();
}
}
}