-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from solid-connection/main
[RELEASE]댓글, 게시글 좋아요 기능, 지원서 3차 지망 대학
- Loading branch information
Showing
45 changed files
with
1,715 additions
and
130 deletions.
There are no files selected for viewing
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
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
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
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
62 changes: 62 additions & 0 deletions
62
src/main/java/com/example/solidconnection/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,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 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; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/example/solidconnection/comment/dto/CommentCreateRequest.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,28 @@ | ||
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.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.Size; | ||
|
||
public record CommentCreateRequest( | ||
@NotBlank(message = "댓글 내용은 빈 값일 수 없습니다.") | ||
@Size(min = 1, max = 255, message = "댓글 내용은 최소 1자 이상, 최대 255자 이하여야 합니다.") | ||
String content, | ||
Long 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 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 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 | ||
) { | ||
} |
Oops, something went wrong.