Skip to content

Commit

Permalink
[J2KB-3rd-Season#13]feat: 게시글 조회시 비회원 채택글 제한기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
enjay27 committed Jun 11, 2021
1 parent ced6254 commit 888e70b
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ public DefaultResponse<?> getPostList(@RequestParam(defaultValue = "-1") Long id
@GetMapping("/postlist/{id}")
public DefaultResponse<PostDetailsDto> 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")
Expand Down
34 changes: 32 additions & 2 deletions src/main/java/com/devin/dev/dto/post/PostDetailsDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -24,13 +26,14 @@ public class PostDetailsDto {
private List<String> post_images;
private List<String> post_tags;
private boolean status_accept;
private boolean like_post;
private LocalDateTime date_create;
private LocalDateTime date_update;
private Integer reply_num;
private List<PostReplyDto> reply;

@QueryProjection
public PostDetailsDto(Long post_id, Long publisher_id, String publisher_name, String publisher_profile, Long publisher_exp, String title, String content, List<String> post_images, List<String> post_tags, boolean status_accept, boolean like_post, LocalDateTime date_create, LocalDateTime date_update, Integer reply_num, List<PostReplyDto> replyDtos) {
public PostDetailsDto(Long post_id, Long publisher_id, String publisher_name, String publisher_profile, Long publisher_exp, String title, String content, List<String> post_images, List<String> post_tags, boolean status_accept, boolean like_post, LocalDateTime date_create, LocalDateTime date_update, Integer reply_num, List<PostReplyDto> reply) {
this.post_id = post_id;
this.publisher_id = publisher_id;
this.publisher_name = publisher_name;
Expand All @@ -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) {
Expand All @@ -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());
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/devin/dev/entity/user/UserStatus.java
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -23,5 +24,7 @@ public interface PostRepositoryQuery {

Page<Post> findAllByTagId(Long id, Pageable pageable);

Optional<PostDetailsDto> findPostDetailsById(Long id, ReplyOrderCondition condition);
Optional<PostDetailsDto> findPostDetailsByIdWithUserType(Long id, UserStatus status, ReplyOrderCondition condition);

Optional<PostDetailsDto> findPostDetailsByIdWithUserType(Long id, ReplyOrderCondition condition);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -136,6 +137,27 @@ public Optional<PostDetailsDto> findPostDetailsById(Long id, ReplyOrderCondition

Optional<PostDetailsDto> postDetailsDtoOptional;

postDetailsDtoOptional = result != null ? Optional.of(new PostDetailsDto(result, status)) : Optional.empty();

return postDetailsDtoOptional;
}

@Override
public Optional<PostDetailsDto> 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<PostDetailsDto> postDetailsDtoOptional;

postDetailsDtoOptional = result != null ? Optional.of(new PostDetailsDto(result)) : Optional.empty();

return postDetailsDtoOptional;
Expand Down
23 changes: 21 additions & 2 deletions src/main/java/com/devin/dev/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -165,7 +165,26 @@ public DefaultResponse<?> editPost(Long userId, Long postId, String title, Strin

@Transactional(readOnly = true)
public DefaultResponse<PostDetailsDto> getPost(Long postId, ReplyOrderCondition replyOrderCondition) {
Optional<PostDetailsDto> postOptional = postRepository.findPostDetailsById(postId, replyOrderCondition);
Optional<PostDetailsDto> 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<PostDetailsDto> 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<PostDetailsDto> postOptional = postRepository.findPostDetailsByIdWithUserType(postId, userStatus, replyOrderCondition);
if (postOptional.isEmpty()) {
return new DefaultResponse<>(StatusCode.NOT_EXIST, ResponseMessage.NOT_FOUND_POST);
}
Expand Down
3 changes: 0 additions & 3 deletions src/test/java/com/devin/dev/service/PostServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 888e70b

Please sign in to comment.