-
Notifications
You must be signed in to change notification settings - Fork 8
댓글 API 구현 #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
leesewon00
merged 13 commits into
solid-connection:main
from
leesewon00:feat/51-create-comment-api
Aug 15, 2024
Merged
댓글 API 구현 #56
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
454e4aa
feat: 댓글 엔티티 정의
leesewon00 dfbf137
refactor: 댓글 엔티티 폴더 이동
leesewon00 e2637bb
feat: 댓글 레포지토리 로직 추가
leesewon00 5a85aba
feat: 댓글 서비스 로직 추가
leesewon00 55173b0
feat: 댓글 컨트롤러 로직 추가
leesewon00 5703ace
feat: 댓글 관련 DTO 정의
leesewon00 4697d9c
feat: 댓글 관련 예외 정의
leesewon00 5a8c0c1
test: 댓글 레포지토리 테스트 로직 추가
leesewon00 8316167
test: 댓글 서비스 테스트 로직 추가
leesewon00 ef42470
feat: 게시글 API 파라미터 검증 로직 추가
leesewon00 306ec01
refactor: 조회수 갱신 함수 함수명 수정
leesewon00 807246b
refactor: 테스트 클래스 접근제한자 수정
leesewon00 f40d658
refactor: 댓글 생성시 parentId 파라미터 null 허용을 위해 record를 class로 수정
leesewon00 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
62 changes: 62 additions & 0 deletions
62
src/main/java/com/example/solidconnection/comment/controller/CommentController.java
This file contains hidden or 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,62 @@ | ||
| package com.example.solidconnection.comment.controller; | ||
|
|
||
| import com.example.solidconnection.comment.dto.*; | ||
| import com.example.solidconnection.comment.service.CommentService; | ||
| import io.swagger.v3.oas.annotations.security.SecurityRequirement; | ||
| import io.swagger.v3.oas.annotations.security.SecurityRequirements; | ||
| import jakarta.validation.Valid; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| import java.security.Principal; | ||
|
|
||
| import static com.example.solidconnection.config.swagger.SwaggerConfig.ACCESS_TOKEN; | ||
|
|
||
| @RestController | ||
| @RequiredArgsConstructor | ||
| @RequestMapping("/posts") | ||
| @SecurityRequirements | ||
| @SecurityRequirement(name = ACCESS_TOKEN) | ||
| public class CommentController { | ||
|
|
||
| private final CommentService commentService; | ||
|
|
||
| @PostMapping("/{post_id}/comments") | ||
| public ResponseEntity<?> createComment( | ||
| Principal principal, | ||
| @PathVariable("post_id") Long postId, | ||
| @Valid @RequestBody CommentCreateRequest commentCreateRequest | ||
| ) { | ||
|
|
||
| CommentCreateResponse commentCreateResponse = commentService.createComment( | ||
| principal.getName(), postId, commentCreateRequest); | ||
| return ResponseEntity.ok().body(commentCreateResponse); | ||
| } | ||
|
|
||
| @PatchMapping("/{post_id}/comments/{comment_id}") | ||
| public ResponseEntity<?> updateComment( | ||
| Principal principal, | ||
| @PathVariable("post_id") Long postId, | ||
| @PathVariable("comment_id") Long commentId, | ||
| @Valid @RequestBody CommentUpdateRequest commentUpdateRequest | ||
| ) { | ||
|
|
||
| CommentUpdateResponse commentUpdateResponse = commentService.updateComment( | ||
| principal.getName(), postId, commentId, commentUpdateRequest | ||
| ); | ||
| return ResponseEntity.ok().body(commentUpdateResponse); | ||
| } | ||
|
|
||
| @DeleteMapping("/{post_id}/comments/{comment_id}") | ||
| public ResponseEntity<?> deleteCommentById( | ||
| Principal principal, | ||
| @PathVariable("post_id") Long postId, | ||
| @PathVariable("comment_id") Long commentId | ||
| ) { | ||
|
|
||
| CommentDeleteResponse commentDeleteResponse = commentService.deleteCommentById(principal.getName(), postId, commentId); | ||
| return ResponseEntity.ok().body(commentDeleteResponse); | ||
| } | ||
|
|
||
| } |
111 changes: 111 additions & 0 deletions
111
src/main/java/com/example/solidconnection/comment/domain/Comment.java
This file contains hidden or 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,111 @@ | ||
| package com.example.solidconnection.comment.domain; | ||
|
|
||
| import com.example.solidconnection.entity.common.BaseEntity; | ||
| import com.example.solidconnection.post.domain.Post; | ||
| import com.example.solidconnection.siteuser.domain.SiteUser; | ||
| import jakarta.persistence.*; | ||
| import lombok.EqualsAndHashCode; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| @Entity | ||
| @Getter | ||
| @NoArgsConstructor | ||
| @EqualsAndHashCode(of = "id") | ||
| public class Comment extends BaseEntity { | ||
|
|
||
| // for recursive query | ||
| @Transient | ||
| private int level; | ||
|
|
||
| @Transient | ||
| private String path; | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| private Long id; | ||
|
|
||
| @Column(length = 255) | ||
| private String content; | ||
|
|
||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| @JoinColumn(name = "post_id") | ||
| private Post post; | ||
|
|
||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| @JoinColumn(name = "site_user_id") | ||
| private SiteUser siteUser; | ||
|
|
||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| @JoinColumn(name = "parent_id") | ||
| private Comment parentComment; | ||
|
|
||
| @OneToMany(mappedBy = "parentComment", cascade = CascadeType.ALL) | ||
| private List<Comment> commentList = new ArrayList<>(); | ||
|
|
||
| public Comment(String content) { | ||
| this.content = content; | ||
| } | ||
|
|
||
| public void setParentCommentAndPostAndSiteUser(Comment parentComment, Post post, SiteUser siteUser) { | ||
|
|
||
| if (this.parentComment != null) { | ||
| this.parentComment.getCommentList().remove(this); | ||
| } | ||
| this.parentComment = parentComment; | ||
| parentComment.getCommentList().add(this); | ||
|
|
||
| if (this.post != null) { | ||
| this.post.getCommentList().remove(this); | ||
| } | ||
| this.post = post; | ||
| post.getCommentList().add(this); | ||
|
|
||
| if (this.siteUser != null) { | ||
| this.siteUser.getCommentList().remove(this); | ||
| } | ||
| this.siteUser = siteUser; | ||
| siteUser.getCommentList().add(this); | ||
| } | ||
|
|
||
| public void setPostAndSiteUser(Post post, SiteUser siteUser) { | ||
|
|
||
| if (this.post != null) { | ||
| this.post.getCommentList().remove(this); | ||
| } | ||
| this.post = post; | ||
| post.getCommentList().add(this); | ||
|
|
||
| if (this.siteUser != null) { | ||
| this.siteUser.getCommentList().remove(this); | ||
| } | ||
| this.siteUser = siteUser; | ||
| siteUser.getCommentList().add(this); | ||
| } | ||
|
|
||
| public void resetPostAndSiteUserAndParentComment() { | ||
| if (this.post != null) { | ||
| this.post.getCommentList().remove(this); | ||
| this.post = null; | ||
| } | ||
| if (this.siteUser != null) { | ||
| this.siteUser.getCommentList().remove(this); | ||
| this.siteUser = null; | ||
| } | ||
| if (this.parentComment != null) { | ||
| this.parentComment.getCommentList().remove(this); | ||
| this.parentComment = null; | ||
| } | ||
| } | ||
|
|
||
| public void updateContent(String content) { | ||
| this.content = content; | ||
| } | ||
|
|
||
| public void deprecateComment() { | ||
| this.content = null; | ||
| } | ||
| } | ||
39 changes: 39 additions & 0 deletions
39
src/main/java/com/example/solidconnection/comment/dto/CommentCreateRequest.java
This file contains hidden or 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,39 @@ | ||
| package com.example.solidconnection.comment.dto; | ||
|
|
||
| import com.example.solidconnection.comment.domain.Comment; | ||
| import com.example.solidconnection.post.domain.Post; | ||
| import com.example.solidconnection.siteuser.domain.SiteUser; | ||
| import jakarta.annotation.Nullable; | ||
| import jakarta.validation.constraints.NotBlank; | ||
| import jakarta.validation.constraints.Size; | ||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| public class CommentCreateRequest { | ||
|
|
||
| @NotBlank(message = "댓글 내용은 빈 값일 수 없습니다.") | ||
| @Size(min = 1, max = 255, message = "댓글 내용은 최소 1자 이상, 최대 255자 이하여야 합니다.") | ||
| String content; | ||
|
|
||
| @Nullable | ||
| Long parentId; | ||
|
|
||
| public CommentCreateRequest(String content, @Nullable Long parentId) { | ||
| this.content = content; | ||
| this.parentId = parentId; | ||
| } | ||
|
|
||
| public Comment toEntity(SiteUser siteUser, Post post, Comment parentComment) { | ||
|
|
||
| Comment comment = new Comment( | ||
| this.content | ||
| ); | ||
|
|
||
| if (parentComment == null) { | ||
| comment.setPostAndSiteUser(post, siteUser); | ||
| } else { | ||
| comment.setParentCommentAndPostAndSiteUser(parentComment, post, siteUser); | ||
| } | ||
| return comment; | ||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/example/solidconnection/comment/dto/CommentCreateResponse.java
This file contains hidden or 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,14 @@ | ||
| package com.example.solidconnection.comment.dto; | ||
|
|
||
| import com.example.solidconnection.comment.domain.Comment; | ||
|
|
||
| public record CommentCreateResponse( | ||
| Long id | ||
| ) { | ||
|
|
||
| public static CommentCreateResponse from(Comment comment) { | ||
| return new CommentCreateResponse( | ||
| comment.getId() | ||
| ); | ||
| } | ||
| } |
6 changes: 6 additions & 0 deletions
6
src/main/java/com/example/solidconnection/comment/dto/CommentDeleteResponse.java
This file contains hidden or 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,6 @@ | ||
| package com.example.solidconnection.comment.dto; | ||
|
|
||
| public record CommentDeleteResponse( | ||
| Long id | ||
| ) { | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/example/solidconnection/comment/dto/CommentUpdateRequest.java
This file contains hidden or 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,12 @@ | ||
| package com.example.solidconnection.comment.dto; | ||
|
|
||
| import jakarta.validation.constraints.NotBlank; | ||
| import jakarta.validation.constraints.Size; | ||
|
|
||
| public record CommentUpdateRequest( | ||
| @NotBlank(message = "댓글 내용은 빈 값일 수 없습니다.") | ||
| @Size(min = 1, max = 255, message = "댓글 내용은 최소 1자 이상, 최대 255자 이하여야 합니다.") | ||
| String content | ||
| ) { | ||
|
|
||
| } |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/example/solidconnection/comment/dto/CommentUpdateResponse.java
This file contains hidden or 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,14 @@ | ||
| package com.example.solidconnection.comment.dto; | ||
|
|
||
| import com.example.solidconnection.comment.domain.Comment; | ||
|
|
||
| public record CommentUpdateResponse( | ||
| Long id | ||
| ) { | ||
|
|
||
| public static CommentUpdateResponse from(Comment comment) { | ||
| return new CommentUpdateResponse( | ||
| comment.getId() | ||
| ); | ||
| } | ||
| } |
2 changes: 1 addition & 1 deletion
2
src/main/java/com/example/solidconnection/comment/dto/PostFindCommentResponse.java
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[진짜 몰라서 하는 질문] 이렇게 remove 하고 add 하는 부분이 왜 필요한가요? 😲
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
데이터베이스와, 메모리 상태의 일관성을 유지하기 위해 사용하였습니다.
간단한 예시입니다.
기댓값은 1이지만, 0이 출력됩니다.
remove의 경우, 연관관계상 둘 이상에 포함될 수 없어서 사용하였습니다.
추천키워드: jpa 연관관계 편의 메소드
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아하!
기존의 연관 관계를 끊기 위해서군요 ㅎㅎ 이해 되었습니다~