Compare commits
	
		
			3 Commits
		
	
	
		
			8a2ac5be69
			...
			48a92a4690
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 48a92a4690 | |||
| 260b06653d | |||
| 4a06b45020 | 
| @ -1,9 +1,6 @@ | |||||||
| package com.guams.review.configuration; | package com.guams.review.configuration; | ||||||
| 
 | 
 | ||||||
| import com.guams.review.exception.AlreadyExistsException; | import com.guams.review.exception.*; | ||||||
| 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.HttpStatus; | ||||||
| import org.springframework.http.ResponseEntity; | import org.springframework.http.ResponseEntity; | ||||||
| import org.springframework.web.bind.annotation.ControllerAdvice; | import org.springframework.web.bind.annotation.ControllerAdvice; | ||||||
| @ -37,4 +34,10 @@ public class Advice { | |||||||
|         return ResponseEntity.status(HttpStatus.UNAUTHORIZED) |         return ResponseEntity.status(HttpStatus.UNAUTHORIZED) | ||||||
|                 .body(Map.of("message", exception.getMessage())); |                 .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())); | ||||||
|  |     } | ||||||
| } | } | ||||||
| @ -36,6 +36,7 @@ public class SpringSecurityConfig { | |||||||
|                         .requestMatchers(HttpMethod.GET, |                         .requestMatchers(HttpMethod.GET, | ||||||
|                                 "/api/authors", |                                 "/api/authors", | ||||||
|                                 "/api/authors/{id}", |                                 "/api/authors/{id}", | ||||||
|  |                                 "/api/authors/{id}/avatar", | ||||||
|                                 "/api/authors/{id}/posts", |                                 "/api/authors/{id}/posts", | ||||||
|                                 "/api/posts", |                                 "/api/posts", | ||||||
|                                 "/api/posts/{id}", |                                 "/api/posts/{id}", | ||||||
|  | |||||||
| @ -2,9 +2,9 @@ package com.guams.review.controller; | |||||||
| 
 | 
 | ||||||
| import com.guams.review.configuration.JwtTokenUtil; | import com.guams.review.configuration.JwtTokenUtil; | ||||||
| import com.guams.review.exception.AlreadyExistsException; | import com.guams.review.exception.AlreadyExistsException; | ||||||
| import com.guams.review.exception.UnauthorizedExecption; |  | ||||||
| import com.guams.review.exception.InvalidNameOrPasswordException; | import com.guams.review.exception.InvalidNameOrPasswordException; | ||||||
| import com.guams.review.exception.NotFoundException; | import com.guams.review.exception.NotFoundException; | ||||||
|  | import com.guams.review.exception.UnauthorizedExecption; | ||||||
| import com.guams.review.model.AuthorRepository; | import com.guams.review.model.AuthorRepository; | ||||||
| import com.guams.review.model.Role; | import com.guams.review.model.Role; | ||||||
| import com.guams.review.model.dao.Author; | import com.guams.review.model.dao.Author; | ||||||
| @ -26,6 +26,7 @@ import org.springframework.web.bind.annotation.*; | |||||||
| import org.springframework.web.multipart.MultipartFile; | import org.springframework.web.multipart.MultipartFile; | ||||||
| 
 | 
 | ||||||
| import java.io.IOException; | import java.io.IOException; | ||||||
|  | import java.util.Base64; | ||||||
| import java.util.List; | import java.util.List; | ||||||
| import java.util.UUID; | import java.util.UUID; | ||||||
| 
 | 
 | ||||||
| @ -58,11 +59,13 @@ public class AuthorController { | |||||||
|         return authorService.insert(updatedAuthor |         return authorService.insert(updatedAuthor | ||||||
|                 .setId(authorToUpdate.getId()) |                 .setId(authorToUpdate.getId()) | ||||||
|                 .setRole(authorToUpdate.getRole()) |                 .setRole(authorToUpdate.getRole()) | ||||||
|  |                 .setProfilePicture(authorToUpdate.getProfilePicture()) | ||||||
|                 .setPassword(passwordEncoder.encode(updatedAuthor.getPassword()))); |                 .setPassword(passwordEncoder.encode(updatedAuthor.getPassword()))); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @PutMapping(value = "{id}/avatar", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE}) |     @PutMapping(value = "{id}/avatar", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE}) | ||||||
|     public Author updateUserAvatar(@PathVariable UUID id, @RequestPart MultipartFile avatar, Authentication authentication) throws IOException { |     public Author updateUserAvatar(@PathVariable UUID id, @RequestPart MultipartFile avatar, Authentication authentication) throws IOException { | ||||||
|  |         if (avatar != null) { | ||||||
|             Author authorToUpdate = authorService.verifyIfUserIsAuthorized(authentication, id); |             Author authorToUpdate = authorService.verifyIfUserIsAuthorized(authentication, id); | ||||||
|             ReturnableAuthor authorToReturn = authorService.insert(authorToUpdate.setProfilePicture(avatar.getBytes())); |             ReturnableAuthor authorToReturn = authorService.insert(authorToUpdate.setProfilePicture(avatar.getBytes())); | ||||||
|             return new Author() |             return new Author() | ||||||
| @ -71,12 +74,23 @@ public class AuthorController { | |||||||
|                     .setProfilePicture(null) |                     .setProfilePicture(null) | ||||||
|                     .setRole(authorToReturn.getRole()) |                     .setRole(authorToReturn.getRole()) | ||||||
|                     .setPassword(""); |                     .setPassword(""); | ||||||
|  |         } else { | ||||||
|  |             return authorService.verifyIfUserIsAuthorized(authentication, id) | ||||||
|  |                     .setProfilePicture(null) | ||||||
|  |                     .setPassword(""); | ||||||
|  | 
 | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @GetMapping("/{id}/avatar") |     @GetMapping("/{id}/avatar") | ||||||
|     public byte[] getProfilePicture(@PathVariable UUID id) { |     public byte[] getProfilePicture(@PathVariable UUID id) { | ||||||
|         Author author = authorService.findById(id).orElseThrow(() -> new NotFoundException("Author not found")); |         Author author = authorService.findById(id).orElseThrow(() -> new NotFoundException("Author not found")); | ||||||
|         return author.getProfilePicture(); |         if (author.getProfilePicture() != null) { | ||||||
|  |             return Base64.getEncoder().encode(author.getProfilePicture()); | ||||||
|  |         } else { | ||||||
|  |             return null; | ||||||
|  |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @DeleteMapping("/{id}") |     @DeleteMapping("/{id}") | ||||||
|  | |||||||
| @ -1,4 +1,6 @@ | |||||||
| package com.guams.review.controller; | package com.guams.review.controller; | ||||||
|  | 
 | ||||||
|  | import com.guams.review.exception.StringTooLongException; | ||||||
| import com.guams.review.exception.UnauthorizedExecption; | import com.guams.review.exception.UnauthorizedExecption; | ||||||
| import com.guams.review.exception.NotFoundException; | import com.guams.review.exception.NotFoundException; | ||||||
| import com.guams.review.model.AuthorRepository; | import com.guams.review.model.AuthorRepository; | ||||||
| @ -45,6 +47,10 @@ public class CommentController { | |||||||
|             throw new UnauthorizedExecption("Vous n'êtes pas autorisé à faire ça"); |             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")); |         Author author = authorRepository.findByName(authentication.getName()).orElseThrow(() -> new NotFoundException("Cet auteur n'existe pas")); | ||||||
|         Comment insertedComment = commentService.insert(comment |         Comment insertedComment = commentService.insert(comment | ||||||
|                 .setCommentDate(Timestamp.from(Instant.now())) |                 .setCommentDate(Timestamp.from(Instant.now())) | ||||||
| @ -61,24 +67,6 @@ public class CommentController { | |||||||
|         return commentService.getCommentsByPostId(postId); |         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}") |     @DeleteMapping("/{id}") | ||||||
|     public void deleteComment(@PathVariable Long id, Authentication authentication) { |     public void deleteComment(@PathVariable Long id, Authentication authentication) { | ||||||
|         if (authentication == null || !authentication.isAuthenticated()) { |         if (authentication == null || !authentication.isAuthenticated()) { | ||||||
|  | |||||||
| @ -0,0 +1,7 @@ | |||||||
|  | package com.guams.review.exception; | ||||||
|  | 
 | ||||||
|  | public class StringTooLongException extends RuntimeException { | ||||||
|  |     public StringTooLongException(String message) { | ||||||
|  |         super(message); | ||||||
|  |     } | ||||||
|  | } | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user