-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Feat/#21 comment domain
- Loading branch information
Showing
10 changed files
with
218 additions
and
1 deletion.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
src/main/java/com/cmc/selfdevelopment/domain/comment/controller/CommentController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/cmc/selfdevelopment/domain/comment/dto/CommentResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/cmc/selfdevelopment/domain/comment/dto/CreateCommentRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/cmc/selfdevelopment/domain/comment/dto/GetCommentsRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/cmc/selfdevelopment/domain/comment/dto/UpdateCommentRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/cmc/selfdevelopment/domain/comment/entity/Comment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/cmc/selfdevelopment/domain/comment/repository/CommentRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
76 changes: 76 additions & 0 deletions
76
src/main/java/com/cmc/selfdevelopment/domain/comment/service/CommentService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters