-
Notifications
You must be signed in to change notification settings - Fork 20
[4주차] 변승현 /[feat] 추가 API 구현 #150
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
gusanans218
wants to merge
3
commits into
Leets-Official:변승현/main
Choose a base branch
from
gusanans218:변승현/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: "\uBCC0\uC2B9\uD604/4\uC8FC\uCC28"
Open
Changes from all commits
Commits
Show all changes
3 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
47 changes: 47 additions & 0 deletions
47
src/main/java/com/example/demo/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,47 @@ | ||
| package com.example.demo.controller; | ||
|
|
||
| import com.example.demo.domain.comment.dto.CommentAdoptRequest; | ||
| import com.example.demo.domain.comment.dto.CommentCreateRequest; | ||
| import com.example.demo.domain.comment.dto.CommentCreateResponse; | ||
| import com.example.demo.domain.comment.dto.CommentStatusResponse; | ||
| import com.example.demo.domain.comment.service.CommentService; | ||
| import com.example.demo.global.response.ApiResponse; | ||
| import jakarta.validation.Valid; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| @RestController | ||
| @RequiredArgsConstructor | ||
| public class CommentController { | ||
|
|
||
| private final CommentService commentService; | ||
|
|
||
| @PostMapping("/api/posts/{postId}/comments") | ||
| public ResponseEntity<ApiResponse<CommentCreateResponse>> createComment( | ||
| @PathVariable Long postId, | ||
| @Valid @RequestBody CommentCreateRequest request | ||
| ) { | ||
| return ResponseEntity.status(HttpStatus.CREATED) | ||
| .body(ApiResponse.success( | ||
| "COMMENT_CREATE_SUCCESS", | ||
| "댓글 생성 성공", | ||
| commentService.createComment(postId, request) | ||
| )); | ||
| } | ||
|
|
||
| @PostMapping("/api/comments/{commentId}/adoption") | ||
| public ResponseEntity<ApiResponse<CommentStatusResponse>> adoptComment( | ||
| @PathVariable Long commentId, | ||
| @Valid @RequestBody CommentAdoptRequest request | ||
| ) { | ||
| return ResponseEntity.ok( | ||
| ApiResponse.success( | ||
| "COMMENT_ADOPT_SUCCESS", | ||
| "댓글 채택 성공", | ||
| commentService.adoptComment(commentId, request.userId()) | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
60 changes: 60 additions & 0 deletions
60
src/main/java/com/example/demo/controller/ReportController.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,60 @@ | ||
| package com.example.demo.controller; | ||
|
|
||
| import com.example.demo.domain.report.dto.ReportCreateRequest; | ||
| import com.example.demo.domain.report.dto.ReportCreateResponse; | ||
| import com.example.demo.domain.report.dto.ReportResolveRequest; | ||
| import com.example.demo.domain.report.dto.ReportResolveResponse; | ||
| import com.example.demo.domain.report.service.ReportService; | ||
| import com.example.demo.global.response.ApiResponse; | ||
| import jakarta.validation.Valid; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| @RestController | ||
| @RequiredArgsConstructor | ||
| public class ReportController { | ||
|
|
||
| private final ReportService reportService; | ||
|
|
||
| @PostMapping("/api/posts/{postId}/reports") | ||
| public ResponseEntity<ApiResponse<ReportCreateResponse>> reportPost( | ||
| @PathVariable Long postId, | ||
| @Valid @RequestBody ReportCreateRequest request | ||
| ) { | ||
| return ResponseEntity.status(HttpStatus.CREATED) | ||
| .body(ApiResponse.success( | ||
| "POST_REPORT_SUCCESS", | ||
| "게시글 신고 접수 성공", | ||
| reportService.reportPost(postId, request) | ||
| )); | ||
| } | ||
|
|
||
| @PostMapping("/api/comments/{commentId}/reports") | ||
| public ResponseEntity<ApiResponse<ReportCreateResponse>> reportComment( | ||
| @PathVariable Long commentId, | ||
| @Valid @RequestBody ReportCreateRequest request | ||
| ) { | ||
| return ResponseEntity.status(HttpStatus.CREATED) | ||
| .body(ApiResponse.success( | ||
| "COMMENT_REPORT_SUCCESS", | ||
| "댓글 신고 접수 성공", | ||
| reportService.reportComment(commentId, request) | ||
| )); | ||
| } | ||
|
|
||
| @PatchMapping("/api/reports/{reportId}/resolve") | ||
| public ResponseEntity<ApiResponse<ReportResolveResponse>> resolveReport( | ||
| @PathVariable Long reportId, | ||
| @Valid @RequestBody ReportResolveRequest request | ||
| ) { | ||
| return ResponseEntity.ok( | ||
| ApiResponse.success( | ||
| "REPORT_RESOLVE_SUCCESS", | ||
| "신고 처리 완료", | ||
| reportService.resolveReport(reportId, request) | ||
| ) | ||
| ); | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/example/demo/domain/comment/dto/CommentAdoptRequest.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.demo.domain.comment.dto; | ||
|
|
||
| import jakarta.validation.constraints.NotNull; | ||
|
|
||
| public record CommentAdoptRequest( | ||
| @NotNull(message = "userId는 필수입니다.") | ||
| Long userId | ||
|
Comment on lines
+5
to
+7
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. 필드값이 하나라면 (지금은) RequestParam으로 받는것도 방법이 될 수 있습니다! 참고로 |
||
| ) { | ||
| } | ||
13 changes: 13 additions & 0 deletions
13
src/main/java/com/example/demo/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,13 @@ | ||
| package com.example.demo.domain.comment.dto; | ||
|
|
||
| import jakarta.validation.constraints.NotBlank; | ||
| import jakarta.validation.constraints.NotNull; | ||
|
|
||
| public record CommentCreateRequest( | ||
| @NotNull(message = "userId는 필수입니다.") | ||
| Long userId, | ||
|
|
||
| @NotBlank(message = "content는 필수입니다.") | ||
| String content | ||
| ) { | ||
| } |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/example/demo/domain/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,9 @@ | ||
| package com.example.demo.domain.comment.dto; | ||
|
|
||
| import com.example.demo.domain.comment.entity.CommentStatus; | ||
|
|
||
| public record CommentCreateResponse( | ||
| Long commentId, | ||
| CommentStatus status | ||
| ) { | ||
| } |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/example/demo/domain/comment/dto/CommentStatusResponse.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.demo.domain.comment.dto; | ||
|
|
||
| import com.example.demo.domain.comment.entity.CommentStatus; | ||
|
|
||
| public record CommentStatusResponse( | ||
| Long commentId, | ||
| CommentStatus status | ||
| ) { | ||
| } |
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
7 changes: 7 additions & 0 deletions
7
src/main/java/com/example/demo/domain/comment/entity/CommentStatus.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,7 @@ | ||
| package com.example.demo.domain.comment.entity; | ||
|
|
||
| public enum CommentStatus { | ||
| ACTIVE, | ||
| HIDDEN, | ||
| ADOPTED | ||
| } |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/example/demo/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.demo.domain.comment.repository; | ||
|
|
||
| import com.example.demo.domain.comment.entity.Comment; | ||
| import com.example.demo.domain.comment.entity.CommentStatus; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| public interface CommentRepository extends JpaRepository<Comment, Long> { | ||
| boolean existsByPostIdAndStatus(Long postId, CommentStatus status); | ||
| } |
78 changes: 78 additions & 0 deletions
78
src/main/java/com/example/demo/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,78 @@ | ||
| package com.example.demo.domain.comment.service; | ||
|
|
||
| import com.example.demo.domain.comment.dto.CommentCreateRequest; | ||
| import com.example.demo.domain.comment.dto.CommentCreateResponse; | ||
| import com.example.demo.domain.comment.dto.CommentStatusResponse; | ||
| import com.example.demo.domain.comment.entity.Comment; | ||
| import com.example.demo.domain.comment.entity.CommentStatus; | ||
| import com.example.demo.domain.comment.repository.CommentRepository; | ||
| import com.example.demo.domain.post.entity.Post; | ||
| import com.example.demo.domain.post.entity.PostStatus; | ||
| import com.example.demo.domain.post.repository.PostRepository; | ||
| import com.example.demo.domain.user.entity.User; | ||
| import com.example.demo.domain.user.repository.UserRepository; | ||
| import com.example.demo.global.exception.CustomException; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.http.HttpStatus; | ||
| 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 CommentCreateResponse createComment(Long postId, CommentCreateRequest request) { | ||
| Post post = findPost(postId); | ||
| User user = findUser(request.userId()); | ||
|
|
||
| if (post.getStatus() == PostStatus.HIDDEN) { | ||
| throw new CustomException(HttpStatus.BAD_REQUEST, "POST_HIDDEN", "숨김 처리된 게시글에는 댓글을 작성할 수 없습니다."); | ||
| } | ||
|
|
||
| Comment comment = commentRepository.save(Comment.of(user, post, request.content())); | ||
| return new CommentCreateResponse(comment.getId(), comment.getStatus()); | ||
| } | ||
|
|
||
| @Transactional | ||
| public CommentStatusResponse adoptComment(Long commentId, Long userId) { | ||
| Comment comment = findComment(commentId); | ||
| Post post = comment.getPost(); | ||
|
|
||
| if (!post.getUser().getId().equals(userId)) { | ||
| throw new CustomException(HttpStatus.FORBIDDEN, "FORBIDDEN", "댓글 채택 권한이 없습니다."); | ||
| } | ||
| if (post.getStatus() == PostStatus.HIDDEN) { | ||
| throw new CustomException(HttpStatus.BAD_REQUEST, "POST_HIDDEN", "숨김 처리된 게시글에서는 댓글을 채택할 수 없습니다."); | ||
| } | ||
| if (comment.getStatus() == CommentStatus.HIDDEN) { | ||
| throw new CustomException(HttpStatus.BAD_REQUEST, "COMMENT_HIDDEN", "숨김 처리된 댓글은 채택할 수 없습니다."); | ||
| } | ||
| if (commentRepository.existsByPostIdAndStatus(post.getId(), CommentStatus.ADOPTED)) { | ||
| throw new CustomException(HttpStatus.CONFLICT, "COMMENT_ALREADY_ADOPTED", "이미 채택된 댓글이 있습니다."); | ||
| } | ||
|
|
||
| comment.adopt(); | ||
| return new CommentStatusResponse(comment.getId(), comment.getStatus()); | ||
| } | ||
|
|
||
| public Comment findComment(Long commentId) { | ||
| return commentRepository.findById(commentId) | ||
| .orElseThrow(() -> new CustomException(HttpStatus.NOT_FOUND, "COMMENT_NOT_FOUND", "해당 댓글을 찾을 수 없습니다.")); | ||
| } | ||
|
|
||
| private Post findPost(Long postId) { | ||
| return postRepository.findById(postId) | ||
| .orElseThrow(() -> new CustomException(HttpStatus.NOT_FOUND, "POST_NOT_FOUND", "해당 게시글을 찾을 수 없습니다.")); | ||
| } | ||
|
|
||
| private User findUser(Long userId) { | ||
| return userRepository.findById(userId) | ||
| .orElseThrow(() -> new CustomException(HttpStatus.NOT_FOUND, "USER_NOT_FOUND", "해당 사용자를 찾을 수 없습니다.")); | ||
| } | ||
| } |
3 changes: 3 additions & 0 deletions
3
src/main/java/com/example/demo/domain/post/dto/PostDetailResponse.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
3 changes: 3 additions & 0 deletions
3
src/main/java/com/example/demo/domain/post/dto/PostListItemResponse.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
6 changes: 6 additions & 0 deletions
6
src/main/java/com/example/demo/domain/post/entity/PostStatus.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.demo.domain.post.entity; | ||
|
|
||
| public enum PostStatus { | ||
| ACTIVE, | ||
| HIDDEN | ||
| } |
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
13 changes: 13 additions & 0 deletions
13
src/main/java/com/example/demo/domain/report/dto/ReportCreateRequest.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,13 @@ | ||
| package com.example.demo.domain.report.dto; | ||
|
|
||
| import jakarta.validation.constraints.NotBlank; | ||
| import jakarta.validation.constraints.NotNull; | ||
|
|
||
| public record ReportCreateRequest( | ||
| @NotNull(message = "reporterId는 필수입니다.") | ||
| Long reporterId, | ||
|
|
||
| @NotBlank(message = "reason은 필수입니다.") | ||
| String reason | ||
| ) { | ||
| } |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/example/demo/domain/report/dto/ReportCreateResponse.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,11 @@ | ||
| package com.example.demo.domain.report.dto; | ||
|
|
||
| import com.example.demo.domain.report.entity.ReportStatus; | ||
| import com.example.demo.domain.report.entity.ReportTargetType; | ||
|
|
||
| public record ReportCreateResponse( | ||
| Long reportId, | ||
| ReportTargetType targetType, | ||
| ReportStatus status | ||
| ) { | ||
| } |
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.
댓글 생성은 /api/posts/{postId}/comments처럼 “게시글의 하위 리소스인 댓글” 구조로 잘 잡혀 있는데, 댓글 채택 API는 /api/comments/{commentId}/adoption이라 리소스 기준이 달라 보여요.
댓글 채택도 특정 게시글 안의 특정 댓글에 대한 행동이므로, API 경로를 게시글 기준으로 맞추면 더 일관성 있어 보입니다.
만약 채택상태를 변경한다는 의미를 살리면 Patch 로 하는것 도 좋아보입니다!