-
Notifications
You must be signed in to change notification settings - Fork 0
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
[FEAT] 소소피드 댓글 CRUD API 구현
- Loading branch information
Showing
14 changed files
with
327 additions
and
16 deletions.
There are no files selected for viewing
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
11 changes: 11 additions & 0 deletions
11
src/main/java/org/websoso/WSSServer/dto/comment/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,11 @@ | ||
package org.websoso.WSSServer.dto.comment; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.Size; | ||
|
||
public record CommentCreateRequest( | ||
@NotBlank(message = "댓글 내용은 null 이거나, 공백일 수 없습니다.") | ||
@Size(max = 100, message = "댓글 내용은 100자를 초과할 수 없습니다.") | ||
String commentContent | ||
) { | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/org/websoso/WSSServer/dto/comment/CommentGetResponse.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,31 @@ | ||
package org.websoso.WSSServer.dto.comment; | ||
|
||
import com.fasterxml.jackson.annotation.JsonFormat; | ||
import java.time.LocalDate; | ||
import org.websoso.WSSServer.domain.Comment; | ||
import org.websoso.WSSServer.dto.user.UserBasicInfo; | ||
|
||
public record CommentGetResponse( | ||
Long userId, | ||
String nickname, | ||
String avatarImage, | ||
Long commentId, | ||
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "M월 d일", timezone = "Asia/Seoul") | ||
LocalDate createdDate, | ||
String commentContent, | ||
Boolean isModified, | ||
Boolean isMyComment | ||
) { | ||
public static CommentGetResponse of(UserBasicInfo userBasicInfo, Comment comment, Boolean isMyComment) { | ||
return new CommentGetResponse( | ||
userBasicInfo.userId(), | ||
userBasicInfo.nickname(), | ||
userBasicInfo.avatarImage(), | ||
comment.getCommentId(), | ||
comment.getCreatedDate().toLocalDate(), | ||
comment.getCommentContent(), | ||
!comment.getCreatedDate().equals(comment.getModifiedDate()), | ||
isMyComment | ||
); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/org/websoso/WSSServer/dto/comment/CommentUpdateRequest.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 org.websoso.WSSServer.dto.comment; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.Size; | ||
|
||
public record CommentUpdateRequest( | ||
@NotBlank(message = "댓글 내용은 null 이거나, 공백일 수 없습니다.") | ||
@Size(max = 100, message = "댓글 내용은 100자를 초과할 수 없습니다.") | ||
String commentContent | ||
) { | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/org/websoso/WSSServer/dto/comment/CommentsGetResponse.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,15 @@ | ||
package org.websoso.WSSServer.dto.comment; | ||
|
||
import java.util.List; | ||
|
||
public record CommentsGetResponse( | ||
Integer commentsCount, | ||
List<CommentGetResponse> comments | ||
) { | ||
public static CommentsGetResponse of(Integer commentsCount, List<CommentGetResponse> comments) { | ||
return new CommentsGetResponse( | ||
commentsCount, | ||
comments | ||
); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/org/websoso/WSSServer/exception/error/CustomCommentError.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 org.websoso.WSSServer.exception.error; | ||
|
||
import static org.springframework.http.HttpStatus.NOT_FOUND; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
import org.websoso.WSSServer.exception.common.ICustomError; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public enum CustomCommentError implements ICustomError { | ||
|
||
COMMENT_NOT_FOUND("COMMENT-001", "해당 ID를 가진 댓글을 찾을 수 없습니다.", NOT_FOUND), | ||
COMMENT_NOT_BELONG_TO_FEED("COMMENT-002", "댓글이 지정된 피드에 속하지 않습니다.", NOT_FOUND); | ||
|
||
private final String code; | ||
private final String description; | ||
private final HttpStatus statusCode; | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/org/websoso/WSSServer/exception/exception/CustomCommentException.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,13 @@ | ||
package org.websoso.WSSServer.exception.exception; | ||
|
||
import lombok.Getter; | ||
import org.websoso.WSSServer.exception.common.AbstractCustomException; | ||
import org.websoso.WSSServer.exception.error.CustomCommentError; | ||
|
||
@Getter | ||
public class CustomCommentException extends AbstractCustomException { | ||
|
||
public CustomCommentException(CustomCommentError customCommentError, String message) { | ||
super(customCommentError, message); | ||
} | ||
} |
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
9 changes: 9 additions & 0 deletions
9
src/main/java/org/websoso/WSSServer/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,9 @@ | ||
package org.websoso.WSSServer.repository; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
import org.websoso.WSSServer.domain.Comment; | ||
|
||
@Repository | ||
public interface CommentRepository extends JpaRepository<Comment, Long> { | ||
} |
87 changes: 87 additions & 0 deletions
87
src/main/java/org/websoso/WSSServer/service/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,87 @@ | ||
package org.websoso.WSSServer.service; | ||
|
||
import static org.websoso.WSSServer.domain.common.Action.DELETE; | ||
import static org.websoso.WSSServer.domain.common.Action.UPDATE; | ||
import static org.websoso.WSSServer.exception.error.CustomCommentError.COMMENT_NOT_FOUND; | ||
|
||
import java.util.AbstractMap; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.websoso.WSSServer.domain.Comment; | ||
import org.websoso.WSSServer.domain.Feed; | ||
import org.websoso.WSSServer.domain.User; | ||
import org.websoso.WSSServer.dto.comment.CommentGetResponse; | ||
import org.websoso.WSSServer.dto.comment.CommentsGetResponse; | ||
import org.websoso.WSSServer.dto.user.UserBasicInfo; | ||
import org.websoso.WSSServer.exception.exception.CustomCommentException; | ||
import org.websoso.WSSServer.repository.CommentRepository; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional | ||
public class CommentService { | ||
|
||
private final CommentRepository commentRepository; | ||
private final UserService userService; | ||
private final AvatarService avatarService; | ||
private final BlockService blockService; | ||
|
||
public void createComment(Long userId, Feed feed, String commentContent) { | ||
commentRepository.save(Comment.create(userId, feed, commentContent)); | ||
} | ||
|
||
public void updateComment(Long userId, Feed feed, Long commentId, String commentContent) { | ||
Comment comment = getCommentOrException(commentId); | ||
comment.validateFeedAssociation(feed); | ||
comment.validateUserAuthorization(userId, UPDATE); | ||
comment.updateContent(commentContent); | ||
} | ||
|
||
public void deleteComment(Long userId, Feed feed, Long commentId) { | ||
Comment comment = getCommentOrException(commentId); | ||
comment.validateFeedAssociation(feed); | ||
comment.validateUserAuthorization(userId, DELETE); | ||
commentRepository.delete(comment); | ||
} | ||
|
||
@Transactional(readOnly = true) | ||
public CommentsGetResponse getComments(User user, Feed feed) { | ||
List<Comment> comments = feed.getComments(); | ||
|
||
List<CommentGetResponse> responses = comments | ||
.stream() | ||
.map(comment -> new AbstractMap.SimpleEntry<>( | ||
comment, userService.getUserOrException(comment.getUserId()) | ||
)) | ||
.filter(entry -> !entry.getKey().getIsHidden() && !isBlocked(entry.getValue(), user)) | ||
.map(entry -> CommentGetResponse.of( | ||
getUserBasicInfo(entry.getValue()), | ||
entry.getKey(), | ||
isUserCommentOwner(entry.getValue(), user))) | ||
.toList(); | ||
|
||
return CommentsGetResponse.of(comments.size(), responses); | ||
} | ||
|
||
private Comment getCommentOrException(Long commentId) { | ||
return commentRepository.findById(commentId).orElseThrow(() -> | ||
new CustomCommentException(COMMENT_NOT_FOUND, "comment with the given id was not found")); | ||
} | ||
|
||
private UserBasicInfo getUserBasicInfo(User user) { | ||
return user.getUserBasicInfo( | ||
avatarService.getAvatarOrException(user.getAvatarId()).getAvatarImage() | ||
); | ||
} | ||
|
||
private Boolean isUserCommentOwner(User createdUser, User user) { | ||
return createdUser.equals(user); | ||
} | ||
|
||
private Boolean isBlocked(User createdFeedUser, User user) { | ||
return blockService.isBlockedRelationship(user.getUserId(), createdFeedUser.getUserId()); | ||
} | ||
|
||
} |
Oops, something went wrong.