-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
모임 댓글 기능 구현
- Loading branch information
Showing
15 changed files
with
397 additions
and
7 deletions.
There are no files selected for viewing
60 changes: 57 additions & 3 deletions
60
backend/src/main/java/mouda/backend/comment/domain/Comment.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 |
---|---|---|
@@ -1,34 +1,88 @@ | ||
package mouda.backend.comment.domain; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
import org.springframework.http.HttpStatus; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.ManyToOne; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import mouda.backend.comment.exception.CommentErrorMessage; | ||
import mouda.backend.comment.exception.CommentException; | ||
import mouda.backend.member.domain.Member; | ||
import mouda.backend.moim.domain.Moim; | ||
|
||
@Entity | ||
@Getter | ||
@NoArgsConstructor | ||
public class Comment { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@Column(nullable = false) | ||
private String content; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "moim_id") | ||
@JoinColumn(nullable = false) | ||
private Moim moim; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "member_id") | ||
@JoinColumn(nullable = false) | ||
private Member member; | ||
|
||
@Column(nullable = false) | ||
private LocalDateTime createdAt; | ||
|
||
private Long parentId; | ||
|
||
@Builder | ||
public Comment(String content, Moim moim, Member member, LocalDateTime createdAt, Long parentId) { | ||
validateContent(content); | ||
validateMoim(moim); | ||
validateMember(member); | ||
this.content = content; | ||
this.moim = moim; | ||
this.member = member; | ||
this.createdAt = createdAt; | ||
this.parentId = parentId; | ||
} | ||
|
||
private void validateContent(String content) { | ||
if (content == null || content.isBlank()) { | ||
throw new CommentException(HttpStatus.BAD_REQUEST, CommentErrorMessage.CONTENT_NOT_FOUND); | ||
} | ||
} | ||
|
||
private void validateMoim(Moim moim) { | ||
if (moim == null) { | ||
throw new CommentException(HttpStatus.NOT_FOUND, CommentErrorMessage.MOIM_NOT_FOUND); | ||
} | ||
} | ||
|
||
private void validateMember(Member member) { | ||
if (member == null) { | ||
throw new CommentException(HttpStatus.NOT_FOUND, CommentErrorMessage.MEMBER_NOT_FOUND); | ||
} | ||
} | ||
|
||
public boolean isParent() { | ||
return parentId == null; | ||
} | ||
|
||
public boolean isChild() { | ||
return parentId != null; | ||
} | ||
|
||
public String getAuthorNickname() { | ||
return member.getNickname(); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
backend/src/main/java/mouda/backend/comment/dto/request/CommentCreateRequest.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,25 @@ | ||
package mouda.backend.comment.dto.request; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
import jakarta.validation.constraints.NotNull; | ||
import mouda.backend.comment.domain.Comment; | ||
import mouda.backend.member.domain.Member; | ||
import mouda.backend.moim.domain.Moim; | ||
|
||
public record CommentCreateRequest( | ||
Long parentId, | ||
|
||
@NotNull | ||
String content | ||
) { | ||
public Comment toEntity(Moim moim, Member member) { | ||
return Comment.builder() | ||
.content(content) | ||
.moim(moim) | ||
.member(member) | ||
.createdAt(LocalDateTime.now()) | ||
.parentId(parentId) | ||
.build(); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
backend/src/main/java/mouda/backend/comment/dto/response/ChildCommentResponse.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,29 @@ | ||
package mouda.backend.comment.dto.response; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
import com.fasterxml.jackson.annotation.JsonFormat; | ||
|
||
import lombok.Builder; | ||
import mouda.backend.comment.domain.Comment; | ||
|
||
@Builder | ||
public record ChildCommentResponse( | ||
Long commentId, | ||
|
||
String nickname, | ||
|
||
String content, | ||
|
||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm") | ||
LocalDateTime dateTime | ||
) { | ||
public static ChildCommentResponse toResponse(Comment comment) { | ||
return ChildCommentResponse.builder() | ||
.commentId(comment.getId()) | ||
.nickname(comment.getAuthorNickname()) | ||
.content(comment.getContent()) | ||
.dateTime(comment.getCreatedAt()) | ||
.build(); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
backend/src/main/java/mouda/backend/comment/dto/response/CommentResponse.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,37 @@ | ||
package mouda.backend.comment.dto.response; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
import com.fasterxml.jackson.annotation.JsonFormat; | ||
|
||
import lombok.Builder; | ||
import mouda.backend.comment.domain.Comment; | ||
|
||
@Builder | ||
public record CommentResponse( | ||
Long commentId, | ||
|
||
String nickname, | ||
|
||
String content, | ||
|
||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm") | ||
LocalDateTime dateTime, | ||
|
||
List<ChildCommentResponse> children | ||
) { | ||
public static CommentResponse toResponse(Comment parentComment, List<Comment> childComments) { | ||
List<ChildCommentResponse> children = childComments.stream() | ||
.map(ChildCommentResponse::toResponse) | ||
.toList(); | ||
|
||
return CommentResponse.builder() | ||
.commentId(parentComment.getId()) | ||
.nickname(parentComment.getAuthorNickname()) | ||
.content(parentComment.getContent()) | ||
.dateTime(parentComment.getCreatedAt()) | ||
.children(children) | ||
.build(); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
backend/src/main/java/mouda/backend/comment/exception/CommentErrorMessage.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,16 @@ | ||
package mouda.backend.comment.exception; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public enum CommentErrorMessage { | ||
|
||
CONTENT_NOT_FOUND("댓글 내용이 존재하지 않습니다."), | ||
MEMBER_NOT_FOUND("작성자가 존재하지 않습니다."), | ||
PARENT_NOT_FOUND("부모 댓글이 존재하지 않습니다."), | ||
MOIM_NOT_FOUND("모임이 존재하지 않습니다."); | ||
|
||
private final String message; | ||
} |
12 changes: 12 additions & 0 deletions
12
backend/src/main/java/mouda/backend/comment/exception/CommentException.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,12 @@ | ||
package mouda.backend.comment.exception; | ||
|
||
import org.springframework.http.HttpStatus; | ||
|
||
import mouda.backend.exception.MoudaException; | ||
|
||
public class CommentException extends MoudaException { | ||
|
||
public CommentException(HttpStatus httpStatus, CommentErrorMessage commentErrorMessage) { | ||
super(httpStatus, commentErrorMessage.getMessage()); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
backend/src/main/java/mouda/backend/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,12 @@ | ||
package mouda.backend.comment.repository; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import mouda.backend.comment.domain.Comment; | ||
|
||
public interface CommentRepository extends JpaRepository<Comment, Long> { | ||
|
||
List<Comment> findAllByMoimIdOrderByCreatedAt(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
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
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
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.