From eb2f3bf1516988d25adbf701d9b6aaf9bd4a91d9 Mon Sep 17 00:00:00 2001 From: Guams Date: Wed, 22 Jan 2025 20:50:16 +0100 Subject: [PATCH] update de la route '/comment/{id}/comments' --- .../guams/review/controller/AuthorController.java | 13 +++++++++---- .../guams/review/controller/CommentController.java | 3 +++ .../com/guams/review/controller/PostController.java | 6 +++++- .../com/guams/review/service/mapper/Mapper.java | 10 ++++++++++ 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/guams/review/controller/AuthorController.java b/src/main/java/com/guams/review/controller/AuthorController.java index 42d21ab..192e1b5 100644 --- a/src/main/java/com/guams/review/controller/AuthorController.java +++ b/src/main/java/com/guams/review/controller/AuthorController.java @@ -53,7 +53,6 @@ public class AuthorController { @PutMapping("/{id}") public ReturnableAuthor updateUser(@PathVariable UUID id, @RequestBody Author updatedAuthor, Authentication authentication) { - System.out.println(updatedAuthor.getName() + " " + updatedAuthor.getPassword() + " " + updatedAuthor.getId()); Author authorToUpdate = authorService.verifyIfUserIsAuthorized(authentication, id); if (passwordEncoder.matches(updatedAuthor.getPassword(), authorToUpdate.getPassword())) { return authorService.insert(updatedAuthor @@ -66,9 +65,15 @@ public class AuthorController { } @PutMapping(value = "{id}/avatar", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE}) - public void updateUserAvatar(@PathVariable UUID id, @RequestPart MultipartFile avatar, Authentication authentication) throws IOException { + public Author updateUserAvatar(@PathVariable UUID id, @RequestPart MultipartFile avatar, Authentication authentication) throws IOException { Author authorToUpdate = authorService.verifyIfUserIsAuthorized(authentication, id); - authorService.insert(authorToUpdate.setProfilePicture(avatar.getBytes())); + ReturnableAuthor authorToReturn = authorService.insert(authorToUpdate.setProfilePicture(avatar.getBytes())); + return new Author() + .setId(authorToReturn.getId()) + .setName(authorToReturn.getName()) + .setProfilePicture(null) + .setRole(authorToReturn.getRole()) + .setPassword(""); } @DeleteMapping("/{id}") @@ -122,6 +127,6 @@ public class AuthorController { Author author = authorService.findByName(authentication.getName()).orElseThrow(() -> new NotFoundException("Author not found")); - return author.setPassword(""); + return author.setPassword("").setProfilePicture(null); } } diff --git a/src/main/java/com/guams/review/controller/CommentController.java b/src/main/java/com/guams/review/controller/CommentController.java index f2923bf..8e38333 100644 --- a/src/main/java/com/guams/review/controller/CommentController.java +++ b/src/main/java/com/guams/review/controller/CommentController.java @@ -81,6 +81,9 @@ public class CommentController { @DeleteMapping("/{id}") public void deleteComment(@PathVariable Long id, Authentication authentication) { + if (authentication == null || !authentication.isAuthenticated()) { + throw new UnauthorizedExecption("You are not authorized to access this resource"); + } Comment commentToDelete = commentService.findById(id).orElseThrow(() -> new NotFoundException("Comment not found")); commentService.delete(commentToDelete); commentService.deleteAssociationByCommentId(commentToDelete.getId()); diff --git a/src/main/java/com/guams/review/controller/PostController.java b/src/main/java/com/guams/review/controller/PostController.java index a95cbfb..2b22451 100644 --- a/src/main/java/com/guams/review/controller/PostController.java +++ b/src/main/java/com/guams/review/controller/PostController.java @@ -6,7 +6,9 @@ import com.guams.review.model.dao.Author; import com.guams.review.model.dao.Comment; import com.guams.review.model.dao.Post; import com.guams.review.service.AuthorService; +import com.guams.review.service.CommentService; import com.guams.review.service.PostService; +import com.guams.review.service.mapper.Mapper; import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; @@ -29,6 +31,8 @@ public class PostController { private final PostService postService; private final AuthorService authorService; + private final CommentService commentService; + private final Mapper mapper; @GetMapping public List listPosts() { @@ -97,10 +101,10 @@ public class PostController { Author authenticatedAuthor = authorService.findByName(authentication.getName()).orElseThrow(() -> new NotFoundException("Author not found")); if (authorService.listPublicationOfAuthor(authenticatedAuthor.getId()).stream().map(Post::getId).toList().contains(id)) { Post postToDelete = postService.findById(id).orElseThrow(() -> new NotFoundException("Post not found")); + commentService.getCommentsByPostId(id).stream().map(mapper::mapCommentWithAuthor).forEach(commentService::delete); postService.delete(authenticatedAuthor.getId(), postToDelete.getId()); } else { throw new UnauthorizedExecption("You do not have permission to delete this post"); } } - } diff --git a/src/main/java/com/guams/review/service/mapper/Mapper.java b/src/main/java/com/guams/review/service/mapper/Mapper.java index e467766..d0af692 100644 --- a/src/main/java/com/guams/review/service/mapper/Mapper.java +++ b/src/main/java/com/guams/review/service/mapper/Mapper.java @@ -1,6 +1,8 @@ package com.guams.review.service.mapper; import com.guams.review.model.dao.Author; +import com.guams.review.model.dao.Comment; +import com.guams.review.model.dao.CommentWithAuthor; import org.springframework.stereotype.Component; @Component @@ -15,4 +17,12 @@ public class Mapper { .setRole(author.getRole()) .setProfilePicture(author.getProfilePicture()); } + + public Comment mapCommentWithAuthor(CommentWithAuthor comment) { + return new Comment() + .setId(comment.getId()) + .setContent(comment.getContent()) + .setCommentDate(comment.getCommentDate()) + .setIsUpdated(comment.getIsUpdated()); + } }