Skip to content

Commit c2af9b4

Browse files
authored
Merge pull request #26 from CMC-Hackathon-team5/feat/#21-comment-domain
Feat/#21 comment domain
2 parents b41d15a + 76c5f47 commit c2af9b4

File tree

10 files changed

+218
-1
lines changed

10 files changed

+218
-1
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.cmc.selfdevelopment.domain.comment.controller;
2+
3+
import com.cmc.selfdevelopment.domain.comment.dto.CommentResponseDto;
4+
import com.cmc.selfdevelopment.domain.comment.dto.CreateCommentRequestDto;
5+
import com.cmc.selfdevelopment.domain.comment.dto.GetCommentsRequestDto;
6+
import com.cmc.selfdevelopment.domain.comment.dto.UpdateCommentRequestDto;
7+
import com.cmc.selfdevelopment.domain.comment.service.CommentService;
8+
import com.cmc.selfdevelopment.global.common.api.ApiResponse;
9+
import com.cmc.selfdevelopment.global.common.api.ResponseCode;
10+
import io.swagger.v3.oas.annotations.Operation;
11+
import lombok.RequiredArgsConstructor;
12+
import lombok.extern.slf4j.Slf4j;
13+
import org.springframework.http.HttpStatus;
14+
import org.springframework.http.ResponseEntity;
15+
import org.springframework.web.bind.annotation.*;
16+
17+
import java.util.List;
18+
19+
@Slf4j
20+
@RestController
21+
@RequiredArgsConstructor
22+
@RequestMapping("/api/comment")
23+
public class CommentController {
24+
private final CommentService commentService;
25+
26+
@Operation(summary = "댓글 생성", description = "댓글을 생성하는 메소드입니다.")
27+
@PostMapping
28+
public ResponseEntity<ApiResponse> createComment(@RequestBody CreateCommentRequestDto createCommentRequestDto) {
29+
// TODO: userId 받아오는 메소드 연결
30+
Long userId = 1L;
31+
32+
Long diaryId = createCommentRequestDto.getDiaryId();
33+
String content = createCommentRequestDto.getContent();
34+
35+
commentService.createComment(userId, diaryId, content);
36+
return ResponseEntity.status(HttpStatus.CREATED).body(new ApiResponse(ResponseCode.COMMENT_CREATED));
37+
}
38+
39+
@Operation(summary = "댓글 전체 조회", description = "회고에 해당하는 댓글 전체 조회하는 메소드입니다.")
40+
@GetMapping
41+
public ResponseEntity<ApiResponse<List<CommentResponseDto>>> getComments(@RequestBody GetCommentsRequestDto getCommentsRequestDto) {
42+
Long diaryId = getCommentsRequestDto.getDiaryId();
43+
44+
List<CommentResponseDto> comments = commentService.getComments(diaryId);
45+
46+
return ResponseEntity.status(HttpStatus.OK).body(new ApiResponse<>(ResponseCode.GET_COMMENTS_SUCCESS, comments));
47+
}
48+
49+
@Operation(summary = "댓글 수정", description = "댓글을 수정하는 메소드입니다.")
50+
@PutMapping("/{id}")
51+
public ResponseEntity<ApiResponse> updateComment(@PathVariable("id") Long commentId, @RequestBody UpdateCommentRequestDto updateCommentRequestDto) {
52+
String content = updateCommentRequestDto.getContent();
53+
commentService.updateComment(commentId, content);
54+
return ResponseEntity.status(HttpStatus.OK).body(new ApiResponse<>(ResponseCode.COMMENT_UPDATED));
55+
}
56+
57+
@Operation(summary = "댓글 삭제", description = "댓글을 삭제하는 메소드입니다.")
58+
@DeleteMapping("/{id}")
59+
public ResponseEntity<ApiResponse> deleteComment(@PathVariable("id") Long commentId){
60+
commentService.deleteComment(commentId);
61+
return ResponseEntity.status(HttpStatus.OK).body(new ApiResponse(ResponseCode.COMMENT_DELETED));
62+
}
63+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.cmc.selfdevelopment.domain.comment.dto;
2+
3+
import lombok.Builder;
4+
import lombok.Getter;
5+
6+
@Getter
7+
@Builder
8+
public class CommentResponseDto {
9+
private Long id;
10+
private Long userId;
11+
private Long diaryId;
12+
private String content;
13+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.cmc.selfdevelopment.domain.comment.dto;
2+
3+
import lombok.Getter;
4+
5+
@Getter
6+
public class CreateCommentRequestDto {
7+
private String content;
8+
private Long diaryId;
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.cmc.selfdevelopment.domain.comment.dto;
2+
3+
import lombok.Getter;
4+
5+
@Getter
6+
public class GetCommentsRequestDto {
7+
private Long diaryId;
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.cmc.selfdevelopment.domain.comment.dto;
2+
3+
import lombok.Getter;
4+
5+
@Getter
6+
public class UpdateCommentRequestDto {
7+
private String content;
8+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.cmc.selfdevelopment.domain.comment.entity;
2+
3+
import com.cmc.selfdevelopment.domain.diary.entity.Diary;
4+
import com.cmc.selfdevelopment.domain.user.entity.User;
5+
import com.cmc.selfdevelopment.global.common.entity.BaseEntity;
6+
import lombok.Getter;
7+
import lombok.RequiredArgsConstructor;
8+
import lombok.Setter;
9+
import lombok.experimental.SuperBuilder;
10+
11+
import javax.persistence.Entity;
12+
import javax.persistence.JoinColumn;
13+
import javax.persistence.ManyToOne;
14+
import javax.persistence.Table;
15+
16+
@Getter
17+
@Table(name = "Comment")
18+
@SuperBuilder
19+
@RequiredArgsConstructor
20+
@Entity
21+
public class Comment extends BaseEntity {
22+
@ManyToOne @Setter @JoinColumn(name = "user_id", nullable = false) private User user;
23+
@ManyToOne @Setter @JoinColumn(name = "diary_id", nullable = false) private Diary diary;
24+
@Setter private String content;
25+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.cmc.selfdevelopment.domain.comment.repository;
2+
3+
import com.cmc.selfdevelopment.domain.comment.entity.Comment;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
import java.util.List;
7+
8+
public interface CommentRepository extends JpaRepository<Comment, Long> {
9+
List<Comment> findByDiaryId(Long diaryId);
10+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.cmc.selfdevelopment.domain.comment.service;
2+
3+
import com.cmc.selfdevelopment.domain.comment.dto.CommentResponseDto;
4+
import com.cmc.selfdevelopment.domain.comment.entity.Comment;
5+
import com.cmc.selfdevelopment.domain.comment.repository.CommentRepository;
6+
import com.cmc.selfdevelopment.domain.diary.entity.Diary;
7+
import com.cmc.selfdevelopment.domain.diary.repository.DiaryRepository;
8+
import com.cmc.selfdevelopment.domain.user.entity.User;
9+
import com.cmc.selfdevelopment.domain.user.repository.UserRepository;
10+
import com.cmc.selfdevelopment.global.common.api.ErrorCode;
11+
import com.cmc.selfdevelopment.global.common.exception.CustomException;
12+
import lombok.RequiredArgsConstructor;
13+
import lombok.extern.slf4j.Slf4j;
14+
import org.springframework.stereotype.Service;
15+
import org.springframework.transaction.annotation.Transactional;
16+
17+
import java.util.List;
18+
import java.util.stream.Collectors;
19+
20+
@Slf4j
21+
@RequiredArgsConstructor
22+
@Transactional
23+
@Service
24+
public class CommentService {
25+
private final CommentRepository commentRepository;
26+
private final UserRepository userRepository;
27+
private final DiaryRepository diaryRepository;
28+
29+
@Transactional
30+
public void createComment(Long userId, Long diaryId, String content) {
31+
User user = userRepository.findById(userId)
32+
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
33+
34+
Diary diary = diaryRepository.findById(diaryId)
35+
.orElseThrow(() -> new CustomException(ErrorCode.DIARY_NOT_FOUND));
36+
37+
Comment comment = Comment.builder()
38+
.user(user)
39+
.diary(diary)
40+
.content(content)
41+
.build();
42+
43+
commentRepository.save(comment);
44+
return;
45+
}
46+
47+
public List<CommentResponseDto> getComments(Long diaryId) {
48+
List<Comment> comments = commentRepository.findByDiaryId(diaryId);
49+
List<CommentResponseDto> commentResponseDtos = comments.stream().map((comment ->
50+
CommentResponseDto.builder()
51+
.id(comment.getId())
52+
.userId(comment.getUser().getId())
53+
.diaryId(comment.getDiary().getId())
54+
.content(comment.getContent())
55+
.build())
56+
).collect(Collectors.toList());
57+
return commentResponseDtos;
58+
}
59+
60+
@Transactional
61+
public void updateComment(Long commentId, String content) {
62+
Comment comment = commentRepository.findById(commentId)
63+
.orElseThrow(() -> new CustomException(ErrorCode.COMMENT_NOT_FOUND));
64+
comment.setContent(content);
65+
commentRepository.save(comment);
66+
return;
67+
}
68+
69+
@Transactional
70+
public void deleteComment(Long commentId) {
71+
Comment comment = commentRepository.findById(commentId)
72+
.orElseThrow(() -> new CustomException(ErrorCode.COMMENT_NOT_FOUND));
73+
commentRepository.delete(comment);
74+
return;
75+
}
76+
}

src/main/java/com/cmc/selfdevelopment/global/common/api/ErrorCode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ public enum ErrorCode {
2222
DIARY_NOT_FOUND(HttpStatus.NOT_FOUND, "존재하지 않는 계획입니다."),
2323
IMPROVEMENT_NOT_FOUND(HttpStatus.NOT_FOUND, "존재하지 않는 자기 계발입니다."),
2424
IMPROVEMENT_DUPLICATED(HttpStatus.CONFLICT, "이미 존재하는 자기 계발입니다."),
25+
COMMENT_NOT_FOUND(HttpStatus.NOT_FOUND, "존재하지 않는 댓글입니다.");
2526
TODO_NOT_FOUND(HttpStatus.NOT_FOUND, "존재하지 않는 todo입니다."),
2627
TODO_DUPLICATED(HttpStatus.CONFLICT, "이미 존재하는 자기 todo입니다."),
2728
;
2829

29-
3030
private final HttpStatus status;
3131
private final String message;
3232

src/main/java/com/cmc/selfdevelopment/global/common/api/ResponseCode.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,15 @@ public enum ResponseCode {
1919
DIARY_UPDATED(HttpStatus.OK, "회고 수정에 성공하였습니다."),
2020
GET_DIARY(HttpStatus.OK, "회고 조회에 성공하였습니다."),
2121
DIARY_DELETED(HttpStatus.OK, "회고 삭제에 성공하였습니다." ),
22+
COMMENT_CREATED(HttpStatus.OK, "댓글 생성에 성공하였습니다."),
23+
GET_COMMENTS_SUCCESS(HttpStatus.OK, "댓글 전체 조회에 성공하였습니다."),
24+
COMMENT_UPDATED(HttpStatus.CREATED, "댓글 수정에 성공하였습니다."),
25+
COMMENT_DELETED(HttpStatus.OK, "댓글 삭제에 성공하였습니다.");
2226
TODO_CHANGE(HttpStatus.OK, "isDone 변경에 성공했습니다."),
2327
TODO_LIST(HttpStatus.OK, "해당 날짜의 Todo 리스트입니다"),
2428
TODO_MONTH_PERCENT(HttpStatus.OK, "특정 달의 TODO 성취도 입니다")
2529
;
2630
private final HttpStatus status;
2731
private final String message;
32+
2833
}

0 commit comments

Comments
 (0)