Compare commits

..

No commits in common. "48a92a469035b0ecf75cfaa7807b9f117aeb47cc" and "8a2ac5be696af31cb8f0414aeef761798a3a0391" have entirely different histories.

5 changed files with 32 additions and 45 deletions

View File

@ -1,6 +1,9 @@
package com.guams.review.configuration;
import com.guams.review.exception.*;
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 org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
@ -34,10 +37,4 @@ 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

@ -36,7 +36,6 @@ public class SpringSecurityConfig {
.requestMatchers(HttpMethod.GET,
"/api/authors",
"/api/authors/{id}",
"/api/authors/{id}/avatar",
"/api/authors/{id}/posts",
"/api/posts",
"/api/posts/{id}",

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;
@ -26,7 +26,6 @@ import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.Base64;
import java.util.List;
import java.util.UUID;
@ -59,38 +58,25 @@ 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 {
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("");
}
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("");
}
@GetMapping("/{id}/avatar")
public byte[] getProfilePicture(@PathVariable UUID id) {
Author author = authorService.findById(id).orElseThrow(() -> new NotFoundException("Author not found"));
if (author.getProfilePicture() != null) {
return Base64.getEncoder().encode(author.getProfilePicture());
} else {
return null;
}
return author.getProfilePicture();
}
@DeleteMapping("/{id}")

View File

@ -1,6 +1,4 @@
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;
@ -47,10 +45,6 @@ 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()))
@ -67,6 +61,24 @@ 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

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