Skip to content

Commit

Permalink
Merge pull request #26 from CMC-Hackathon-team5/feat/#21-comment-domain
Browse files Browse the repository at this point in the history
Feat/#21 comment domain
  • Loading branch information
kkyu0718 authored Jan 7, 2023
2 parents b41d15a + 76c5f47 commit c2af9b4
Show file tree
Hide file tree
Showing 10 changed files with 218 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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<ApiResponse> 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<ApiResponse<List<CommentResponseDto>>> getComments(@RequestBody GetCommentsRequestDto getCommentsRequestDto) {
Long diaryId = getCommentsRequestDto.getDiaryId();

List<CommentResponseDto> comments = commentService.getComments(diaryId);

return ResponseEntity.status(HttpStatus.OK).body(new ApiResponse<>(ResponseCode.GET_COMMENTS_SUCCESS, comments));
}

@Operation(summary = "댓글 수정", description = "댓글을 수정하는 메소드입니다.")
@PutMapping("/{id}")
public ResponseEntity<ApiResponse> 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<ApiResponse> deleteComment(@PathVariable("id") Long commentId){
commentService.deleteComment(commentId);
return ResponseEntity.status(HttpStatus.OK).body(new ApiResponse(ResponseCode.COMMENT_DELETED));
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.cmc.selfdevelopment.domain.comment.dto;

import lombok.Getter;

@Getter
public class CreateCommentRequestDto {
private String content;
private Long diaryId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.cmc.selfdevelopment.domain.comment.dto;

import lombok.Getter;

@Getter
public class GetCommentsRequestDto {
private Long diaryId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.cmc.selfdevelopment.domain.comment.dto;

import lombok.Getter;

@Getter
public class UpdateCommentRequestDto {
private String content;
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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<Comment, Long> {
List<Comment> findByDiaryId(Long diaryId);
}
Original file line number Diff line number Diff line change
@@ -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<CommentResponseDto> getComments(Long diaryId) {
List<Comment> comments = commentRepository.findByDiaryId(diaryId);
List<CommentResponseDto> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}

0 comments on commit c2af9b4

Please sign in to comment.