diff --git a/src/main/java/com/example/onederful/domain/comment/controller/CommentController.java b/src/main/java/com/example/onederful/domain/comment/controller/CommentController.java index 37cc60a..1b17cd2 100644 --- a/src/main/java/com/example/onederful/domain/comment/controller/CommentController.java +++ b/src/main/java/com/example/onederful/domain/comment/controller/CommentController.java @@ -26,10 +26,10 @@ public class CommentController { @PostMapping("/tasks/{task_id}/comments") public ResponseEntity save(@PathVariable Long task_id, HttpServletRequest httpServletRequest, @RequestBody CommentRequestDto requestDto){ - CreateCommentResponseDataDto createCommentResponseDataDto = + CommentResponseDataDto CommentResponseDataDto = commentService.save(task_id, httpServletRequest, requestDto.getContent()); - ApiResponseDto success = ApiResponseDto.success("댓글이 생성되었습니다,", createCommentResponseDataDto); + ApiResponseDto success = ApiResponseDto.success("댓글이 생성되었습니다,", CommentResponseDataDto); return ResponseEntity.status(HttpStatus.OK).body(success); } @@ -40,28 +40,16 @@ public ResponseEntity updateComment( @PathVariable Long task_id, @PathVariable Long comment_id, @RequestBody CommentRequestDto requestDto, HttpServletRequest httpServletRequest ) { - UpdateCommentResponseDataDto updateCommentResponseDataDto = + CommentResponseDataDto CommentResponseDataDto = commentService.updateComment(task_id, comment_id, requestDto.getContent(), httpServletRequest); - ApiResponseDto success = ApiResponseDto.success("댓글이 수정되었습니다.", updateCommentResponseDataDto); + ApiResponseDto success = ApiResponseDto.success("댓글이 수정되었습니다.", CommentResponseDataDto); return ResponseEntity.status(HttpStatus.OK).body(success); } - // 테스크별 댓글 조회 + // 댓글 조회 (테스크별) @GetMapping("/tasks/{task_id}/comments") - public ResponseEntity findAllCommentByTaskId( - @PathVariable Long task_id) { - - List commentResponseDataDtoList = commentService.findAllCommentByTaskId(task_id); - - ApiResponseDto success = ApiResponseDto.success("task " + task_id + "에 달린 댓글 목록", commentResponseDataDtoList); - - return ResponseEntity.status(HttpStatus.OK).body(success); - } - - // 테스크별 댓글 조회 페이지화 - @GetMapping("/tasks/{task_id}/comments/pages") public ResponseEntity findAllCommentByTaskIdInPage( @PathVariable Long task_id, @RequestParam(defaultValue = "0") int page, @@ -70,12 +58,12 @@ public ResponseEntity findAllCommentByTaskIdInPage( Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "createdAt")); Page commentResponseDtoInPage = commentService.findAllCommentByTaskIdInPage(task_id,pageable); - ApiResponseDto success = ApiResponseDto.success("task " + task_id + "에 달린 댓글 목록", commentResponseDtoInPage); + ApiResponseDto success = ApiResponseDto.success("댓글 목록을 조회했습니다.", commentResponseDtoInPage); return ResponseEntity.status(HttpStatus.OK).body(success); } - // 내용으로 댓글 조회 + // 댓글 조회 (내용 검색) @GetMapping("/search") public ResponseEntity findCommentByContent(@RequestBody CommentRequestDto requestDto) { diff --git a/src/main/java/com/example/onederful/domain/comment/dto/CommentResponseDataDto.java b/src/main/java/com/example/onederful/domain/comment/dto/CommentResponseDataDto.java index 6833feb..5800071 100644 --- a/src/main/java/com/example/onederful/domain/comment/dto/CommentResponseDataDto.java +++ b/src/main/java/com/example/onederful/domain/comment/dto/CommentResponseDataDto.java @@ -1,33 +1,44 @@ package com.example.onederful.domain.comment.dto; import com.example.onederful.domain.comment.entity.Comment; +import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Getter; -import java.time.LocalDate; import java.time.LocalDateTime; +@Builder +@AllArgsConstructor @Getter public class CommentResponseDataDto { - private final String writer; + private final Long id; private final String content; - private final Long comment_id; + private final Long taskId; + private final Long userId; + private final UserData user; private final LocalDateTime createdAt; private final LocalDateTime updatedAt; - - public CommentResponseDataDto(Long comment_id, String writer, String content, LocalDateTime createdAt, LocalDateTime updatedAt) { - this.comment_id = comment_id; - this.writer = writer; - this.content = content; - this.createdAt = createdAt; - this.updatedAt = updatedAt; + public static CommentResponseDataDto of(Comment comment){ + return CommentResponseDataDto.builder() + .id(comment.getId()) + .content(comment.getContent()) + .taskId(comment.getTask().getId()) + .userId(comment.getUser().getId()) + .user(UserData.of(comment.getUser())) + .createdAt(comment.getCreatedAt()) + .updatedAt(comment.getUpdatedAt()) + .build(); } + public static CommentResponseDataDto from(Comment comment){ return new CommentResponseDataDto( comment.getId(), - comment.getUser().getName(), comment.getContent(), + comment.getTask().getId(), + comment.getUser().getId(), + UserData.of(comment.getUser()), comment.getCreatedAt(), comment.getUpdatedAt() ); diff --git a/src/main/java/com/example/onederful/domain/comment/dto/CreateCommentResponseDataDto.java b/src/main/java/com/example/onederful/domain/comment/dto/CreateCommentResponseDataDto.java deleted file mode 100644 index e04c8e3..0000000 --- a/src/main/java/com/example/onederful/domain/comment/dto/CreateCommentResponseDataDto.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.example.onederful.domain.comment.dto; - -import lombok.Getter; - -@Getter -public class CreateCommentResponseDataDto { - - private final Long id; - private final String writer; - private final String content; - - public CreateCommentResponseDataDto(Long id, String writer, String content){ - this.id = id; - this.writer = writer; - this.content = content; - } - -} diff --git a/src/main/java/com/example/onederful/domain/comment/dto/ResponseDto.java b/src/main/java/com/example/onederful/domain/comment/dto/ResponseDto.java deleted file mode 100644 index f9e81cc..0000000 --- a/src/main/java/com/example/onederful/domain/comment/dto/ResponseDto.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.example.onederful.domain.comment.dto; - -import lombok.Getter; - -import java.time.OffsetDateTime; - -@Getter -public class ResponseDto { - private final boolean success; - private final String message; - private final T dataDto; - private final OffsetDateTime timestamp; - - public ResponseDto(boolean success, String message, T dataDto) { - this.success = success; - this.message = message; - this.dataDto = dataDto; - this.timestamp = OffsetDateTime.now(); - } - - public static ResponseDto success(String message, T data){ - return new ResponseDto<>(true, message, data); - } - - public static ResponseDto fail(String message){ - return new ResponseDto<>(false, message, null); - } -} diff --git a/src/main/java/com/example/onederful/domain/comment/dto/UpdateCommentResponseDataDto.java b/src/main/java/com/example/onederful/domain/comment/dto/UpdateCommentResponseDataDto.java deleted file mode 100644 index 9e49201..0000000 --- a/src/main/java/com/example/onederful/domain/comment/dto/UpdateCommentResponseDataDto.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.example.onederful.domain.comment.dto; - -import lombok.Getter; -import org.springframework.cglib.core.Local; - -import java.time.LocalDateTime; - -@Getter -public class UpdateCommentResponseDataDto { - - private final Long id; - private final String writer; - private final String content; - private final LocalDateTime created_at; - private final LocalDateTime updated_at; - - - public UpdateCommentResponseDataDto(Long id, String writer, String content, LocalDateTime createdAt, LocalDateTime updatedAt) { - this.id = id; - this.writer = writer; - this.content = content; - this.created_at = createdAt; - this.updated_at = updatedAt; - } -} diff --git a/src/main/java/com/example/onederful/domain/comment/dto/UserData.java b/src/main/java/com/example/onederful/domain/comment/dto/UserData.java new file mode 100644 index 0000000..8968bcf --- /dev/null +++ b/src/main/java/com/example/onederful/domain/comment/dto/UserData.java @@ -0,0 +1,27 @@ +package com.example.onederful.domain.comment.dto; + + +import com.example.onederful.domain.user.entity.User; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; + +@Getter +@Builder +@AllArgsConstructor +public class UserData { + + private Long id; + private String username; + private String name; + private String email; + + public static UserData of(User user) { + return UserData.builder() + .id(user.getId()) + .username(user.getUsername()) + .name(user.getName()) + .email(user.getEmail()) + .build(); + } +} diff --git a/src/main/java/com/example/onederful/domain/comment/service/CommentService.java b/src/main/java/com/example/onederful/domain/comment/service/CommentService.java index d28c7c8..4bc59cc 100644 --- a/src/main/java/com/example/onederful/domain/comment/service/CommentService.java +++ b/src/main/java/com/example/onederful/domain/comment/service/CommentService.java @@ -1,9 +1,6 @@ package com.example.onederful.domain.comment.service; import com.example.onederful.domain.comment.dto.CommentResponseDataDto; -import com.example.onederful.domain.comment.dto.CreateCommentResponseDataDto; -import com.example.onederful.domain.comment.dto.ResponseDto; -import com.example.onederful.domain.comment.dto.UpdateCommentResponseDataDto; import com.example.onederful.domain.comment.entity.Comment; import com.example.onederful.domain.comment.repository.CommentRepository; import com.example.onederful.domain.task.entity.Task; @@ -32,7 +29,8 @@ public class CommentService { private final TaskRepository taskRepository; private final JwtUtil jwtUtil; - public CreateCommentResponseDataDto save(Long task_id, HttpServletRequest httpServletRequest, String content) { + // 댓글 생성 + public CommentResponseDataDto save(Long task_id, HttpServletRequest httpServletRequest, String content) { // 토큰에서 Id 가져오기 Long user_id = jwtUtil.extractId(httpServletRequest); @@ -44,19 +42,20 @@ public CreateCommentResponseDataDto save(Long task_id, HttpServletRequest httpSe .orElseThrow(() -> new CustomException(ErrorCode.NONEXISTENT_TASK)); Comment comment = new Comment(content, user, task); - Comment savedComment = commentRepository.save(comment); - return new CreateCommentResponseDataDto(savedComment.getId(), user.getName(), savedComment.getContent()); + Comment savedComment = commentRepository.save(comment); + return CommentResponseDataDto.of(savedComment); } + // 댓글 수정 @Transactional - public UpdateCommentResponseDataDto updateComment(Long task_id, Long comment_id, String content, HttpServletRequest httpServletRequest) { + public CommentResponseDataDto updateComment(Long task_id, Long comment_id, String content, HttpServletRequest httpServletRequest) { Task task = taskRepository.findById(task_id) .orElseThrow(() -> new CustomException(ErrorCode.NONEXISTENT_TASK)); Comment comment = commentRepository.findById(comment_id) - .orElseThrow(() ->new CustomException(ErrorCode.NONEXISTENT_COMMENT)); + .orElseThrow(() -> new CustomException(ErrorCode.NONEXISTENT_COMMENT)); // 토큰에서 Id 가져오기 Long userId = jwtUtil.extractId(httpServletRequest); @@ -64,38 +63,27 @@ public UpdateCommentResponseDataDto updateComment(Long task_id, Long comment_id, User user = userRepository.findById(userId) .orElseThrow(() -> new CustomException(ErrorCode.NONEXISTENT_USER)); - if(comment.getIsDeleted()==true){ + if (comment.getIsDeleted() == true) { throw new CustomException(ErrorCode.INVALID_COMMENT); } comment.update(content); - return new UpdateCommentResponseDataDto(comment.getId(), user.getName(), comment.getContent(),comment.getCreatedAt(), comment.getUpdatedAt()); - } - - public List findAllCommentByTaskId(Long task_id){ - Task task = taskRepository.findById(task_id) - .orElseThrow(() -> new CustomException(ErrorCode.NONEXISTENT_TASK)); - - List commentListById = commentRepository.findAllByTaskIdOrderByCreatedAtDesc(task_id); - - return commentListById.stream() - .filter(comment -> !comment.getIsDeleted()) - .map(CommentResponseDataDto::from) - .collect(Collectors.toList()); + return CommentResponseDataDto.of(comment); } - public Page findAllCommentByTaskIdInPage(Long task_id, Pageable pageable){ + // 댓글 조회 (테스크별) + public Page findAllCommentByTaskIdInPage(Long task_id, Pageable pageable) { // 페이징 대상 조회 final Page commentListByIdInPage = commentRepository.findByTaskIdAndIsDeletedFalse(task_id, pageable); return commentListByIdInPage.map(CommentResponseDataDto::from); } - - public List findCommentByContent(String content){ + // 댓글 조회 (내용 검색) + public List findCommentByContent(String content) { // 찾는 내용을 댓글을 적은 사람과 댓글 내용에서 검색 - List commentListByContent = commentRepository.findByContentOrUsername("%"+content+"%"); + List commentListByContent = commentRepository.findByContentOrUsername("%" + content + "%"); return commentListByContent.stream() .filter(comment -> !comment.getIsDeleted()) @@ -104,10 +92,11 @@ public List findCommentByContent(String content){ } + // 댓글 삭제 @Transactional - public void deleteComment(Long commentId){ + public void deleteComment(Long commentId) { Comment comment = commentRepository.findById(commentId) - .orElseThrow(() ->new CustomException(ErrorCode.NONEXISTENT_COMMENT)); + .orElseThrow(() -> new CustomException(ErrorCode.NONEXISTENT_COMMENT)); comment.delete(); } diff --git a/src/test/java/com/example/onederful/domain/comment/service/CommentServiceTest.java b/src/test/java/com/example/onederful/domain/comment/service/CommentServiceTest.java index 9d5ba8a..4d8b014 100644 --- a/src/test/java/com/example/onederful/domain/comment/service/CommentServiceTest.java +++ b/src/test/java/com/example/onederful/domain/comment/service/CommentServiceTest.java @@ -1,7 +1,6 @@ package com.example.onederful.domain.comment.service; -import com.example.onederful.domain.comment.dto.CreateCommentResponseDataDto; -import com.example.onederful.domain.comment.dto.UpdateCommentResponseDataDto; +import com.example.onederful.domain.comment.dto.CommentResponseDataDto; import com.example.onederful.domain.comment.entity.Comment; import com.example.onederful.domain.comment.repository.CommentRepository; import com.example.onederful.domain.task.entity.Task; @@ -16,7 +15,6 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; -import org.springframework.http.HttpStatus; import java.time.LocalDateTime; import java.util.Optional; @@ -68,7 +66,7 @@ void save() { given(taskRepository.findById(task_id)).willReturn(Optional.of(task)); given(commentRepository.save(any(Comment.class))).willReturn(comment); // when - CreateCommentResponseDataDto result = commentService.save(task_id, httpServletRequest, content); + CommentResponseDataDto result = commentService.save(task_id, httpServletRequest, content); // then assertThat(result.getContent()).isEqualTo(content); @@ -103,11 +101,11 @@ void updateComment() { given(userRepository.findById(userId)).willReturn(Optional.of(user)); // when - UpdateCommentResponseDataDto result = commentService.updateComment(taskId, commentId, updatedContent, httpServletRequest); + CommentResponseDataDto result = commentService.updateComment(taskId, commentId, updatedContent, httpServletRequest); // then assertThat(result.getContent()).isEqualTo(updatedContent); - assertThat(result.getUpdated_at()).isNotNull(); + assertThat(result.getUpdatedAt()).isNotNull(); }