Skip to content

Commit

Permalink
Merge pull request #134 from Me1tingPot/feature/#133
Browse files Browse the repository at this point in the history
[REFACTOR] 커뮤니티 코드 리팩터링
  • Loading branch information
moonyaeyoon committed Jul 25, 2024
2 parents a8f36ad + 0fe13ed commit 77580d9
Show file tree
Hide file tree
Showing 26 changed files with 661 additions and 806 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import meltingpot.server.comment.dto.CommentRequestDTO;
import meltingpot.server.comment.dto.CommentResponseDTO;
import meltingpot.server.comment.dto.CommentCreateRequest;
import meltingpot.server.comment.dto.CommentsListResponse;
import meltingpot.server.comment.service.CommentService;
import meltingpot.server.domain.entity.Account;
import meltingpot.server.util.CurrentUser;
Expand All @@ -22,47 +22,45 @@ public class CommentController {

@Operation(summary = "댓글 작성")
@PostMapping("/{postId}")
public ResponseEntity<ResponseData<CommentResponseDTO.CreateCommentResultDTO>> createComment(@RequestBody CommentRequestDTO.CreateCommentDTO createCommentDTO, @CurrentUser Account account, @PathVariable Long postId) {
public ResponseEntity<ResponseData> createComment(@RequestBody CommentCreateRequest commentCreateRequest, @CurrentUser Account account, @PathVariable Long postId) {
try {
return ResponseData.toResponseEntity(ResponseCode.CREATE_COMMENT_SUCCESS, commentService.createComment(createCommentDTO,account,postId));
return ResponseData.toResponseEntity(commentService.createComment(commentCreateRequest,account,postId));
} catch (NoSuchElementException e) {
return ResponseData.toResponseEntity(ResponseCode.COMMENT_CREATE_FAIL, null);
return ResponseData.toResponseEntity(ResponseCode.COMMENT_CREATE_FAIL);
}
}

@Operation(summary = "대댓글 작성")
@PostMapping("/child/{commentId}")
public ResponseEntity<ResponseData<CommentResponseDTO.CreateCommentResultDTO>> createChildComment(@RequestBody CommentRequestDTO.CreateCommentDTO createCommentDTO, @CurrentUser Account account,@PathVariable Long commentId) {
public ResponseEntity<ResponseData> createChildComment(@RequestBody CommentCreateRequest commentCreateRequest, @CurrentUser Account account,@PathVariable Long commentId) {
try {
return ResponseData.toResponseEntity(ResponseCode.CREATE_CHILD_COMMENT_SUCCESS, commentService.createChildComment(createCommentDTO,account,commentId));
return ResponseData.toResponseEntity(commentService.createChildComment(commentCreateRequest,account,commentId));
} catch (NoSuchElementException e) {
return ResponseData.toResponseEntity(ResponseCode.COMMENT_CREATE_FAIL, null);
return ResponseData.toResponseEntity(ResponseCode.COMMENT_CREATE_FAIL);
}
}

@Operation(summary = "댓글 수정")
@PutMapping("/{commentId}")
public ResponseEntity<ResponseData<CommentResponseDTO.CreateCommentResultDTO>> updateComment(@RequestBody CommentRequestDTO.CreateCommentDTO createCommentDTO, @CurrentUser Account account, @PathVariable Long commentId){
public ResponseEntity<ResponseData> updateComment(@RequestBody CommentCreateRequest commentCreateRequest, @CurrentUser Account account, @PathVariable Long commentId){
try {
return ResponseData.toResponseEntity(ResponseCode.UPDATE_COMMENT_SUCCESS, commentService.updateComment(createCommentDTO,account,commentId));
return ResponseData.toResponseEntity(commentService.updateComment(commentCreateRequest,account,commentId));
} catch (NoSuchElementException e) {
return ResponseData.toResponseEntity(ResponseCode.COMMENT_UPDATE_FAIL, null);
return ResponseData.toResponseEntity(ResponseCode.COMMENT_UPDATE_FAIL);
}
}

@Operation(summary = "댓글 목록 가져오기")
@GetMapping("/list/{postId}")
public ResponseEntity<ResponseData<CommentResponseDTO.CommentsListDTO>> getCommentsList ( @CurrentUser Account account, @PathVariable Long postId,
@RequestParam(required = false) Long cursor,
@RequestParam(defaultValue = "10") int pageSize){
public ResponseEntity<ResponseData<CommentsListResponse>> getCommentsList (@CurrentUser Account account, @PathVariable Long postId,
@RequestParam(required = false) Long cursor,
@RequestParam(defaultValue = "10") int pageSize){
try{
return ResponseData.toResponseEntity(ResponseCode.READ_COMMENTS_LIST_SUCCESS, commentService.getCommentsList(account,postId,cursor,pageSize));
}catch (NoSuchElementException e) {
return ResponseData.toResponseEntity(ResponseCode.READ_COMMENT_FAIL, null);
}
}


}


This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package meltingpot.server.comment.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import meltingpot.server.domain.entity.Account;
import meltingpot.server.domain.entity.comment.Comment;
import meltingpot.server.domain.entity.post.Post;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class CommentCreateRequest {
private String content;
private Boolean isAnonymous;
private String imageKey;

public Comment toEntity(Post post, Account account, Comment parentComment){
return Comment.builder()
.content(content)
.isAnonymous(isAnonymous)
.post(post)
.account(account)
.parent(parentComment)
.build();
}
}
27 changes: 0 additions & 27 deletions src/main/java/meltingpot/server/comment/dto/CommentRequestDTO.java

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package meltingpot.server.comment.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import meltingpot.server.domain.entity.comment.Comment;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Getter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class CommentsListResponse {
private List<CommentDetail> commentsList;
private Long nextCursor;
private Boolean isLast;

@Getter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public static class CommentDetail {
private Long commentId;
private Long parentId;
private Long userId;
private String content;
private String name;
private Boolean isAnonymous;
private String imageUrl;
private LocalDateTime updatedAt;

public static CommentDetail from(Comment comment) {
return CommentDetail.builder()
.commentId(comment.getId())
.parentId(comment.getParent() != null ? comment.getParent().getId() : null)
.userId(comment.getAccount().getId())
.content(comment.getContent())
.name(comment.getAccount().getName())
.isAnonymous(comment.getIsAnonymous())
.imageUrl(comment.getCommentImage() != null ? comment.getCommentImage().getImageUrl() : null)
.updatedAt(comment.getUpdatedAt())
.build();
}
}

public static CommentsListResponse from(List<CommentDetail> commentDetails, Long nextCursor, Boolean isLast) {
return CommentsListResponse.builder()
.commentsList(commentDetails)
.nextCursor(nextCursor)
.isLast(isLast)
.build();
}
}
Loading

0 comments on commit 77580d9

Please sign in to comment.