From f41c83097045c32fc2361308b65f4fec04096167 Mon Sep 17 00:00:00 2001 From: Guams Date: Thu, 23 Jan 2025 21:54:29 +0100 Subject: [PATCH] Eh jsp j'ai fais des trucs --- .../review/controller/AuthorController.java | 6 +++++ .../review/controller/PostController.java | 6 +++++ .../guams/review/model/CommentRepository.java | 2 +- .../guams/review/model/PostRepository.java | 20 ++++++++++++++ .../review/model/dao/CommentWithAuthor.java | 2 +- .../review/model/dao/PostWithAuthor.java | 26 +++++++++++++++++++ .../com/guams/review/service/PostService.java | 5 ++++ 7 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/guams/review/model/dao/PostWithAuthor.java diff --git a/src/main/java/com/guams/review/controller/AuthorController.java b/src/main/java/com/guams/review/controller/AuthorController.java index 192e1b5..4a61f5a 100644 --- a/src/main/java/com/guams/review/controller/AuthorController.java +++ b/src/main/java/com/guams/review/controller/AuthorController.java @@ -76,6 +76,12 @@ public class AuthorController { .setPassword(""); } + @GetMapping("/{id}/avatar") + public byte[] getProfilePicture(@PathVariable UUID id) { + Author author = authorService.findById(id).orElseThrow(() -> new NotFoundException("Author not found")); + return author.getProfilePicture(); + } + @DeleteMapping("/{id}") public void deleteUser(@PathVariable UUID id, Authentication authentication) { Author authorToDelete = authorService.verifyIfUserIsAuthorized(authentication, id); diff --git a/src/main/java/com/guams/review/controller/PostController.java b/src/main/java/com/guams/review/controller/PostController.java index 2b22451..574ffa1 100644 --- a/src/main/java/com/guams/review/controller/PostController.java +++ b/src/main/java/com/guams/review/controller/PostController.java @@ -9,6 +9,7 @@ import com.guams.review.service.AuthorService; import com.guams.review.service.CommentService; import com.guams.review.service.PostService; import com.guams.review.service.mapper.Mapper; +import com.guams.review.model.dao.PostWithAuthor; import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; @@ -39,6 +40,11 @@ public class PostController { return postService.list(); } + @GetMapping("authors") + public List listPostsWithAuthor() { + return postService.listWithAuthors(); + } + @GetMapping("/{id}") public Post findPost(@PathVariable Long id) { return postService.findById(id).orElseThrow(() -> new NotFoundException("Post not found")); diff --git a/src/main/java/com/guams/review/model/CommentRepository.java b/src/main/java/com/guams/review/model/CommentRepository.java index c10d42a..b81409b 100644 --- a/src/main/java/com/guams/review/model/CommentRepository.java +++ b/src/main/java/com/guams/review/model/CommentRepository.java @@ -31,7 +31,7 @@ public interface CommentRepository extends CrudRepository { " JOIN comment_association ca ON ca.comment_fk = c.id" + " JOIN post p ON p.id = ca.post_fk" + " JOIN author a ON ca.author_fk = a.id" + - " WHERE p.id = 3") + " WHERE p.id = :postId") List getCommentsByPostId(Long postId); @Modifying diff --git a/src/main/java/com/guams/review/model/PostRepository.java b/src/main/java/com/guams/review/model/PostRepository.java index 1d79211..7bffe43 100644 --- a/src/main/java/com/guams/review/model/PostRepository.java +++ b/src/main/java/com/guams/review/model/PostRepository.java @@ -1,6 +1,7 @@ package com.guams.review.model; import com.guams.review.model.dao.Post; +import com.guams.review.model.dao.PostWithAuthor; import org.springframework.data.jdbc.repository.query.Query; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; @@ -15,5 +16,24 @@ public interface PostRepository extends CrudRepository { "from post join publication on post.id=publication.post_fk where publication.author_fk=:authorId") List findPostsOfAuthor(UUID authorId); + + @Query(value = "SELECT " + + " a.id AS author_id, " + + " a.name AS author_name, " + + " a.profile_picture AS author_profile_picture, " + + " a.role AS role, " + + " p.id AS post_id, " + + " p.description AS post_description, " + + " p.illustration AS post_illustration, " + + " p.title AS post_title, " + + " p.body AS post_body, " + + " p.category AS post_category, " + + " p.publication_date AS post_publication_date, " + + " p.is_updated AS post_is_updated " + + "FROM post p " + + " JOIN publication pub ON p.id = pub.post_fk " + + " JOIN author a ON pub.author_fk = a.id;") + List findPostsWithAuthors(); + List findAll(); } diff --git a/src/main/java/com/guams/review/model/dao/CommentWithAuthor.java b/src/main/java/com/guams/review/model/dao/CommentWithAuthor.java index 68044d1..9d026e9 100644 --- a/src/main/java/com/guams/review/model/dao/CommentWithAuthor.java +++ b/src/main/java/com/guams/review/model/dao/CommentWithAuthor.java @@ -13,7 +13,7 @@ public class CommentWithAuthor { private Long id; private UUID authorId; private String authorName; - private String profilePicture; + private byte[] profilePicture; private String authorRole; private String content; private Timestamp commentDate; diff --git a/src/main/java/com/guams/review/model/dao/PostWithAuthor.java b/src/main/java/com/guams/review/model/dao/PostWithAuthor.java new file mode 100644 index 0000000..706c40b --- /dev/null +++ b/src/main/java/com/guams/review/model/dao/PostWithAuthor.java @@ -0,0 +1,26 @@ +package com.guams.review.model.dao; + +import lombok.Getter; +import lombok.Setter; +import lombok.experimental.Accessors; + +import java.sql.Timestamp; +import java.util.UUID; + +@Getter +@Setter +@Accessors(chain = true) +public class PostWithAuthor { + private UUID authorId; + private String authorName; + private byte[] authorProfilePicture; + private String role; + private Long postId; + private String postDescription; + private byte[] postIllustration; + private String postTitle; + private String postBody; + private String postCategory; + private Timestamp postPublicationDate; + private Boolean postIsUpdated; +} diff --git a/src/main/java/com/guams/review/service/PostService.java b/src/main/java/com/guams/review/service/PostService.java index 2f2c346..b0c50e0 100644 --- a/src/main/java/com/guams/review/service/PostService.java +++ b/src/main/java/com/guams/review/service/PostService.java @@ -4,6 +4,7 @@ package com.guams.review.service; import com.guams.review.model.AuthorRepository; import com.guams.review.model.dao.Post; import com.guams.review.model.PostRepository; +import com.guams.review.model.dao.PostWithAuthor; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -27,6 +28,10 @@ public class PostService return postRepository.findById(id); } + public List listWithAuthors() { + return postRepository.findPostsWithAuthors(); + } + public Post insert(Post post) { return postRepository.save(post); }