diff --git a/src/main/java/com/cmc/selfdevelopment/domain/comment/controller/CommentController.java b/src/main/java/com/cmc/selfdevelopment/domain/comment/controller/CommentController.java new file mode 100644 index 0000000..9b64af7 --- /dev/null +++ b/src/main/java/com/cmc/selfdevelopment/domain/comment/controller/CommentController.java @@ -0,0 +1,63 @@ +package com.cmc.selfdevelopment.domain.comment.controller; + +import com.cmc.selfdevelopment.domain.comment.dto.CommentResponseDto; +import com.cmc.selfdevelopment.domain.comment.dto.CreateCommentRequestDto; +import com.cmc.selfdevelopment.domain.comment.dto.GetCommentsRequestDto; +import com.cmc.selfdevelopment.domain.comment.dto.UpdateCommentRequestDto; +import com.cmc.selfdevelopment.domain.comment.service.CommentService; +import com.cmc.selfdevelopment.global.common.api.ApiResponse; +import com.cmc.selfdevelopment.global.common.api.ResponseCode; +import io.swagger.v3.oas.annotations.Operation; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@Slf4j +@RestController +@RequiredArgsConstructor +@RequestMapping("/api/comment") +public class CommentController { + private final CommentService commentService; + + @Operation(summary = "댓글 생성", description = "댓글을 생성하는 메소드입니다.") + @PostMapping + public ResponseEntity createComment(@RequestBody CreateCommentRequestDto createCommentRequestDto) { + // TODO: userId 받아오는 메소드 연결 + Long userId = 1L; + + Long diaryId = createCommentRequestDto.getDiaryId(); + String content = createCommentRequestDto.getContent(); + + commentService.createComment(userId, diaryId, content); + return ResponseEntity.status(HttpStatus.CREATED).body(new ApiResponse(ResponseCode.COMMENT_CREATED)); + } + + @Operation(summary = "댓글 전체 조회", description = "회고에 해당하는 댓글 전체 조회하는 메소드입니다.") + @GetMapping + public ResponseEntity>> getComments(@RequestBody GetCommentsRequestDto getCommentsRequestDto) { + Long diaryId = getCommentsRequestDto.getDiaryId(); + + List comments = commentService.getComments(diaryId); + + return ResponseEntity.status(HttpStatus.OK).body(new ApiResponse<>(ResponseCode.GET_COMMENTS_SUCCESS, comments)); + } + + @Operation(summary = "댓글 수정", description = "댓글을 수정하는 메소드입니다.") + @PutMapping("/{id}") + public ResponseEntity updateComment(@PathVariable("id") Long commentId, @RequestBody UpdateCommentRequestDto updateCommentRequestDto) { + String content = updateCommentRequestDto.getContent(); + commentService.updateComment(commentId, content); + return ResponseEntity.status(HttpStatus.OK).body(new ApiResponse<>(ResponseCode.COMMENT_UPDATED)); + } + + @Operation(summary = "댓글 삭제", description = "댓글을 삭제하는 메소드입니다.") + @DeleteMapping("/{id}") + public ResponseEntity deleteComment(@PathVariable("id") Long commentId){ + commentService.deleteComment(commentId); + return ResponseEntity.status(HttpStatus.OK).body(new ApiResponse(ResponseCode.COMMENT_DELETED)); + } +} diff --git a/src/main/java/com/cmc/selfdevelopment/domain/comment/dto/CommentResponseDto.java b/src/main/java/com/cmc/selfdevelopment/domain/comment/dto/CommentResponseDto.java new file mode 100644 index 0000000..145cdc1 --- /dev/null +++ b/src/main/java/com/cmc/selfdevelopment/domain/comment/dto/CommentResponseDto.java @@ -0,0 +1,13 @@ +package com.cmc.selfdevelopment.domain.comment.dto; + +import lombok.Builder; +import lombok.Getter; + +@Getter +@Builder +public class CommentResponseDto { + private Long id; + private Long userId; + private Long diaryId; + private String content; +} diff --git a/src/main/java/com/cmc/selfdevelopment/domain/comment/dto/CreateCommentRequestDto.java b/src/main/java/com/cmc/selfdevelopment/domain/comment/dto/CreateCommentRequestDto.java new file mode 100644 index 0000000..284cfe8 --- /dev/null +++ b/src/main/java/com/cmc/selfdevelopment/domain/comment/dto/CreateCommentRequestDto.java @@ -0,0 +1,9 @@ +package com.cmc.selfdevelopment.domain.comment.dto; + +import lombok.Getter; + +@Getter +public class CreateCommentRequestDto { + private String content; + private Long diaryId; +} diff --git a/src/main/java/com/cmc/selfdevelopment/domain/comment/dto/GetCommentsRequestDto.java b/src/main/java/com/cmc/selfdevelopment/domain/comment/dto/GetCommentsRequestDto.java new file mode 100644 index 0000000..44884a7 --- /dev/null +++ b/src/main/java/com/cmc/selfdevelopment/domain/comment/dto/GetCommentsRequestDto.java @@ -0,0 +1,8 @@ +package com.cmc.selfdevelopment.domain.comment.dto; + +import lombok.Getter; + +@Getter +public class GetCommentsRequestDto { + private Long diaryId; +} diff --git a/src/main/java/com/cmc/selfdevelopment/domain/comment/dto/UpdateCommentRequestDto.java b/src/main/java/com/cmc/selfdevelopment/domain/comment/dto/UpdateCommentRequestDto.java new file mode 100644 index 0000000..b9906c7 --- /dev/null +++ b/src/main/java/com/cmc/selfdevelopment/domain/comment/dto/UpdateCommentRequestDto.java @@ -0,0 +1,8 @@ +package com.cmc.selfdevelopment.domain.comment.dto; + +import lombok.Getter; + +@Getter +public class UpdateCommentRequestDto { + private String content; +} diff --git a/src/main/java/com/cmc/selfdevelopment/domain/comment/entity/Comment.java b/src/main/java/com/cmc/selfdevelopment/domain/comment/entity/Comment.java new file mode 100644 index 0000000..8078600 --- /dev/null +++ b/src/main/java/com/cmc/selfdevelopment/domain/comment/entity/Comment.java @@ -0,0 +1,25 @@ +package com.cmc.selfdevelopment.domain.comment.entity; + +import com.cmc.selfdevelopment.domain.diary.entity.Diary; +import com.cmc.selfdevelopment.domain.user.entity.User; +import com.cmc.selfdevelopment.global.common.entity.BaseEntity; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import lombok.Setter; +import lombok.experimental.SuperBuilder; + +import javax.persistence.Entity; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.Table; + +@Getter +@Table(name = "Comment") +@SuperBuilder +@RequiredArgsConstructor +@Entity +public class Comment extends BaseEntity { + @ManyToOne @Setter @JoinColumn(name = "user_id", nullable = false) private User user; + @ManyToOne @Setter @JoinColumn(name = "diary_id", nullable = false) private Diary diary; + @Setter private String content; +} diff --git a/src/main/java/com/cmc/selfdevelopment/domain/comment/repository/CommentRepository.java b/src/main/java/com/cmc/selfdevelopment/domain/comment/repository/CommentRepository.java new file mode 100644 index 0000000..9c15655 --- /dev/null +++ b/src/main/java/com/cmc/selfdevelopment/domain/comment/repository/CommentRepository.java @@ -0,0 +1,10 @@ +package com.cmc.selfdevelopment.domain.comment.repository; + +import com.cmc.selfdevelopment.domain.comment.entity.Comment; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.List; + +public interface CommentRepository extends JpaRepository { + List findByDiaryId(Long diaryId); +} diff --git a/src/main/java/com/cmc/selfdevelopment/domain/comment/service/CommentService.java b/src/main/java/com/cmc/selfdevelopment/domain/comment/service/CommentService.java new file mode 100644 index 0000000..dcb51de --- /dev/null +++ b/src/main/java/com/cmc/selfdevelopment/domain/comment/service/CommentService.java @@ -0,0 +1,76 @@ +package com.cmc.selfdevelopment.domain.comment.service; + +import com.cmc.selfdevelopment.domain.comment.dto.CommentResponseDto; +import com.cmc.selfdevelopment.domain.comment.entity.Comment; +import com.cmc.selfdevelopment.domain.comment.repository.CommentRepository; +import com.cmc.selfdevelopment.domain.diary.entity.Diary; +import com.cmc.selfdevelopment.domain.diary.repository.DiaryRepository; +import com.cmc.selfdevelopment.domain.user.entity.User; +import com.cmc.selfdevelopment.domain.user.repository.UserRepository; +import com.cmc.selfdevelopment.global.common.api.ErrorCode; +import com.cmc.selfdevelopment.global.common.exception.CustomException; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; +import java.util.stream.Collectors; + +@Slf4j +@RequiredArgsConstructor +@Transactional +@Service +public class CommentService { + private final CommentRepository commentRepository; + private final UserRepository userRepository; + private final DiaryRepository diaryRepository; + + @Transactional + public void createComment(Long userId, Long diaryId, String content) { + User user = userRepository.findById(userId) + .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); + + Diary diary = diaryRepository.findById(diaryId) + .orElseThrow(() -> new CustomException(ErrorCode.DIARY_NOT_FOUND)); + + Comment comment = Comment.builder() + .user(user) + .diary(diary) + .content(content) + .build(); + + commentRepository.save(comment); + return; + } + + public List getComments(Long diaryId) { + List comments = commentRepository.findByDiaryId(diaryId); + List commentResponseDtos = comments.stream().map((comment -> + CommentResponseDto.builder() + .id(comment.getId()) + .userId(comment.getUser().getId()) + .diaryId(comment.getDiary().getId()) + .content(comment.getContent()) + .build()) + ).collect(Collectors.toList()); + return commentResponseDtos; + } + + @Transactional + public void updateComment(Long commentId, String content) { + Comment comment = commentRepository.findById(commentId) + .orElseThrow(() -> new CustomException(ErrorCode.COMMENT_NOT_FOUND)); + comment.setContent(content); + commentRepository.save(comment); + return; + } + + @Transactional + public void deleteComment(Long commentId) { + Comment comment = commentRepository.findById(commentId) + .orElseThrow(() -> new CustomException(ErrorCode.COMMENT_NOT_FOUND)); + commentRepository.delete(comment); + return; + } +} diff --git a/src/main/java/com/cmc/selfdevelopment/global/common/api/ErrorCode.java b/src/main/java/com/cmc/selfdevelopment/global/common/api/ErrorCode.java index ab0fd8d..a1af9ef 100644 --- a/src/main/java/com/cmc/selfdevelopment/global/common/api/ErrorCode.java +++ b/src/main/java/com/cmc/selfdevelopment/global/common/api/ErrorCode.java @@ -22,11 +22,11 @@ public enum ErrorCode { DIARY_NOT_FOUND(HttpStatus.NOT_FOUND, "존재하지 않는 계획입니다."), IMPROVEMENT_NOT_FOUND(HttpStatus.NOT_FOUND, "존재하지 않는 자기 계발입니다."), IMPROVEMENT_DUPLICATED(HttpStatus.CONFLICT, "이미 존재하는 자기 계발입니다."), + COMMENT_NOT_FOUND(HttpStatus.NOT_FOUND, "존재하지 않는 댓글입니다."); TODO_NOT_FOUND(HttpStatus.NOT_FOUND, "존재하지 않는 todo입니다."), TODO_DUPLICATED(HttpStatus.CONFLICT, "이미 존재하는 자기 todo입니다."), ; - private final HttpStatus status; private final String message; diff --git a/src/main/java/com/cmc/selfdevelopment/global/common/api/ResponseCode.java b/src/main/java/com/cmc/selfdevelopment/global/common/api/ResponseCode.java index 77f50fa..8a2b4b0 100644 --- a/src/main/java/com/cmc/selfdevelopment/global/common/api/ResponseCode.java +++ b/src/main/java/com/cmc/selfdevelopment/global/common/api/ResponseCode.java @@ -19,10 +19,15 @@ public enum ResponseCode { DIARY_UPDATED(HttpStatus.OK, "회고 수정에 성공하였습니다."), GET_DIARY(HttpStatus.OK, "회고 조회에 성공하였습니다."), DIARY_DELETED(HttpStatus.OK, "회고 삭제에 성공하였습니다." ), + COMMENT_CREATED(HttpStatus.OK, "댓글 생성에 성공하였습니다."), + GET_COMMENTS_SUCCESS(HttpStatus.OK, "댓글 전체 조회에 성공하였습니다."), + COMMENT_UPDATED(HttpStatus.CREATED, "댓글 수정에 성공하였습니다."), + COMMENT_DELETED(HttpStatus.OK, "댓글 삭제에 성공하였습니다."); TODO_CHANGE(HttpStatus.OK, "isDone 변경에 성공했습니다."), TODO_LIST(HttpStatus.OK, "해당 날짜의 Todo 리스트입니다"), TODO_MONTH_PERCENT(HttpStatus.OK, "특정 달의 TODO 성취도 입니다") ; private final HttpStatus status; private final String message; + }