3 endpoints complete for comments -> get all comments, get by id, get by slug

This commit is contained in:
2026-01-03 22:13:13 +00:00
parent 95286ae6dd
commit 1358b8981c
2 changed files with 33 additions and 27 deletions

View File

@@ -18,17 +18,21 @@ public class CommentsController {
@Autowired
private CommentsService commentsService;
@GetMapping("/{page_slug}")
public List<Comments> getAllCommentsBySlug(@PathVariable String page_slug) {
List<Comments> allComments = commentsService;
@GetMapping("/{page_slug}")
public List<Comments> getAllCommentsBySlug(@PathVariable String page_slug) {
List<Comments> allComments = commentsService.getAllCommentsBySlug(page_slug);
return allComments;
}
@GetMapping("/comments/item/{id}")
public Comments getCommentById(@PathVariable Long id) {
@GetMapping("/comments/item/{id}")
public Comments getCommentById(@PathVariable Long id) {
return commentsService.getCommentById(id);
}
}
@GetMapping("/comments/items")
public List<Comments> getAllComments() {
return commentsService.getAllComments();
}
}

View File

@@ -11,26 +11,28 @@ import java.util.stream.*;
public class CommentsService {
private static final Logger logger = System.getLogger(CommentsService.class.getName());
private final CommentsRepository commentsRepository;
private final CommentsRepository commentsRepository;
public CommentsService(CommentsRepository commentsRepository) {
this.commentsRepository = commentsRepository;
}
public CommentsService(CommentsRepository commentsRepository) {
this.commentsRepository = commentsRepository;
}
public List<Comments> getAllComments() {
return commentsRepository.findAll();
}
public List<Comments> getAllComments() {
return commentsRepository.findAll();
}
public List<Comments> getAllCommentsBySlug(String page_slug) {
logger.log(System.Logger.Level.INFO, "Entering getAllCommentsBySlug with slug: " + page_slug);
return commentsRepository.findAll().stream()
List<Comments> allComments = commentsRepository.findAll();
List<Comments> filteredComments = allComments.stream()
.filter(comment -> page_slug.equals(comment.getPageSlug()))
.toList();
}
.collect(Collectors.toList());
return filteredComments;
}
public Comments getCommentById(Long id) {
logger.log(System.Logger.Level.INFO, "Entering getCommentById with id: " + id);
return commentsRepository.findById(id).get();
}
public Comments getCommentById(Long id) {
logger.log(System.Logger.Level.INFO, "Entering getCommentById with id: " + id);
return commentsRepository.findById(id).get();
}
}