package com.guams.review.controller; import com.guams.review.exception.ForbiddenExecption; import com.guams.review.exception.NotFoundException; import com.guams.review.model.AuthorRepository; import com.guams.review.model.dao.Author; import com.guams.review.model.dao.Comment; import com.guams.review.model.dao.CommentIds; import com.guams.review.service.AuthorService; import com.guams.review.service.CommentService; import com.guams.review.service.PostService; import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.core.Authentication; import org.springframework.web.bind.annotation.*; import java.sql.Timestamp; import java.time.Instant; import java.util.List; @RestController @RequestMapping(path = "api/comments") @RequiredArgsConstructor public class CommentController { private final CommentService commentService; private final AuthorRepository authorRepository; private final PostService postService; private final AuthorService authorService; @GetMapping public List listAllComments() { return commentService.list(); } @PostMapping("/posts/{id}") public ResponseEntity addComment(@RequestBody Comment comment, Authentication authentication, @PathVariable("id") Long postId) { if (authentication == null || !authentication.isAuthenticated()) { throw new ForbiddenExecption("You are not authorized to access this resource"); } Author author = authorRepository.findByName(authentication.getName()).orElseThrow(() -> new NotFoundException("Author not found")); Comment insertedComment = commentService.insert(comment .setCommentDate(Timestamp.from(Instant.now())) .setIsUpdated(false)); postService.findById(postId).orElseThrow(() -> new NotFoundException("Post not found")); commentService.associateCommentToPostAndAuthor(author.getId(), postId, insertedComment.getId()); return new ResponseEntity<>(insertedComment, HttpStatus.CREATED); } @GetMapping("/posts/{id}") public List listCommentsByPostId(@PathVariable("id") Long postId) { postService.findById(postId).orElseThrow(() -> new NotFoundException("Post not found")); return commentService.getCommentsByCommentId(postId); } @PutMapping("/{id}") public void updateComment(@PathVariable Long id, @RequestBody CommentIds commentIds, Authentication authentication) { if (authentication == null || !authentication.isAuthenticated()) { throw new ForbiddenExecption("You are not authorized to access this resource"); } Author author = authorService.findByName(authentication.getName()).orElseThrow(() -> new NotFoundException("Author not found")); Comment commentToUpdate = commentService.findById(id).orElseThrow(() -> new NotFoundException("Comment not found")); CommentIds concernedCommentIds = commentService.getCommentIdsByCommentId(id).orElseThrow(() -> new NotFoundException("Comment not found")); if (!author.getId().equals(concernedCommentIds.getAuthorId())) { throw new ForbiddenExecption("You are not authorized to access this resource"); } commentService.insert(commentToUpdate .setIsUpdated(true) .setContent(commentIds.getContent())); } // @DeleteMapping("/{id}") // public void deleteComment(@PathVariable Long id, Authentication authentication) {} }