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 @Autowired
private CommentsService commentsService; private CommentsService commentsService;
@GetMapping("/{page_slug}") @GetMapping("/{page_slug}")
public List<Comments> getAllCommentsBySlug(@PathVariable String page_slug) { public List<Comments> getAllCommentsBySlug(@PathVariable String page_slug) {
List<Comments> allComments = commentsService.getAllCommentsBySlug(page_slug);
List<Comments> allComments = commentsService; return allComments;
} }
@GetMapping("/comments/item/{id}") @GetMapping("/comments/item/{id}")
public Comments getCommentById(@PathVariable Long id) { public Comments getCommentById(@PathVariable Long id) {
return commentsService.getCommentById(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 { public class CommentsService {
private static final Logger logger = System.getLogger(CommentsService.class.getName()); private static final Logger logger = System.getLogger(CommentsService.class.getName());
private final CommentsRepository commentsRepository; private final CommentsRepository commentsRepository;
public CommentsService(CommentsRepository commentsRepository) { public CommentsService(CommentsRepository commentsRepository) {
this.commentsRepository = commentsRepository; this.commentsRepository = commentsRepository;
} }
public List<Comments> getAllComments() { public List<Comments> getAllComments() {
return commentsRepository.findAll(); return commentsRepository.findAll();
} }
public List<Comments> getAllCommentsBySlug(String page_slug) { public List<Comments> getAllCommentsBySlug(String page_slug) {
logger.log(System.Logger.Level.INFO, "Entering getAllCommentsBySlug with slug: " + 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())) .filter(comment -> page_slug.equals(comment.getPageSlug()))
.toList(); .collect(Collectors.toList());
} return filteredComments;
}
public Comments getCommentById(Long id) { public Comments getCommentById(Long id) {
logger.log(System.Logger.Level.INFO, "Entering getCommentById with id: " + id); logger.log(System.Logger.Level.INFO, "Entering getCommentById with id: " + id);
return commentsRepository.findById(id).get(); return commentsRepository.findById(id).get();
} }
} }