Skip to content

Commit

Permalink
add count controller
Browse files Browse the repository at this point in the history
  • Loading branch information
giangle-mfv committed Jun 27, 2024
1 parent 12f2487 commit 5053f1d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public List<CommentDto> getCommentsByPostId(@PathVariable(value = "postId") Long
return commentService.getCommentsByPostId(postId);
}

@GetMapping("/posts/{postId}/comments/count")
public int countCommentsByPostId(@PathVariable(value = "postId") Long postId){
return commentService.countCommentsByPostId(postId);
}

@GetMapping("/posts/{postId}/comments/{id}")
public ResponseEntity<CommentDto> getCommentById(@PathVariable(value = "postId") Long postId,
@PathVariable(value = "id") Long commentId){
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/springboot/blog/service/CommentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public interface CommentService {

List<CommentDto> getCommentsByPostId(long postId);

int countCommentsByPostId(long postId);

CommentDto getCommentById(Long postId, Long commentId);

CommentDto updateComment(Long postId, long commentId, CommentDto commentRequest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public List<CommentDto> getCommentsByPostId(long postId) {
return comments.stream().map(comment -> mapToDTO(comment)).collect(Collectors.toList());
}

@Override
public int countCommentsByPostId(long postId) {
return commentRepository.findByPostId(postId).size();
}

@Override
public CommentDto getCommentById(Long postId, Long commentId) {
// retrieve post entity by id
Expand Down

0 comments on commit 5053f1d

Please sign in to comment.