-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from gutanbug/dev
feat #80 : 댓글 및 대댓글 서비스 추가
- Loading branch information
Showing
12 changed files
with
403 additions
and
5 deletions.
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
src/main/java/com/renew/sw/mentoring/domain/comment/CommentService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package com.renew.sw.mentoring.domain.comment; | ||
|
||
import com.renew.sw.mentoring.domain.comment.exception.CommentNotFoundException; | ||
import com.renew.sw.mentoring.domain.comment.model.CommentStatus; | ||
import com.renew.sw.mentoring.domain.comment.model.dto.response.SummarizedCommentDto; | ||
import com.renew.sw.mentoring.domain.comment.model.dto.response.SummarizedReplyDto; | ||
import com.renew.sw.mentoring.domain.comment.model.entity.Comment; | ||
import com.renew.sw.mentoring.domain.comment.repository.CommentRepository; | ||
import com.renew.sw.mentoring.domain.post.exception.PostNotFoundException; | ||
import com.renew.sw.mentoring.domain.post.model.entity.Post; | ||
import com.renew.sw.mentoring.domain.post.repository.PostRepository; | ||
import com.renew.sw.mentoring.domain.user.exception.UserNotFoundException; | ||
import com.renew.sw.mentoring.domain.user.model.entity.User; | ||
import com.renew.sw.mentoring.domain.user.repository.UserRepository; | ||
import com.renew.sw.mentoring.global.error.exception.NotGrantedException; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class CommentService { | ||
|
||
private final PostRepository postRepository; | ||
private final UserRepository userRepository; | ||
private final CommentRepository commentRepository; | ||
|
||
@Transactional(readOnly = true) | ||
public List<SummarizedCommentDto> list(Long postId) { | ||
List<Comment> comments = commentRepository.findByPostIdExceptReply(postId); | ||
return comments.stream().map(e -> { | ||
List<Comment> replies = commentRepository.findAllReplies(e.getId()); | ||
return new SummarizedCommentDto(e, replies.stream().map(SummarizedReplyDto::new).toList()); | ||
}).collect(Collectors.toList()); | ||
} | ||
|
||
@Transactional | ||
public Long create(Long postId, Long userId, String content) { | ||
Post post = postRepository.findById(postId).orElseThrow(PostNotFoundException::new); | ||
User user = userRepository.findById(userId).orElseThrow(UserNotFoundException::new); | ||
|
||
Comment comment = Comment.builder() | ||
.user(user) | ||
.post(post) | ||
.content(content) | ||
.commentStatus(CommentStatus.ACTIVE) | ||
.build(); | ||
|
||
comment.changePost(post); | ||
comment = commentRepository.save(comment); | ||
return comment.getId(); | ||
} | ||
|
||
@Transactional | ||
public void edit(Long commentId, Long userId, String content) { | ||
Comment comment = commentRepository.findById(commentId).orElseThrow(CommentNotFoundException::new); | ||
|
||
if(!comment.getUser().getId().equals(userId)) { | ||
throw new NotGrantedException(); | ||
} | ||
if(comment.getCommentStatus() == CommentStatus.ACTIVE | ||
|| comment.getCommentStatus() == CommentStatus.EDITED) { | ||
comment.updateContent(content); | ||
} | ||
} | ||
|
||
@Transactional | ||
public void delete(Long commentId, Long userId) { | ||
Comment comment = commentRepository.findById(commentId).orElseThrow(CommentNotFoundException::new); | ||
|
||
if(comment.getUser().getUserRole().isAdmin()) { | ||
comment.markedAsDeleted(true); | ||
} else if(comment.getUser().getId().equals(userId)) { | ||
comment.markedAsDeleted(false); | ||
} else { | ||
throw new NotGrantedException(); | ||
} | ||
} | ||
|
||
@Transactional | ||
public Long createReply(Long commentId, Long userId, String content) { | ||
Comment parentComment = commentRepository.findById(commentId).orElseThrow(CommentNotFoundException::new); | ||
Long postId = parentComment.getPost().getId(); | ||
Post post = postRepository.findById(postId).orElseThrow(PostNotFoundException::new); | ||
User user = userRepository.findById(userId).orElseThrow(UserNotFoundException::new); | ||
|
||
Comment comment = Comment.builder() | ||
.user(user) | ||
.post(post) | ||
.content(content) | ||
.commentStatus(CommentStatus.ACTIVE) | ||
.parentCommentId(parentComment.getId()) | ||
.build(); | ||
|
||
comment.changePost(post); | ||
comment = commentRepository.save(comment); | ||
parentComment.addChildComment(comment); | ||
return comment.getId(); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/renew/sw/mentoring/domain/comment/exception/CommentNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.renew.sw.mentoring.domain.comment.exception; | ||
|
||
import com.renew.sw.mentoring.global.error.exception.LocalizedMessageException; | ||
import org.springframework.http.HttpStatus; | ||
|
||
public class CommentNotFoundException extends LocalizedMessageException { | ||
|
||
public CommentNotFoundException() { | ||
super(HttpStatus.NOT_FOUND, "notfound.comment"); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...java/com/renew/sw/mentoring/domain/comment/model/dto/request/RequestCreateCommentDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.renew.sw.mentoring.domain.comment.model.dto.request; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import javax.validation.constraints.NotBlank; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class RequestCreateCommentDto { | ||
|
||
@NotBlank | ||
private String content; | ||
|
||
public RequestCreateCommentDto(String content) { | ||
this.content = content; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...n/java/com/renew/sw/mentoring/domain/comment/model/dto/response/SummarizedCommentDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.renew.sw.mentoring.domain.comment.model.dto.response; | ||
|
||
import com.renew.sw.mentoring.domain.comment.model.CommentStatus; | ||
import com.renew.sw.mentoring.domain.comment.model.entity.Comment; | ||
import lombok.Getter; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
@Getter | ||
public class SummarizedCommentDto { | ||
|
||
private final Long id; | ||
|
||
private final String author; | ||
|
||
private final String content; | ||
|
||
private final List<SummarizedReplyDto> replies; | ||
|
||
public SummarizedCommentDto(Comment comment, List<SummarizedReplyDto> replies) { | ||
this.id = comment.getId(); | ||
if(checkDeletedComment(comment)) { | ||
this.author = null; | ||
this.content = "삭제된 댓글입니다."; | ||
} else { | ||
this.author = comment.getUser().getNickname(); | ||
this.content = comment.getContent(); | ||
} | ||
this.replies = Objects.requireNonNullElseGet(replies, List::of); | ||
} | ||
|
||
private boolean checkDeletedComment(Comment comment) { | ||
return comment.getCommentStatus() == CommentStatus.DELETED || comment.getCommentStatus() == CommentStatus.DELETED_BY_ADMIN; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...ain/java/com/renew/sw/mentoring/domain/comment/model/dto/response/SummarizedReplyDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.renew.sw.mentoring.domain.comment.model.dto.response; | ||
|
||
import com.renew.sw.mentoring.domain.comment.model.CommentStatus; | ||
import com.renew.sw.mentoring.domain.comment.model.entity.Comment; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class SummarizedReplyDto { | ||
|
||
private final Long id; | ||
|
||
private final String author; | ||
|
||
private final String content; | ||
|
||
public SummarizedReplyDto(Comment comment) { | ||
this.id = comment.getId(); | ||
if(checkDeletedReply(comment)) { | ||
this.author = null; | ||
this.content = "삭제된 댓글입니다."; | ||
} else { | ||
this.author = comment.getUser().getNickname(); | ||
this.content = comment.getContent(); | ||
} | ||
} | ||
|
||
private boolean checkDeletedReply(Comment comment) { | ||
return comment.getCommentStatus() == CommentStatus.DELETED || comment.getCommentStatus() == CommentStatus.DELETED_BY_ADMIN; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/main/java/com/renew/sw/mentoring/domain/comment/repository/CommentRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.renew.sw.mentoring.domain.comment.repository; | ||
|
||
import com.renew.sw.mentoring.domain.comment.model.entity.Comment; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
|
||
import java.util.List; | ||
|
||
public interface CommentRepository extends JpaRepository<Comment, Long> { | ||
@Query("select c from Comment c " + | ||
"join fetch c.post " + | ||
"where c.post.id = :postId and c.parentCommentId is null " + | ||
"order by c.createdAt asc ") | ||
List<Comment> findByPostIdExceptReply(@Param("postId") Long postId); | ||
|
||
@Query("select c from Comment c " + | ||
"join fetch c.post " + | ||
"where c.parentCommentId=:id order by c.createdAt asc ") | ||
List<Comment> findAllReplies(Long id); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.