diff --git a/src/main/java/com/devin/dev/controller/post/PostController.java b/src/main/java/com/devin/dev/controller/post/PostController.java index 68a9a18..b559b18 100644 --- a/src/main/java/com/devin/dev/controller/post/PostController.java +++ b/src/main/java/com/devin/dev/controller/post/PostController.java @@ -27,14 +27,15 @@ public DefaultResponse getPostList(@RequestParam(defaultValue = "-1") Long id @GetMapping("/postlist/{id}") public DefaultResponse getPostDetails( @PathVariable("id") Long postId, - @RequestParam(defaultValue = "true", name = "sort_reply") boolean sort_reply) { + @RequestParam(defaultValue = "true", name = "sort_reply") boolean sort_reply, + HttpServletRequest request) { ReplyOrderCondition replyOrderCondition = new ReplyOrderCondition(); if (!sort_reply) { replyOrderCondition.setLatestDate(true); } else { replyOrderCondition.setLikeCount(true); } - return postService.getPost(postId, replyOrderCondition); + return postService.getPost(postId, replyOrderCondition, request); } @GetMapping("/postlist") diff --git a/src/main/java/com/devin/dev/dto/post/PostDetailsDto.java b/src/main/java/com/devin/dev/dto/post/PostDetailsDto.java index 31edc22..97a048d 100644 --- a/src/main/java/com/devin/dev/dto/post/PostDetailsDto.java +++ b/src/main/java/com/devin/dev/dto/post/PostDetailsDto.java @@ -3,6 +3,8 @@ import com.devin.dev.entity.post.Post; import com.devin.dev.entity.post.PostImage; import com.devin.dev.entity.post.PostStatus; +import com.devin.dev.entity.reply.ReplyStatus; +import com.devin.dev.entity.user.UserStatus; import com.querydsl.core.annotations.QueryProjection; import lombok.Data; @@ -24,13 +26,14 @@ public class PostDetailsDto { private List post_images; private List post_tags; private boolean status_accept; + private boolean like_post; private LocalDateTime date_create; private LocalDateTime date_update; private Integer reply_num; private List reply; @QueryProjection - public PostDetailsDto(Long post_id, Long publisher_id, String publisher_name, String publisher_profile, Long publisher_exp, String title, String content, List post_images, List post_tags, boolean status_accept, boolean like_post, LocalDateTime date_create, LocalDateTime date_update, Integer reply_num, List replyDtos) { + public PostDetailsDto(Long post_id, Long publisher_id, String publisher_name, String publisher_profile, Long publisher_exp, String title, String content, List post_images, List post_tags, boolean status_accept, boolean like_post, LocalDateTime date_create, LocalDateTime date_update, Integer reply_num, List reply) { this.post_id = post_id; this.publisher_id = publisher_id; this.publisher_name = publisher_name; @@ -41,10 +44,11 @@ public PostDetailsDto(Long post_id, Long publisher_id, String publisher_name, St this.post_images = post_images; this.post_tags = post_tags; this.status_accept = status_accept; + this.like_post = like_post; this.date_create = date_create; this.date_update = date_update; this.reply_num = reply_num; - this.reply = replyDtos; + this.reply = reply; } public PostDetailsDto(Post post) { @@ -58,9 +62,35 @@ public PostDetailsDto(Post post) { this.post_images = post.getImages().stream().map(PostImage::getPath).collect(Collectors.toList()); this.post_tags = post.getPostTags(); this.status_accept = post.getStatus() == PostStatus.SELECTED; + this.like_post = post.getLikes().size() > 0; this.date_create = post.getCreatedDate(); this.date_update = post.getLastModifiedDate(); this.reply_num = post.getReplies().size(); this.reply = post.getReplies().stream().map(PostReplyDto::new).collect(Collectors.toList()); } + + public PostDetailsDto(Post post, UserStatus status) { + this.post_id = post.getId(); + this.publisher_id = post.getUser().getId(); + this.publisher_name = post.getUser().getName(); + this.publisher_profile = post.getUser().getProfile(); + this.publisher_exp = post.getUser().getExp(); + this.title = post.getTitle(); + this.content = post.getContent(); + this.post_images = post.getImages().stream().map(PostImage::getPath).collect(Collectors.toList()); + this.post_tags = post.getPostTags(); + this.status_accept = post.getStatus() == PostStatus.SELECTED; + this.like_post = post.getLikes().size() > 0; + this.date_create = post.getCreatedDate(); + this.date_update = post.getLastModifiedDate(); + this.reply_num = post.getReplies().size(); + this.reply = status == UserStatus.ACTIVE || status == UserStatus.ADMIN ? + post.getReplies().stream().map(PostReplyDto::new).collect(Collectors.toList()) : + post.getReplies().stream().map(PostReplyDto::new).peek(dto -> { + if (dto.isStatus_accept()) { + dto.setContent("비회원은 볼 수 없습니다."); + dto.getReply_images().clear(); + } + }).collect(Collectors.toList()); + } } diff --git a/src/main/java/com/devin/dev/entity/user/UserStatus.java b/src/main/java/com/devin/dev/entity/user/UserStatus.java index 698c066..625cbe1 100644 --- a/src/main/java/com/devin/dev/entity/user/UserStatus.java +++ b/src/main/java/com/devin/dev/entity/user/UserStatus.java @@ -1,5 +1,5 @@ package com.devin.dev.entity.user; public enum UserStatus { - ADMIN, ACTIVE, DORMANT, SUSPENDED, DELETED + ADMIN, ACTIVE, DORMANT, SUSPENDED, DELETED, NON_MEMBERS } diff --git a/src/main/java/com/devin/dev/repository/post/PostRepositoryQuery.java b/src/main/java/com/devin/dev/repository/post/PostRepositoryQuery.java index 803e156..abc9ef8 100644 --- a/src/main/java/com/devin/dev/repository/post/PostRepositoryQuery.java +++ b/src/main/java/com/devin/dev/repository/post/PostRepositoryQuery.java @@ -7,6 +7,7 @@ import com.devin.dev.dto.post.PostSimpleDto; import com.devin.dev.entity.post.Post; import com.devin.dev.entity.user.User; +import com.devin.dev.entity.user.UserStatus; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; @@ -23,5 +24,7 @@ public interface PostRepositoryQuery { Page findAllByTagId(Long id, Pageable pageable); - Optional findPostDetailsById(Long id, ReplyOrderCondition condition); + Optional findPostDetailsByIdWithUserType(Long id, UserStatus status, ReplyOrderCondition condition); + + Optional findPostDetailsByIdWithUserType(Long id, ReplyOrderCondition condition); } diff --git a/src/main/java/com/devin/dev/repository/post/PostRepositoryQueryImpl.java b/src/main/java/com/devin/dev/repository/post/PostRepositoryQueryImpl.java index 8ab6408..28cf4b5 100644 --- a/src/main/java/com/devin/dev/repository/post/PostRepositoryQueryImpl.java +++ b/src/main/java/com/devin/dev/repository/post/PostRepositoryQueryImpl.java @@ -8,6 +8,7 @@ import com.devin.dev.entity.reply.QReplyImage; import com.devin.dev.entity.reply.QReplyLike; import com.devin.dev.entity.user.User; +import com.devin.dev.entity.user.UserStatus; import com.querydsl.core.QueryResults; import com.querydsl.core.types.OrderSpecifier; import com.querydsl.core.types.dsl.BooleanExpression; @@ -136,6 +137,27 @@ public Optional findPostDetailsById(Long id, ReplyOrderCondition Optional postDetailsDtoOptional; + postDetailsDtoOptional = result != null ? Optional.of(new PostDetailsDto(result, status)) : Optional.empty(); + + return postDetailsDtoOptional; + } + + @Override + public Optional findPostDetailsByIdWithUserType(Long id, ReplyOrderCondition condition) { + Post result = queryFactory + .select(post) + .from(post) + .leftJoin(post.replies, reply).fetchJoin() + .leftJoin(post.user, user).fetchJoin() + .leftJoin(reply.user, user).fetchJoin() + .where(post.id.eq(id)) + .orderBy( + replyOrder(condition) + ) + .fetchOne(); + + Optional postDetailsDtoOptional; + postDetailsDtoOptional = result != null ? Optional.of(new PostDetailsDto(result)) : Optional.empty(); return postDetailsDtoOptional; diff --git a/src/main/java/com/devin/dev/service/PostService.java b/src/main/java/com/devin/dev/service/PostService.java index 5bd7484..f2c1853 100644 --- a/src/main/java/com/devin/dev/service/PostService.java +++ b/src/main/java/com/devin/dev/service/PostService.java @@ -7,8 +7,8 @@ import com.devin.dev.dto.post.PostInfoDto; import com.devin.dev.dto.reply.ReplyLikeDto; import com.devin.dev.entity.post.*; -import com.devin.dev.entity.reply.ReplyLike; import com.devin.dev.entity.user.User; +import com.devin.dev.entity.user.UserStatus; import com.devin.dev.model.DefaultResponse; import com.devin.dev.repository.post.PostImageRepository; import com.devin.dev.repository.post.PostLikeRepository; @@ -165,7 +165,26 @@ public DefaultResponse editPost(Long userId, Long postId, String title, Strin @Transactional(readOnly = true) public DefaultResponse getPost(Long postId, ReplyOrderCondition replyOrderCondition) { - Optional postOptional = postRepository.findPostDetailsById(postId, replyOrderCondition); + Optional postOptional = postRepository.findPostDetailsByIdWithUserType(postId, replyOrderCondition); + if (postOptional.isEmpty()) { + return new DefaultResponse<>(StatusCode.NOT_EXIST, ResponseMessage.NOT_FOUND_POST); + } + PostDetailsDto postDetailsDto = postOptional.get(); + + return new DefaultResponse<>(StatusCode.SUCCESS, ResponseMessage.FOUND_POST, postDetailsDto); + } + + @Transactional(readOnly = true) + public DefaultResponse getPost(Long postId, ReplyOrderCondition replyOrderCondition, HttpServletRequest request) { + String token = tokenProvider.parseToken(request); + UserStatus userStatus; + if (tokenProvider.validateToken(token)) { + userStatus = UserStatus.ACTIVE; + } else { + userStatus = UserStatus.NON_MEMBERS; + } + + Optional postOptional = postRepository.findPostDetailsByIdWithUserType(postId, userStatus, replyOrderCondition); if (postOptional.isEmpty()) { return new DefaultResponse<>(StatusCode.NOT_EXIST, ResponseMessage.NOT_FOUND_POST); } diff --git a/src/test/java/com/devin/dev/service/PostServiceTest.java b/src/test/java/com/devin/dev/service/PostServiceTest.java index 8d0d37b..d50c11f 100644 --- a/src/test/java/com/devin/dev/service/PostServiceTest.java +++ b/src/test/java/com/devin/dev/service/PostServiceTest.java @@ -13,9 +13,6 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.mock.web.MockHttpServletRequest; -import org.springframework.test.annotation.Rollback; -import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; import org.springframework.transaction.annotation.Transactional; import javax.persistence.EntityManager;