update de la route '/comment/{id}/comments'

This commit is contained in:
Guams 2025-01-22 20:50:16 +01:00
parent 20242ca6c4
commit eb2f3bf151
4 changed files with 27 additions and 5 deletions

View File

@ -53,7 +53,6 @@ public class AuthorController {
@PutMapping("/{id}") @PutMapping("/{id}")
public ReturnableAuthor updateUser(@PathVariable UUID id, @RequestBody Author updatedAuthor, Authentication authentication) { 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); Author authorToUpdate = authorService.verifyIfUserIsAuthorized(authentication, id);
if (passwordEncoder.matches(updatedAuthor.getPassword(), authorToUpdate.getPassword())) { if (passwordEncoder.matches(updatedAuthor.getPassword(), authorToUpdate.getPassword())) {
return authorService.insert(updatedAuthor return authorService.insert(updatedAuthor
@ -66,9 +65,15 @@ public class AuthorController {
} }
@PutMapping(value = "{id}/avatar", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE}) @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); 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}") @DeleteMapping("/{id}")
@ -122,6 +127,6 @@ public class AuthorController {
Author author = authorService.findByName(authentication.getName()).orElseThrow(() -> new NotFoundException("Author not found")); Author author = authorService.findByName(authentication.getName()).orElseThrow(() -> new NotFoundException("Author not found"));
return author.setPassword(""); return author.setPassword("").setProfilePicture(null);
} }
} }

View File

@ -81,6 +81,9 @@ public class CommentController {
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public void deleteComment(@PathVariable Long id, Authentication authentication) { 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")); Comment commentToDelete = commentService.findById(id).orElseThrow(() -> new NotFoundException("Comment not found"));
commentService.delete(commentToDelete); commentService.delete(commentToDelete);
commentService.deleteAssociationByCommentId(commentToDelete.getId()); commentService.deleteAssociationByCommentId(commentToDelete.getId());

View File

@ -6,7 +6,9 @@ import com.guams.review.model.dao.Author;
import com.guams.review.model.dao.Comment; import com.guams.review.model.dao.Comment;
import com.guams.review.model.dao.Post; import com.guams.review.model.dao.Post;
import com.guams.review.service.AuthorService; import com.guams.review.service.AuthorService;
import com.guams.review.service.CommentService;
import com.guams.review.service.PostService; import com.guams.review.service.PostService;
import com.guams.review.service.mapper.Mapper;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
@ -29,6 +31,8 @@ public class PostController {
private final PostService postService; private final PostService postService;
private final AuthorService authorService; private final AuthorService authorService;
private final CommentService commentService;
private final Mapper mapper;
@GetMapping @GetMapping
public List<Post> listPosts() { public List<Post> listPosts() {
@ -97,10 +101,10 @@ public class PostController {
Author authenticatedAuthor = authorService.findByName(authentication.getName()).orElseThrow(() -> new NotFoundException("Author not found")); Author authenticatedAuthor = authorService.findByName(authentication.getName()).orElseThrow(() -> new NotFoundException("Author not found"));
if (authorService.listPublicationOfAuthor(authenticatedAuthor.getId()).stream().map(Post::getId).toList().contains(id)) { if (authorService.listPublicationOfAuthor(authenticatedAuthor.getId()).stream().map(Post::getId).toList().contains(id)) {
Post postToDelete = postService.findById(id).orElseThrow(() -> new NotFoundException("Post not found")); 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()); postService.delete(authenticatedAuthor.getId(), postToDelete.getId());
} else { } else {
throw new UnauthorizedExecption("You do not have permission to delete this post"); throw new UnauthorizedExecption("You do not have permission to delete this post");
} }
} }
} }

View File

@ -1,6 +1,8 @@
package com.guams.review.service.mapper; package com.guams.review.service.mapper;
import com.guams.review.model.dao.Author; 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; import org.springframework.stereotype.Component;
@Component @Component
@ -15,4 +17,12 @@ public class Mapper {
.setRole(author.getRole()) .setRole(author.getRole())
.setProfilePicture(author.getProfilePicture()); .setProfilePicture(author.getProfilePicture());
} }
public Comment mapCommentWithAuthor(CommentWithAuthor comment) {
return new Comment()
.setId(comment.getId())
.setContent(comment.getContent())
.setCommentDate(comment.getCommentDate())
.setIsUpdated(comment.getIsUpdated());
}
} }