-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEAT] 모임 게시글 댓글 작성 API 마이그레이션 및 알림 작업 (#148)
* [FEAT] 모임 게시글 댓글 작성 API 마이그레이션 및 푸시 알림 구현 #118 * [INFRA] 게시글 댓글 작성 API v2 라우팅 규칙 변경 #118 * [INFRA] 댓글 작성 API와 게시글 작성 API 라우팅 규칙 수정 #118
- Loading branch information
Showing
13 changed files
with
312 additions
and
132 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
42 changes: 42 additions & 0 deletions
42
main/src/main/java/org/sopt/makers/crew/main/comment/v2/CommentV2Controller.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,42 @@ | ||
package org.sopt.makers.crew.main.comment.v2; | ||
|
||
import io.swagger.v3.oas.annotations.Operation; | ||
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 java.security.Principal; | ||
import lombok.RequiredArgsConstructor; | ||
import org.sopt.makers.crew.main.comment.v2.dto.request.CommentV2CreateCommentBodyDto; | ||
import org.sopt.makers.crew.main.comment.v2.dto.response.CommentV2CreateCommentResponseDto; | ||
import org.sopt.makers.crew.main.comment.v2.service.CommentV2Service; | ||
import org.sopt.makers.crew.main.common.util.UserUtil; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
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.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/comment/v2") | ||
@RequiredArgsConstructor | ||
@Tag(name = "댓글/대댓글") | ||
public class CommentV2Controller { | ||
|
||
private final CommentV2Service commentV2Service; | ||
|
||
@Operation(summary = "모임 게시글 댓글 작성") | ||
@PostMapping() | ||
@ResponseStatus(HttpStatus.CREATED) | ||
@ApiResponses(value = { | ||
@ApiResponse(responseCode = "201", description = "성공"), | ||
}) | ||
public ResponseEntity<CommentV2CreateCommentResponseDto> createComment( | ||
@Valid @RequestBody CommentV2CreateCommentBodyDto requestBody, Principal principal) { | ||
Integer userId = UserUtil.getUserId(principal); | ||
return ResponseEntity.ok(commentV2Service.createComment(requestBody, userId)); | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
.../java/org/sopt/makers/crew/main/comment/v2/dto/request/CommentV2CreateCommentBodyDto.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,22 @@ | ||
package org.sopt.makers.crew.main.comment.v2.dto.request; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import jakarta.validation.constraints.NotEmpty; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
@Schema(description = "댓글 생성 request body dto") | ||
public class CommentV2CreateCommentBodyDto { | ||
|
||
@Schema(example = "1", required = true, description = "게시글 ID") | ||
@NotNull | ||
private Integer postId; | ||
|
||
@Schema(example = "알고보면 쓸데있는 개발 프로세스", required = true, description = "댓글 내용") | ||
@NotEmpty | ||
private String contents; | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
.../org/sopt/makers/crew/main/comment/v2/dto/response/CommentV2CreateCommentResponseDto.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 org.sopt.makers.crew.main.comment.v2.dto.response; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor(staticName = "of") | ||
public class CommentV2CreateCommentResponseDto { | ||
|
||
/** | ||
* 생성된 댓글 id | ||
*/ | ||
private Integer commentId; | ||
} |
10 changes: 10 additions & 0 deletions
10
main/src/main/java/org/sopt/makers/crew/main/comment/v2/service/CommentV2Service.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,10 @@ | ||
package org.sopt.makers.crew.main.comment.v2.service; | ||
|
||
import org.sopt.makers.crew.main.comment.v2.dto.request.CommentV2CreateCommentBodyDto; | ||
import org.sopt.makers.crew.main.comment.v2.dto.response.CommentV2CreateCommentResponseDto; | ||
|
||
public interface CommentV2Service { | ||
|
||
CommentV2CreateCommentResponseDto createComment(CommentV2CreateCommentBodyDto requestBody, | ||
Integer userId); | ||
} |
72 changes: 72 additions & 0 deletions
72
main/src/main/java/org/sopt/makers/crew/main/comment/v2/service/CommentV2ServiceImpl.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,72 @@ | ||
package org.sopt.makers.crew.main.comment.v2.service; | ||
|
||
import static org.sopt.makers.crew.main.internal.notification.PushNotificationEnums.NEW_COMMENT_PUSH_NOTIFICATION_TITLE; | ||
import static org.sopt.makers.crew.main.internal.notification.PushNotificationEnums.PUSH_NOTIFICATION_CATEGORY; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.sopt.makers.crew.main.comment.v2.dto.request.CommentV2CreateCommentBodyDto; | ||
import org.sopt.makers.crew.main.comment.v2.dto.response.CommentV2CreateCommentResponseDto; | ||
import org.sopt.makers.crew.main.entity.comment.Comment; | ||
import org.sopt.makers.crew.main.entity.comment.CommentRepository; | ||
import org.sopt.makers.crew.main.entity.post.Post; | ||
import org.sopt.makers.crew.main.entity.post.PostRepository; | ||
import org.sopt.makers.crew.main.entity.user.User; | ||
import org.sopt.makers.crew.main.entity.user.UserRepository; | ||
import org.sopt.makers.crew.main.internal.notification.PushNotificationService; | ||
import org.sopt.makers.crew.main.internal.notification.dto.PushNotificationRequestDto; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) | ||
public class CommentV2ServiceImpl implements CommentV2Service { | ||
|
||
private final PostRepository postRepository; | ||
private final UserRepository userRepository; | ||
private final CommentRepository commentRepository; | ||
private final PushNotificationService pushNotificationService; | ||
|
||
@Value("${push-notification.web-url}") | ||
private String pushWebUrl; | ||
|
||
/** | ||
* 모임 게시글 댓글 작성 | ||
* | ||
* @throws 400 존재하지 않는 게시글일 떄 | ||
* @apiNote 모임에 속한 유저만 작성 가능 | ||
*/ | ||
@Override | ||
@Transactional | ||
|
||
public CommentV2CreateCommentResponseDto createComment(CommentV2CreateCommentBodyDto requestBody, | ||
Integer userId) { | ||
Post post = postRepository.findByIdOrThrow(requestBody.getPostId()); | ||
User user = userRepository.findByIdOrThrow(userId); | ||
|
||
Comment comment = Comment.builder() | ||
.contents(requestBody.getContents()) | ||
.user(user) | ||
.post(post) | ||
.build(); | ||
|
||
Comment savedComment = commentRepository.save(comment); | ||
|
||
User PostWriter = post.getUser(); | ||
String[] userIds = {String.valueOf(PostWriter.getOrgId())}; | ||
|
||
String pushNotificationContent = String.format("[%s의 댓글] : \"%s\"", | ||
user.getName(), requestBody.getContents()); | ||
String pushNotificationWeblink = pushWebUrl + "/post?id=" + post.getId(); | ||
|
||
PushNotificationRequestDto pushRequestDto = PushNotificationRequestDto.of(userIds, | ||
NEW_COMMENT_PUSH_NOTIFICATION_TITLE.getValue(), | ||
pushNotificationContent, | ||
PUSH_NOTIFICATION_CATEGORY.getValue(), pushNotificationWeblink); | ||
|
||
pushNotificationService.sendPushNotification(pushRequestDto); | ||
|
||
return CommentV2CreateCommentResponseDto.of(savedComment.getId()); | ||
} | ||
} |
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.