-
Notifications
You must be signed in to change notification settings - Fork 20
[4주차] 남유정/[feat] 게시물/댓글/신고 도메인 기반 기능 설계 및 API 구현 #166
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
Open
N-yujeong
wants to merge
2
commits into
Leets-Official:남유정/main
Choose a base branch
from
N-yujeong:남유정/4주차
base: 남유정/main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
The head ref may contain hidden characters: "\uB0A8\uC720\uC815/4\uC8FC\uCC28"
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "permissions": { | ||
| "allow": [ | ||
| "Bash(./gradlew compileJava)" | ||
| ] | ||
| } | ||
| } |
40 changes: 40 additions & 0 deletions
40
src/main/java/com/example/leets7th/domain/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,40 @@ | ||
| package com.example.leets7th.domain.comment.controller; | ||
|
|
||
| import com.example.leets7th.domain.comment.dto.CommentCreateRequest; | ||
| import com.example.leets7th.domain.comment.service.CommentService; | ||
| import com.example.leets7th.global.common.ApiResponse; | ||
| import jakarta.validation.Valid; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/api/posts/{postId}/comments") | ||
| @RequiredArgsConstructor | ||
| public class CommentController implements CommentControllerDocs { | ||
|
|
||
| private final CommentService commentService; | ||
|
|
||
| @PostMapping | ||
| public ResponseEntity<ApiResponse<Map<String, Object>>> createComment( | ||
| @PathVariable Long postId, | ||
| @RequestBody @Valid CommentCreateRequest request, | ||
| @RequestHeader(value = "X-User-Id", defaultValue = "1") Long userId | ||
| ) { | ||
| Long commentId = commentService.createComment(postId, request, userId); | ||
| return ResponseEntity.status(HttpStatus.CREATED).body(ApiResponse.success(Map.of("commentId", commentId, "message", "댓글이 작성되었습니다."))); | ||
| } | ||
|
|
||
| @PatchMapping("/{commentId}/adopt") | ||
| public ResponseEntity<ApiResponse<Map<String, String>>> adoptComment( | ||
| @PathVariable Long postId, | ||
| @PathVariable Long commentId, | ||
| @RequestHeader(value = "X-User-Id", defaultValue = "1") Long userId | ||
| ) { | ||
| commentService.adoptComment(postId, commentId, userId); | ||
| return ResponseEntity.ok(ApiResponse.success(Map.of("message", "댓글이 채택되었습니다."))); | ||
| } | ||
| } |
46 changes: 46 additions & 0 deletions
46
src/main/java/com/example/leets7th/domain/comment/controller/CommentControllerDocs.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,46 @@ | ||
| package com.example.leets7th.domain.comment.controller; | ||
|
|
||
| import com.example.leets7th.domain.comment.dto.CommentCreateRequest; | ||
| import com.example.leets7th.global.common.ApiResponse; | ||
| import io.swagger.v3.oas.annotations.Operation; | ||
| import io.swagger.v3.oas.annotations.Parameter; | ||
| import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
| import io.swagger.v3.oas.annotations.tags.Tag; | ||
| import jakarta.validation.Valid; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.PathVariable; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.RequestHeader; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| @Tag(name = "Comment", description = "댓글 관련 API") | ||
| public interface CommentControllerDocs { | ||
|
|
||
| @Operation(summary = "댓글 작성", description = "게시글에 댓글을 작성합니다.") | ||
| @ApiResponses({ | ||
| @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "201", description = "작성 성공"), | ||
| @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "게시글 없음") | ||
| }) | ||
| ResponseEntity<ApiResponse<Map<String, Object>>> createComment( | ||
| @Parameter(description = "게시글 ID", example = "1") @PathVariable Long postId, | ||
| @RequestBody @Valid CommentCreateRequest request, | ||
| @Parameter(description = "작성자 ID (임시)", example = "1") | ||
| @RequestHeader(value = "X-User-Id", defaultValue = "1") Long userId | ||
| ); | ||
|
|
||
| @Operation(summary = "댓글 채택", description = "게시글 작성자가 댓글을 채택합니다. 게시글당 하나의 댓글만 채택 가능합니다.") | ||
| @ApiResponses({ | ||
| @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "채택 성공"), | ||
| @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "400", description = "해당 게시글의 댓글이 아님"), | ||
| @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "403", description = "채택 권한 없음 (게시글 작성자가 아님)"), | ||
| @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "게시글 또는 댓글 없음"), | ||
| @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "409", description = "이미 채택된 댓글 존재") | ||
| }) | ||
| ResponseEntity<ApiResponse<Map<String, String>>> adoptComment( | ||
| @Parameter(description = "게시글 ID", example = "1") @PathVariable Long postId, | ||
| @Parameter(description = "댓글 ID", example = "1") @PathVariable Long commentId, | ||
| @Parameter(description = "요청자 ID (임시)", example = "1") | ||
| @RequestHeader(value = "X-User-Id", defaultValue = "1") Long userId | ||
| ); | ||
| } | ||
9 changes: 9 additions & 0 deletions
9
src/main/java/com/example/leets7th/domain/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,9 @@ | ||
| package com.example.leets7th.domain.comment.dto; | ||
|
|
||
| import jakarta.validation.constraints.NotBlank; | ||
|
|
||
| public record CommentCreateRequest( | ||
| @NotBlank(message = "댓글 내용을 입력해주세요.") | ||
| String content | ||
| ) { | ||
| } |
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
9 changes: 9 additions & 0 deletions
9
src/main/java/com/example/leets7th/domain/comment/repository/CommentRepository.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,9 @@ | ||
| package com.example.leets7th.domain.comment.repository; | ||
|
|
||
| import com.example.leets7th.domain.comment.entity.Comment; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| public interface CommentRepository extends JpaRepository<Comment, Long> { | ||
|
|
||
| boolean existsByPostIdAndAdoptedTrue(Long postId); | ||
| } |
64 changes: 64 additions & 0 deletions
64
src/main/java/com/example/leets7th/domain/comment/service/CommentService.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,64 @@ | ||
| package com.example.leets7th.domain.comment.service; | ||
|
|
||
| import com.example.leets7th.domain.comment.dto.CommentCreateRequest; | ||
| import com.example.leets7th.domain.comment.entity.Comment; | ||
| import com.example.leets7th.domain.comment.repository.CommentRepository; | ||
| import com.example.leets7th.domain.post.entity.Post; | ||
| import com.example.leets7th.domain.post.repository.PostRepository; | ||
| import com.example.leets7th.domain.user.entity.User; | ||
| import com.example.leets7th.domain.user.repository.UserRepository; | ||
| import com.example.leets7th.global.exception.AlreadyAdoptedException; | ||
| import com.example.leets7th.global.exception.CommentNotFoundException; | ||
| import com.example.leets7th.global.exception.ForbiddenException; | ||
| import com.example.leets7th.global.exception.PostNotFoundException; | ||
| import com.example.leets7th.global.exception.UserNotFoundException; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| @Transactional(readOnly = true) | ||
| public class CommentService { | ||
|
|
||
| private final CommentRepository commentRepository; | ||
| private final PostRepository postRepository; | ||
| private final UserRepository userRepository; | ||
|
|
||
| @Transactional | ||
| public Long createComment(Long postId, CommentCreateRequest request, Long userId) { | ||
| Post post = postRepository.findById(postId) | ||
| .orElseThrow(() -> new PostNotFoundException(postId)); | ||
| User user = userRepository.findById(userId) | ||
| .orElseThrow(() -> new UserNotFoundException(userId)); | ||
| Comment comment = Comment.create(request.content(), user, post); | ||
| commentRepository.save(comment); | ||
| return comment.getId(); | ||
| } | ||
|
|
||
| @Transactional | ||
| public void adoptComment(Long postId, Long commentId, Long userId) { | ||
| Post post = postRepository.findById(postId) | ||
| .orElseThrow(() -> new PostNotFoundException(postId)); | ||
|
|
||
| // 게시글 작성자만 채택 가능 | ||
| if (!post.getUser().getId().equals(userId)) { | ||
| throw new ForbiddenException("게시글 작성자만 댓글을 채택할 수 있습니다."); | ||
| } | ||
|
|
||
| Comment comment = commentRepository.findById(commentId) | ||
| .orElseThrow(() -> new CommentNotFoundException(commentId)); | ||
|
|
||
| // 해당 게시글의 댓글인지 확인 | ||
| if (!comment.getPost().getId().equals(postId)) { | ||
| throw new IllegalArgumentException("해당 게시글에 속한 댓글이 아닙니다."); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기도 동일하게 커스텀 예외처리하는건 어떨까요~?! |
||
| } | ||
|
|
||
| // 이미 채택된 댓글이 있는지 확인 | ||
| if (commentRepository.existsByPostIdAndAdoptedTrue(postId)) { | ||
| throw new AlreadyAdoptedException(); | ||
| } | ||
|
|
||
| comment.adopt(); | ||
| } | ||
| } | ||
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.
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.
역시 스웨거 문서관리 👍 👍 👍