Skip to content

Commit

Permalink
fix/Comment, ContentMark 응답 및 기능 일부 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
dnjsals45 authored Feb 23, 2024
2 parents 3940954 + 216df99 commit 5882172
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,21 @@ public ResponseEntity<?> createComment(@Parameter(name = "content_id", descripti

@Operation(summary = "댓글 수정", description = "comment_id 댓글 수정")
@PatchMapping("/{comment_id}")
@PreAuthorize("isAuthenticated() && @authorManager.isCommentAuthor(#authentication.getPrincipal(), #commentId)")
@PreAuthorize("isAuthenticated() && @authorManager.isCommentAuthor(authentication.getPrincipal(), #commentId)")
public ResponseEntity<?> updateComment(@Parameter(name = "content_id", description = "게시글 번호") @PathVariable(name = "content_id") Long contentId,
@Parameter(name = "comment_id", description = "댓글 번호") @PathVariable(name = "comment_id") Long commentId,
@Valid @RequestBody UpdateCommentReq request,
@AuthenticationPrincipal CustomUserDetails securityUser) {
return ResponseEntity.ok().body(SuccessResponse.from(commentService.updateComment(commentId, request, securityUser)));
return ResponseEntity.ok().body(SuccessResponse.from(commentService.updateComment(contentId, commentId, request, securityUser)));
}

@Operation(summary = "댓글 삭제", description = "comment_id 댓글 삭제. Soft Delete")
@DeleteMapping("/{comment_id}")
@PreAuthorize("isAuthenticated() && @authorManager.isCommentAuthor(#authentication.getPrincipal(), #commentId)")
@PreAuthorize("isAuthenticated() && @authorManager.isCommentAuthor(authentication.getPrincipal(), #commentId)")
public ResponseEntity<?> deleteComment(@Parameter(name = "content_id", description = "게시글 번호") @PathVariable(name = "content_id") Long contentId,
@Parameter(name = "comment_id", description = "댓글 번호") @PathVariable(name = "comment_id") Long commentId,
@AuthenticationPrincipal CustomUserDetails securityUser) {
return ResponseEntity.ok().body(SuccessResponse.from(commentService.deleteComment(commentId, securityUser)));
return ResponseEntity.ok().body(SuccessResponse.from(commentService.deleteComment(contentId, commentId, securityUser)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ public class CommentService {
* @param contentId
*/
@Transactional
public List<CommentDTO> getComments(Long contentId) {
public List<GetCommentRes> getComments(Long contentId) {
contentRepository.findById(contentId)
.orElseThrow(() -> new ContentErrorException(ContentErrorCode.INVALID_CONTENT_ID));

List<Comment> comments = commentRepository.findCommentsByContentId(contentId);

List<CommentDTO> response = new ArrayList<>();
List<GetCommentRes> response = new ArrayList<>();

for (Comment comment : comments) {
CommentDTO dto = CommentDTO.fromEntity(comment);
GetCommentRes dto = GetCommentRes.fromEntity(comment);
response.add(dto);
}

Expand Down Expand Up @@ -84,7 +84,11 @@ public CreateCommentRes createComment(Long contentId, CreateCommentReq req, Cust
* 댓글 수정
* @param req
*/
public UpdateCommentRes updateComment(Long commentId, UpdateCommentReq req, CustomUserDetails securityUser) {
public UpdateCommentRes updateComment(Long contentId, Long commentId, UpdateCommentReq req, CustomUserDetails securityUser) {
if (!contentRepository.existsByIdAndDeleteAtIsNull(contentId)) {
throw new ContentErrorException(ContentErrorCode.INVALID_CONTENT_ID);
}

Comment comment = commentRepository.findById(commentId)
.orElseThrow(() -> new CommentErrorException(CommentErrorCode.INVALID_COMMENT_ID));

Expand All @@ -101,7 +105,11 @@ public UpdateCommentRes updateComment(Long commentId, UpdateCommentReq req, Cust
* @param commentId
*/
@Transactional
public DeleteCommentRes deleteComment(Long commentId, CustomUserDetails securityUser) {
public DeleteCommentRes deleteComment(Long contentId, Long commentId, CustomUserDetails securityUser) {
if (!contentRepository.existsByIdAndDeleteAtIsNull(contentId)) {
throw new ContentErrorException(ContentErrorCode.INVALID_CONTENT_ID);
}

Comment comment = commentRepository.findById(commentId)
.orElseThrow(() -> new CommentErrorException(CommentErrorCode.INVALID_COMMENT_ID));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package io.oopy.coding.api.mark.service;

import io.oopy.coding.api.content.exception.ContentErrorCode;
import io.oopy.coding.api.content.exception.ContentErrorException;
import io.oopy.coding.api.content.service.ContentService;
import io.oopy.coding.api.mark.exception.ContentMarkErrorCode;
import io.oopy.coding.api.mark.exception.ContentMarkErrorException;
import io.oopy.coding.domain.mark.entity.MarkType;
import io.oopy.coding.domain.mark.dto.ChangeUserPressReq;
import io.oopy.coding.domain.mark.dto.ContentMarkDto;
Expand Down Expand Up @@ -40,8 +44,8 @@ public ContentMarkDto getMarkByContent(Long contentId) {
}

return ContentMarkDto.builder()
.like(likeCount)
.bookmark(bookmarkCount)
.like(likeCount == null ? 0 : likeCount)
.bookmark(bookmarkCount == null ? 0 : bookmarkCount)
.build();
}

Expand Down Expand Up @@ -72,8 +76,14 @@ else if (contentMark.getType().equals(MarkType.BOOKMARK))
@Transactional
public void changeUserPress(Long contentId, CustomUserDetails securityUser, ChangeUserPressReq req) {
Content content = contentService.findContent(contentId);
if (content.getDeleteAt() != null) {
throw new ContentErrorException(ContentErrorCode.DELETED_CONTENT);
}

MarkType markType = MarkType.fromString(req.getType());
if (markType == null) {
throw new ContentMarkErrorException(ContentMarkErrorCode.INVALID_TYPE);
}

ContentMark userMark = contentMarkRepository.findContentMarksByContentIdAndUserIdAndType(contentId, securityUser.getUserId(), markType);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ public class CommentDTO {
private Long id;
private String commentBody;
private Long parentId;
private LocalDateTime deleteAt;
private LocalDateTime deletedAt;

public static CommentDTO fromEntity(Comment comment) {
return CommentDTO.builder()
.id(comment.getId())
.commentBody(comment.getCommentBody())
.parentId(comment.getParentId())
.deleteAt(comment.getDeleteAt())
.deletedAt(comment.getDeleteAt())
.build();
}
}
34 changes: 30 additions & 4 deletions src/main/java/io/oopy/coding/domain/comment/dto/GetCommentRes.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.oopy.coding.domain.comment.dto;

import io.oopy.coding.domain.comment.entity.Comment;
import io.oopy.coding.domain.user.entity.User;
import lombok.*;

import java.time.LocalDateTime;
Expand All @@ -9,23 +11,47 @@
@Builder
public class GetCommentRes {

private Comment comment;
private User user;
private ResComment comment;
private ResUser user;

@Builder
@Getter
public static class Comment {
public static class ResComment {
private String body;
private Long parentId;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
private LocalDateTime deletedAt;

public static ResComment from(Comment comment) {
return ResComment.builder()
.body(comment.getCommentBody())
.parentId(comment.getParentId())
.createdAt(comment.getCreatedAt())
.updatedAt(comment.getUpdatedAt())
.deletedAt(comment.getDeleteAt())
.build();
}
}

@Builder
@Getter
public static class User {
public static class ResUser {
private String name;
private String profileImageUrl;

public static ResUser from(User user) {
return ResUser.builder()
.name(user.getName())
.profileImageUrl(user.getProfileImageUrl())
.build();
}
}

public static GetCommentRes fromEntity(Comment comment) {
return GetCommentRes.builder()
.comment(ResComment.from(comment))
.user(ResUser.from(comment.getUser()))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@
public interface ContentRepository extends JpaRepository<Content, Long> {
List<Content> findContentsByUserId(Long userId);
boolean existsByIdAndUserId(Long id, Long userId);

boolean existsByIdAndDeleteAtIsNull(Long id);
}

0 comments on commit 5882172

Please sign in to comment.