From 260b06653d11e0a00f300b6a51d3a50a105ee281 Mon Sep 17 00:00:00 2001 From: guams Date: Wed, 18 Jun 2025 09:56:58 +0200 Subject: [PATCH] fix de bugs --- .../guams/review/configuration/Advice.java | 11 ++++--- .../review/controller/AuthorController.java | 33 +++++++++++++------ .../review/controller/CommentController.java | 24 ++++---------- .../exception/StringTooLongException.java | 7 ++++ 4 files changed, 43 insertions(+), 32 deletions(-) create mode 100644 src/main/java/com/guams/review/exception/StringTooLongException.java diff --git a/src/main/java/com/guams/review/configuration/Advice.java b/src/main/java/com/guams/review/configuration/Advice.java index 4f1c845..aa65684 100644 --- a/src/main/java/com/guams/review/configuration/Advice.java +++ b/src/main/java/com/guams/review/configuration/Advice.java @@ -1,9 +1,6 @@ package com.guams.review.configuration; -import com.guams.review.exception.AlreadyExistsException; -import com.guams.review.exception.InvalidNameOrPasswordException; -import com.guams.review.exception.NotFoundException; -import com.guams.review.exception.UnauthorizedExecption; +import com.guams.review.exception.*; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ControllerAdvice; @@ -37,4 +34,10 @@ public class Advice { return ResponseEntity.status(HttpStatus.UNAUTHORIZED) .body(Map.of("message", exception.getMessage())); } + + @ExceptionHandler(value = StringTooLongException.class) + public ResponseEntity> handleStringTooLong(StringTooLongException exception) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST) + .body(Map.of("message", exception.getMessage())); + } } \ No newline at end of file diff --git a/src/main/java/com/guams/review/controller/AuthorController.java b/src/main/java/com/guams/review/controller/AuthorController.java index f372bdb..65e88e2 100644 --- a/src/main/java/com/guams/review/controller/AuthorController.java +++ b/src/main/java/com/guams/review/controller/AuthorController.java @@ -2,9 +2,9 @@ package com.guams.review.controller; import com.guams.review.configuration.JwtTokenUtil; import com.guams.review.exception.AlreadyExistsException; -import com.guams.review.exception.UnauthorizedExecption; import com.guams.review.exception.InvalidNameOrPasswordException; import com.guams.review.exception.NotFoundException; +import com.guams.review.exception.UnauthorizedExecption; import com.guams.review.model.AuthorRepository; import com.guams.review.model.Role; import com.guams.review.model.dao.Author; @@ -59,25 +59,38 @@ public class AuthorController { return authorService.insert(updatedAuthor .setId(authorToUpdate.getId()) .setRole(authorToUpdate.getRole()) + .setProfilePicture(authorToUpdate.getProfilePicture()) .setPassword(passwordEncoder.encode(updatedAuthor.getPassword()))); } @PutMapping(value = "{id}/avatar", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE}) public Author updateUserAvatar(@PathVariable UUID id, @RequestPart MultipartFile avatar, Authentication authentication) throws IOException { - Author authorToUpdate = authorService.verifyIfUserIsAuthorized(authentication, id); - ReturnableAuthor authorToReturn = authorService.insert(authorToUpdate.setProfilePicture(avatar.getBytes())); - return new Author() - .setId(authorToReturn.getId()) - .setName(authorToReturn.getName()) - .setProfilePicture(null) - .setRole(authorToReturn.getRole()) - .setPassword(""); + if (avatar != null) { + Author authorToUpdate = authorService.verifyIfUserIsAuthorized(authentication, id); + ReturnableAuthor authorToReturn = authorService.insert(authorToUpdate.setProfilePicture(avatar.getBytes())); + return new Author() + .setId(authorToReturn.getId()) + .setName(authorToReturn.getName()) + .setProfilePicture(null) + .setRole(authorToReturn.getRole()) + .setPassword(""); + } else { + return authorService.verifyIfUserIsAuthorized(authentication, id) + .setProfilePicture(null) + .setPassword(""); + + } + } @GetMapping("/{id}/avatar") public byte[] getProfilePicture(@PathVariable UUID id) { Author author = authorService.findById(id).orElseThrow(() -> new NotFoundException("Author not found")); - return Base64.getEncoder().encode(author.getProfilePicture()); + if (author.getProfilePicture() != null) { + return Base64.getEncoder().encode(author.getProfilePicture()); + } else { + return null; + } } @DeleteMapping("/{id}") diff --git a/src/main/java/com/guams/review/controller/CommentController.java b/src/main/java/com/guams/review/controller/CommentController.java index 1c523c4..40a1014 100644 --- a/src/main/java/com/guams/review/controller/CommentController.java +++ b/src/main/java/com/guams/review/controller/CommentController.java @@ -1,4 +1,6 @@ package com.guams.review.controller; + +import com.guams.review.exception.StringTooLongException; import com.guams.review.exception.UnauthorizedExecption; import com.guams.review.exception.NotFoundException; import com.guams.review.model.AuthorRepository; @@ -45,6 +47,10 @@ public class CommentController { throw new UnauthorizedExecption("Vous n'êtes pas autorisé à faire ça"); } + if (comment.getContent().length() >= 512) { + throw new StringTooLongException("Votre commentaire est trop long..."); + } + Author author = authorRepository.findByName(authentication.getName()).orElseThrow(() -> new NotFoundException("Cet auteur n'existe pas")); Comment insertedComment = commentService.insert(comment .setCommentDate(Timestamp.from(Instant.now())) @@ -61,24 +67,6 @@ public class CommentController { return commentService.getCommentsByPostId(postId); } - @PutMapping("/{id}") - public void updateComment(@PathVariable Long id, @RequestBody CommentIds commentIds, Authentication authentication) { - if (authentication == null || !authentication.isAuthenticated()) { - throw new UnauthorizedExecption("Vous n'êtes pas autorisé à faire ceci"); - } - - Author author = authorService.findByName(authentication.getName()).orElseThrow(() -> new NotFoundException("Cet auteur n'existe pas")); - Comment commentToUpdate = commentService.findById(id).orElseThrow(() -> new NotFoundException("Ce commentaire n'existe pas")); - CommentIds concernedCommentIds = commentService.getCommentIdsByCommentId(id).orElseThrow(() -> new NotFoundException("Ce commentaire n'existe pas")); - if (!author.getId().equals(concernedCommentIds.getAuthorId())) { - throw new UnauthorizedExecption("Vous n'êtes pas autorisé à faire ceci"); - } - - commentService.insert(commentToUpdate - .setIsUpdated(true) - .setContent(commentIds.getContent())); - } - @DeleteMapping("/{id}") public void deleteComment(@PathVariable Long id, Authentication authentication) { if (authentication == null || !authentication.isAuthenticated()) { diff --git a/src/main/java/com/guams/review/exception/StringTooLongException.java b/src/main/java/com/guams/review/exception/StringTooLongException.java new file mode 100644 index 0000000..814f5cf --- /dev/null +++ b/src/main/java/com/guams/review/exception/StringTooLongException.java @@ -0,0 +1,7 @@ +package com.guams.review.exception; + +public class StringTooLongException extends RuntimeException { + public StringTooLongException(String message) { + super(message); + } +}