dev #1

Merged
Guams merged 2 commits from dev into master 2025-06-18 09:58:15 +02:00
4 changed files with 43 additions and 32 deletions
Showing only changes of commit 260b06653d - Show all commits

View File

@ -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<Map<String, String>> handleStringTooLong(StringTooLongException exception) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(Map.of("message", exception.getMessage()));
}
}

View File

@ -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}")

View File

@ -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()) {

View File

@ -0,0 +1,7 @@
package com.guams.review.exception;
public class StringTooLongException extends RuntimeException {
public StringTooLongException(String message) {
super(message);
}
}