-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#20 [feat] POST - 댓글 등록 API 구현
- Loading branch information
Showing
21 changed files
with
248 additions
and
6 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
module-api/src/main/java/com/mile/controller/post/PostController.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,38 @@ | ||
package com.mile.controller.post; | ||
|
||
import com.mile.dto.SuccessResponse; | ||
import com.mile.exception.message.SuccessMessage; | ||
import com.mile.post.service.PostService; | ||
import com.mile.post.service.dto.CommentCreateRequest; | ||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.security.Principal; | ||
|
||
@RestController | ||
@RequestMapping("/api/post") | ||
@RequiredArgsConstructor | ||
public class PostController implements PostControllerSwagger{ | ||
|
||
private final PostService postService; | ||
|
||
@PostMapping("/{postId}/comment") | ||
@Override | ||
public SuccessResponse postComment( | ||
@PathVariable final Long postId, | ||
@Valid @RequestBody final CommentCreateRequest commentCreateRequest, | ||
final Principal principal | ||
) { | ||
postService.createCommentOnPost( | ||
postId, | ||
Long.valueOf(principal.getName()), | ||
commentCreateRequest | ||
); | ||
return SuccessResponse.of(SuccessMessage.COMMENT_CREATE_SUCCESS); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
module-api/src/main/java/com/mile/controller/post/PostControllerSwagger.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,40 @@ | ||
package com.mile.controller.post; | ||
|
||
import com.mile.dto.ErrorResponse; | ||
import com.mile.dto.SuccessResponse; | ||
import com.mile.post.service.dto.CommentCreateRequest; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.media.Content; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import jakarta.validation.Valid; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
|
||
import java.security.Principal; | ||
|
||
@Tag(name = "Post", description = "게시글 관련 API - 댓글 등록/ 조회 포함") | ||
public interface PostControllerSwagger { | ||
|
||
@Operation(summary = "댓글 작성") | ||
@ApiResponses( | ||
value = { | ||
@ApiResponse(responseCode = "200", description = "댓글 등록이 완료되었습니다."), | ||
@ApiResponse(responseCode = "400", | ||
description = "1. 댓글 최대 입력 길이(500자)를 초과하였습니다.\n" + | ||
"2.댓글에 내용이 없습니다.", | ||
content = @Content(schema = @Schema(implementation = ErrorResponse.class))), | ||
@ApiResponse(responseCode = "403", description = "해당 사용자는 모임에 접근 권한이 없습니다.", | ||
content = @Content(schema = @Schema(implementation = ErrorResponse.class))), | ||
@ApiResponse(responseCode = "500", description = "서버 내부 오류입니다.", | ||
content = @Content(schema = @Schema(implementation = ErrorResponse.class))) | ||
} | ||
) | ||
SuccessResponse postComment( | ||
@PathVariable final Long postId, | ||
@Valid @RequestBody final CommentCreateRequest commentCreateRequest, | ||
final Principal principal | ||
); | ||
} |
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
7 changes: 7 additions & 0 deletions
7
module-domain/src/main/java/com/mile/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,7 @@ | ||
package com.mile.comment.repository; | ||
|
||
import com.mile.comment.domain.Comment; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface CommentRepository extends JpaRepository<Comment, Long> { | ||
} |
17 changes: 17 additions & 0 deletions
17
module-domain/src/main/java/com/mile/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 |
---|---|---|
@@ -1,7 +1,24 @@ | ||
package com.mile.comment.service; | ||
|
||
import com.mile.comment.domain.Comment; | ||
import com.mile.comment.repository.CommentRepository; | ||
import com.mile.post.domain.Post; | ||
import com.mile.post.service.dto.CommentCreateRequest; | ||
import com.mile.user.domain.User; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class CommentService { | ||
private static boolean ANONYMOUS_TRUE = true; | ||
private final CommentRepository commentRepository; | ||
|
||
public void createComment( | ||
final Post post, | ||
final User user, | ||
final CommentCreateRequest commentCreateRequest | ||
) { | ||
commentRepository.save(Comment.create(post, user, commentCreateRequest, ANONYMOUS_TRUE)); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
module-domain/src/main/java/com/mile/curious/domain/Curious.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
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
7 changes: 7 additions & 0 deletions
7
module-domain/src/main/java/com/mile/post/repository/PostRepository.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,7 @@ | ||
package com.mile.post.repository; | ||
|
||
import com.mile.post.domain.Post; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface PostRepository extends JpaRepository<Post, Long> { | ||
} |
52 changes: 52 additions & 0 deletions
52
module-domain/src/main/java/com/mile/post/service/PostService.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,52 @@ | ||
package com.mile.post.service; | ||
|
||
import com.mile.comment.service.CommentService; | ||
import com.mile.exception.message.ErrorMessage; | ||
import com.mile.exception.model.NotFoundException; | ||
import com.mile.moim.serivce.MoimService; | ||
import com.mile.post.domain.Post; | ||
import com.mile.post.repository.PostRepository; | ||
import com.mile.post.service.dto.CommentCreateRequest; | ||
import com.mile.user.serivce.UserService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class PostService { | ||
|
||
private final PostRepository postRepository; | ||
private final MoimService moimService; | ||
private final CommentService commentService; | ||
private final UserService userService; | ||
|
||
@Transactional | ||
public void createCommentOnPost( | ||
final Long postId, | ||
final Long userId, | ||
final CommentCreateRequest commentCreateRequest | ||
|
||
) { | ||
Post post = findById(postId); | ||
authenticateUserWithPost(post, userId); | ||
commentService.createComment(post, userService.findById(userId), commentCreateRequest); | ||
} | ||
|
||
|
||
private void authenticateUserWithPost( | ||
final Post post, | ||
final Long userId | ||
) { | ||
moimService.authenticateUserOfMoim(post.getTopic().getMoim().getId(), userId); | ||
} | ||
|
||
public Post findById( | ||
final Long postId | ||
) { | ||
return postRepository.findById(postId) | ||
.orElseThrow( | ||
() -> new NotFoundException(ErrorMessage.POST_NOT_FOUND) | ||
); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
module-domain/src/main/java/com/mile/post/service/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,14 @@ | ||
package com.mile.post.service.dto; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.Size; | ||
|
||
public record CommentCreateRequest( | ||
|
||
@Schema(description = "댓글 내용", example = "댓글 내용을 입력해주세요") | ||
@NotBlank(message = "댓글에 내용이 없습니다.") | ||
@Size(max = 500, message = "댓글 최대 입력 길이(500자)를 초과하였습니다.") | ||
String content | ||
) { | ||
} |
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
Oops, something went wrong.