Skip to content

Commit

Permalink
fix: 게시글 상세보기에도 댓글 수 버그 수정
Browse files Browse the repository at this point in the history
* refactor: 댓글 개수 구하는 비즈니스 로직 엔티티 내부로 분리

* refactor: 엔티티 내부 메소드를 사용

* fix: 게시글 상세보기도 삭제되지 않은 댓글만 개수를 세도록 수정

* refactor: getCommentCount -> countComment
  • Loading branch information
jjuny0310 authored Oct 30, 2023
1 parent 21f62a5 commit 12ee9da
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 84 deletions.
148 changes: 71 additions & 77 deletions src/main/java/com/ssafy/ssafsound/domain/post/domain/Post.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,19 @@
package com.ssafy.ssafsound.domain.post.domain;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import com.ssafy.ssafsound.domain.BaseTimeEntity;
import com.ssafy.ssafsound.domain.board.domain.Board;
import com.ssafy.ssafsound.domain.comment.domain.Comment;
import com.ssafy.ssafsound.domain.member.domain.Member;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity(name = "post")
@Getter
Expand All @@ -35,64 +24,69 @@
@Where(clause = "deleted_post = false")
public class Post extends BaseTimeEntity {

@Id
@Column(name = "post_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column
private String title;

@Column
private String content;

@Column
private Long view;

@Builder.Default
@Column
private Boolean deletedPost = Boolean.FALSE;

@Column
private Boolean anonymity;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "board_id")
private Board board;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
private Member member;

@Builder.Default
@OneToMany(mappedBy = "post")
private List<PostImage> images = new ArrayList<>();

@Builder.Default
@OneToMany(mappedBy = "post")
private List<PostLike> likes = new ArrayList<>();

@Builder.Default
@OneToMany(mappedBy = "post")
private List<Comment> comments = new ArrayList<>();

@Builder.Default
@OneToMany(mappedBy = "post")
private List<PostScrap> scraps = new ArrayList<>();

public void updatePost(String title, String content, Boolean anonymity) {
this.title = title;
this.content = content;
this.anonymity = anonymity;
}

public static Post of(Board board, Member member, String title, String content, Boolean anonymity) {
return Post.builder()
.board(board)
.member(member)
.title(title)
.content(content)
.anonymity(anonymity)
.build();
}
@Id
@Column(name = "post_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column
private String title;

@Column
private String content;

@Column
private Long view;

@Builder.Default
@Column
private Boolean deletedPost = Boolean.FALSE;

@Column
private Boolean anonymity;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "board_id")
private Board board;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
private Member member;

@Builder.Default
@OneToMany(mappedBy = "post")
private List<PostImage> images = new ArrayList<>();

@Builder.Default
@OneToMany(mappedBy = "post")
private List<PostLike> likes = new ArrayList<>();

@Builder.Default
@OneToMany(mappedBy = "post")
private List<Comment> comments = new ArrayList<>();

@Builder.Default
@OneToMany(mappedBy = "post")
private List<PostScrap> scraps = new ArrayList<>();

public void updatePost(String title, String content, Boolean anonymity) {
this.title = title;
this.content = content;
this.anonymity = anonymity;
}

public static Post of(Board board, Member member, String title, String content, Boolean anonymity) {
return Post.builder()
.board(board)
.member(member)
.title(title)
.content(content)
.anonymity(anonymity)
.build();
}

public int countComment() {
return (int) this.getComments().stream()
.filter(comment -> !comment.getDeletedComment()).count();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static GetPostDetailElement of(Post post, Member loginMember) {
.title(post.getTitle())
.content(post.getContent())
.likeCount(post.getLikes().size())
.commentCount(post.getComments().size())
.commentCount(post.countComment())
.scrapCount(post.getScraps().size())
.createdAt(post.getCreatedAt())
.anonymity(post.getAnonymity())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,13 @@ public GetPostElement(Post post) {
this.title = post.getTitle();
this.content = post.getContent();
this.likeCount = post.getLikes().size();
this.commentCount = getCommentSize(post);
this.commentCount = post.countComment();
this.createdAt = post.getCreatedAt();
this.nickname = anonymity ? "익명" : post.getMember().getNickname();
this.anonymity = anonymity;
this.thumbnail = findThumbnailUrl(post);
}

private int getCommentSize(Post post) {
return (int) post.getComments().stream()
.filter(comment -> !comment.getDeletedComment()).count();
}

private String findThumbnailUrl(Post post) {
List<PostImage> images = post.getImages();
if (images.size() >= 1)
Expand Down

0 comments on commit 12ee9da

Please sign in to comment.