Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/익명 게시글 response 처리 #259

Merged
merged 8 commits into from
Aug 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.keeper.homepage.domain.comment.application;

import static com.keeper.homepage.domain.post.application.PostService.ANONYMOUS_NAME;
import static com.keeper.homepage.domain.post.entity.category.Category.CategoryType.익명게시판;
import static com.keeper.homepage.global.error.ErrorCode.COMMENT_NOT_ALLOWED;
import static com.keeper.homepage.global.error.ErrorCode.COMMENT_NOT_PARENT;
import static com.keeper.homepage.global.error.ErrorCode.COMMENT_NOT_WRITER;
Expand Down Expand Up @@ -63,7 +65,12 @@ public CommentListResponse getComments(Long postId) {
List<Comment> comments = post.getComments();

List<CommentResponse> commentResponses = comments.stream()
.map(CommentResponse::from)
.map(comment -> {
if (post.isCategory(익명게시판)) {
return CommentResponse.of(comment, ANONYMOUS_NAME, null);
}
return CommentResponse.from(comment);
})
.toList();
return CommentListResponse.from(commentResponses);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,17 @@ public static CommentResponse from(Comment comment) {
.dislikeCount(comment.getCommentDislikes().size())
.build();
}

public static CommentResponse of(Comment comment, String writerName, String writerThumbnailPath) {
return CommentResponse.builder()
.commentId(comment.getId())
.writerName(writerName)
.writerThumbnailPath(writerThumbnailPath)
.content(comment.getContent())
.registerTime(comment.getRegisterTime())
.parentId(comment.getParent() != null ? comment.getParent().getId() : null)
.likeCount(comment.getCommentLikes().size())
.dislikeCount(comment.getCommentDislikes().size())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.keeper.homepage.domain.post.application;

import static com.keeper.homepage.domain.post.entity.category.Category.CategoryType.ANONYMOUS_CATEGORY;
import static com.keeper.homepage.domain.post.entity.category.Category.CategoryType.EXAM_CATEGORY;
import static com.keeper.homepage.domain.post.entity.category.Category.CategoryType.익명게시판;
import static com.keeper.homepage.domain.post.entity.category.Category.CategoryType.시험게시판;
import static com.keeper.homepage.global.error.ErrorCode.FILE_NOT_FOUND;
import static com.keeper.homepage.global.error.ErrorCode.POST_ACCESS_CONDITION_NEED;
import static com.keeper.homepage.global.error.ErrorCode.POST_CONTENT_NEED;
Expand Down Expand Up @@ -120,7 +120,7 @@ public PostDetailResponse find(Member member, long postId, String password) {
}

private void checkExamPost(Member member, Post post) {
if (post.isCategory(EXAM_CATEGORY.getId())) {
if (post.isCategory(시험게시판)) {
checkAccessibleExamPost(member, post);
}
}
Expand Down Expand Up @@ -165,14 +165,14 @@ private void checkAccessibleSecretPost(Member member, Post post, String password
}

private String getWriterName(Post post) {
if (post.isCategory(ANONYMOUS_CATEGORY.getId())) {
if (post.isCategory(익명게시판)) {
return ANONYMOUS_NAME;
}
return post.getWriterNickname();
}

private String getWriterThumbnailPath(Post post) {
if (post.isCategory(ANONYMOUS_CATEGORY.getId())) {
if (post.isCategory(익명게시판)) {
return null;
}
return post.getMember().getThumbnailPath();
Expand Down Expand Up @@ -215,6 +215,7 @@ public void deletePostThumbnail(Member member, long postId) {
thumbnailUtil.deleteFileAndEntityIfExist(post.getThumbnail());
}

@Transactional
public void delete(Member member, long postId) {
Post post = validPostFindService.findById(postId);

Expand Down Expand Up @@ -282,30 +283,37 @@ public Page<PostResponse> getPosts(long categoryId, String searchType, String se
Category category = categoryFindService.findById(categoryId);
if (searchType == null) {
return postRepository.findAllRecentByCategory(category, pageable)
.map(PostResponse::from);
.map(this::getPostResponse);
}
if (searchType.equals("title")) {
return postRepository.findAllRecentByCategoryAndTitle(category, search, pageable)
.map(PostResponse::from);
.map(this::getPostResponse);
}
if (searchType.equals("content")) {
return postRepository.findAllRecentByCategoryAndContent(category, search, pageable)
.map(PostResponse::from);
.map(this::getPostResponse);
}
if (searchType.equals("writer")) {
return postRepository.findAllRecentByCategoryAndWriter(category, search, pageable)
.map(PostResponse::from);
.map(this::getPostResponse);
}
if (searchType.equals("title+content")) {
return postRepository.findAllRecentByCategoryAndTitleOrContent(category, search, pageable)
.map(PostResponse::from);
.map(this::getPostResponse);
}
throw new BusinessException(searchType, "searchType", POST_SEARCH_TYPE_NOT_FOUND);
}

private PostResponse getPostResponse(Post post) {
if (post.isCategory(익명게시판)) {
return PostResponse.of(post, ANONYMOUS_NAME, null);
}
return PostResponse.from(post);
}

public List<PostResponse> getRecentPosts() {
return postRepository.findAllRecent().stream()
.map(PostResponse::from)
.map(this::getPostResponse)
.limit(10)
.toList();
}
Expand All @@ -320,7 +328,7 @@ public List<PostResponse> getTrendPosts() {
return Integer.compare(postScore2, postScore1);
});
return posts.stream()
.map(PostResponse::from)
.map(this::getPostResponse)
.limit(10)
.toList();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.keeper.homepage.domain.post.converter;

import com.keeper.homepage.domain.post.entity.category.Category.CategoryType;
import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;
import lombok.extern.slf4j.Slf4j;

@Converter
@Slf4j
public class CategoryTypeConverter implements AttributeConverter<CategoryType, String> {

@Override
public String convertToDatabaseColumn(CategoryType type) {
return type != null ? type.getName() : null;
}

@Override
public CategoryType convertToEntityAttribute(String dbData) {
if (dbData == null) {
return null;
}
try {
return CategoryType.fromCode(dbData);
} catch (IllegalArgumentException e) {
log.error("failure to convert cause unexpected code [{}]", dbData, e);
throw e;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,32 @@ public interface PostRepository extends JpaRepository<Post, Long> {

Optional<Post> findByIdAndIdNot(Long postId, Long virtualId);

/**
* 카테고리 + 공지글 + 등록시간 최신순 정렬
*
* @param category 게시글 카테고리
*/
@Query("SELECT p FROM Post p "
+ "WHERE p.category = :category "
+ "AND p.isNotice = true "
+ "ORDER BY p.registerTime DESC")
List<Post> findAllNoticeByCategory(@Param("category") Category category);

/**
* 임시 저장글 제외 + 등록 시간 최신순 정렬
*/
@Query("SELECT p FROM Post p "
+ "WHERE p.isTemp = false "
+ "AND p.id <> 1 " // virtual post
+ "ORDER BY p.registerTime DESC")
List<Post> findAllRecent();

/**
* 카테고리 + 공지글 제외 + 임시글 제외 + 등록시간 최신순 정렬
*
* @param category 게시글 카테고리
* @param pageable Pageable
*/
@Query("SELECT p FROM Post p "
+ "WHERE p.category = :category "
+ "AND p.isNotice = false "
Expand All @@ -35,6 +49,13 @@ public interface PostRepository extends JpaRepository<Post, Long> {
+ "ORDER BY p.registerTime DESC")
Page<Post> findAllRecentByCategory(@Param("category") Category category, Pageable pageable);

/**
* 카테고리 + 공지글 제외 + 임시글 제외 + 제목 검색 + 등록시간 최신순 정렬
*
* @param category 게시글 카테고리
* @param search 검색어
* @param pageable Pageable
*/
@Query("SELECT p FROM Post p "
+ "WHERE p.category = :category "
+ "AND p.isNotice = false "
Expand All @@ -44,6 +65,13 @@ public interface PostRepository extends JpaRepository<Post, Long> {
Page<Post> findAllRecentByCategoryAndTitle(@Param("category") Category category, @Param("search") String search,
Pageable pageable);

/**
* 카테고리 + 공지글 제외 + 임시글 제외 + 내용 검색 + 등록시간 최신순 정렬
*
* @param category 게시글 카테고리
* @param search 검색어
* @param pageable Pageable
*/
@Query("SELECT p FROM Post p "
+ "WHERE p.category = :category "
+ "AND p.isNotice = false "
Expand All @@ -53,6 +81,13 @@ Page<Post> findAllRecentByCategoryAndTitle(@Param("category") Category category,
Page<Post> findAllRecentByCategoryAndContent(@Param("category") Category category, @Param("search") String search,
Pageable pageable);

/**
* 카테고리 + 공지글 제외 + 임시글 제외 + 제목&내용 검색 + 등록시간 최신순 정렬
*
* @param category 게시글 카테고리
* @param search 검색어
* @param pageable Pageable
*/
@Query("SELECT p FROM Post p "
+ "WHERE p.category = :category "
+ "AND p.isNotice = false "
Expand All @@ -63,6 +98,13 @@ Page<Post> findAllRecentByCategoryAndContent(@Param("category") Category categor
Page<Post> findAllRecentByCategoryAndTitleOrContent(@Param("category") Category category,
@Param("search") String search, Pageable pageable);

/**
* 카테고리 + 공지글 제외 + 임시글 제외 + 작성자 닉네임 검색 + 등록시간 최신순 정렬
*
* @param category 게시글 카테고리
* @param search 검색어
* @param pageable Pageable
*/
@Query("SELECT p FROM Post p "
+ "WHERE p.category = :category "
+ "AND p.isNotice = false "
Expand All @@ -72,6 +114,12 @@ Page<Post> findAllRecentByCategoryAndTitleOrContent(@Param("category") Category
Page<Post> findAllRecentByCategoryAndWriter(@Param("category") Category category, @Param("search") String search,
Pageable pageable);

/**
* 임시 저장글 제외 + 등록 시간 최신순 정렬 + 날짜 사이의 게시글
*
* @param startDate 가져올 시작 시간
* @param endDate 가져올 끝 시간
*/
Comment on lines +117 to +122
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JPA 메서드에 대한 주석 혹시 어떤가요??

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

변경이 자주 일어날 것 같지 않아서 전 개인적으로 좋은 것 같아요~ 👍👍👍

@Query("SELECT p FROM Post p " +
"WHERE p.isTemp = false " +
"AND p.registerTime BETWEEN :startDate AND :endDate")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static PostDetailResponse of(Post post, String writerName, String writerT
Post nextPost) {
return PostDetailResponse.builder()
.categoryId(post.getCategory().getId())
.categoryName(post.getCategory().getName())
.categoryName(post.getCategory().getType().toString())
.title(post.getTitle())
.writerName(writerName)
.writerThumbnailPath(writerThumbnailPath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class PostResponse {
private Long id;
private String title;
private String writerName;
private String writerThumbnailPath;
private Integer visitCount;
private Integer commentCount;
private Boolean isSecret;
Expand All @@ -30,6 +31,21 @@ public static PostResponse from(Post post) {
.id(post.getId())
.title(post.getTitle())
.writerName(post.getWriterNickname())
.writerThumbnailPath(post.getMember().getThumbnailPath())
.visitCount(post.getVisitCount())
.commentCount(post.getComments().size())
.isSecret(post.isSecret())
.thumbnailPath(post.getThumbnailPath())
.registerTime(post.getRegisterTime())
.build();
}

public static PostResponse of(Post post, String writerName, String writerThumbnailPath) {
return PostResponse.builder()
.id(post.getId())
.title(post.getTitle())
.writerName(writerName)
.writerThumbnailPath(writerThumbnailPath)
.visitCount(post.getVisitCount())
.commentCount(post.getComments().size())
.isSecret(post.isSecret())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.keeper.homepage.domain.post.entity;

import static com.keeper.homepage.domain.post.entity.category.Category.getCategoryBy;
import static jakarta.persistence.CascadeType.ALL;
import static jakarta.persistence.CascadeType.REMOVE;
import static jakarta.persistence.FetchType.LAZY;
Expand All @@ -12,6 +13,7 @@
import com.keeper.homepage.domain.member.entity.post.MemberHasPostDislike;
import com.keeper.homepage.domain.member.entity.post.MemberHasPostLike;
import com.keeper.homepage.domain.post.entity.category.Category;
import com.keeper.homepage.domain.post.entity.category.Category.CategoryType;
import com.keeper.homepage.domain.thumbnail.entity.Thumbnail;
import com.keeper.homepage.global.entity.BaseEntity;
import jakarta.persistence.Column;
Expand Down Expand Up @@ -141,8 +143,8 @@ public void addVisitCount() {
this.visitCount++;
}

public boolean isCategory(long categoryId) {
return this.category.getId().equals(categoryId);
public boolean isCategory(CategoryType category) {
return this.category.equals(getCategoryBy(category));
}

public boolean isMine(Member member) {
Expand Down
Loading