Skip to content

Commit

Permalink
Merge pull request #34 from Team-Shaka/feature/33-reaction
Browse files Browse the repository at this point in the history
Feature/33-반응 추가/삭제
  • Loading branch information
koojun99 authored Feb 7, 2024
2 parents 418e0a0 + 7836921 commit 9f7711d
Show file tree
Hide file tree
Showing 20 changed files with 412 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.example.tree.domain.comment.entity.Comment;
import org.example.tree.domain.post.entity.Post;
import org.example.tree.domain.profile.entity.Profile;
import org.example.tree.domain.reaction.dto.ReactionResponseDTO;
import org.example.tree.domain.reaction.entity.Reaction;
import org.springframework.stereotype.Component;

import java.util.List;
Expand All @@ -20,11 +22,12 @@ public Comment toComment(String content, Profile profile, Post post) {
.build();
}

public CommentResponseDTO.getComment toGetComment(Comment comment, List<ReplyResponseDTO.getReply> replies) {
public CommentResponseDTO.getComment toGetComment(Comment comment, List<ReactionResponseDTO.getReaction> reactions, List<ReplyResponseDTO.getReply> replies) {
return CommentResponseDTO.getComment.builder()
.commentId(comment.getId())
.memberName(comment.getProfile().getMemberName())
.content(comment.getContent())
.reactions(reactions)
.createdAt(comment.getCreatedAt())
.replies(replies)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
import org.example.tree.domain.comment.entity.Comment;
import org.example.tree.domain.comment.entity.Reply;
import org.example.tree.domain.profile.entity.Profile;
import org.example.tree.domain.reaction.dto.ReactionResponseDTO;
import org.example.tree.domain.reaction.entity.Reaction;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class ReplyConverter {

Expand All @@ -17,11 +21,12 @@ public Reply toReply(String content, Profile profile, Comment comment) {
.build();
}

public ReplyResponseDTO.getReply toGetReply(Reply reply) {
public ReplyResponseDTO.getReply toGetReply(Reply reply, List<ReactionResponseDTO.getReaction> reactions) {
return ReplyResponseDTO.getReply.builder()
.replyId(reply.getId())
.memberName(reply.getProfile().getMemberName())
.content(reply.getContent())
.reactions(reactions)
.createdAt(reply.getCreatedAt())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.example.tree.domain.comment.dto;

import lombok.*;
import org.example.tree.domain.reaction.dto.ReactionResponseDTO;

import java.time.LocalDateTime;
import java.util.List;
Expand All @@ -17,6 +18,7 @@ public static class getComment {
private Long commentId;
private String memberName;
private String content;
private List<ReactionResponseDTO.getReaction> reactions;
private LocalDateTime createdAt;
private List<ReplyResponseDTO.getReply> replies;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.example.tree.domain.comment.dto;

import lombok.*;
import org.example.tree.domain.reaction.dto.ReactionResponseDTO;

import java.time.LocalDateTime;
import java.util.List;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ReplyResponseDTO {
Expand All @@ -15,6 +17,7 @@ public static class getReply {
private Long replyId;
private String memberName;
private String content;
private List<ReactionResponseDTO.getReaction> reactions;
private LocalDateTime createdAt;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.example.tree.domain.post.service.PostQueryService;
import org.example.tree.domain.profile.entity.Profile;
import org.example.tree.domain.profile.service.ProfileService;
import org.example.tree.domain.reaction.dto.ReactionResponseDTO;
import org.example.tree.domain.reaction.service.ReactionService;
import org.example.tree.global.exception.GeneralException;
import org.example.tree.global.exception.GlobalErrorCode;
import org.springframework.stereotype.Component;
Expand All @@ -27,6 +29,7 @@ public class CommentService {
private final ReplyService replyService;
private final CommentConverter commentConverter;
private final ProfileService profileService;
private final ReactionService reactionService;

@Transactional
public void createComment(Long treeId, Long postId, CommentRequestDTO.createComment request, String token) {
Expand All @@ -44,8 +47,9 @@ public List<CommentResponseDTO.getComment> getComments(Long treeId, Long postId,
List<Comment> comments = commentQueryService.getComments(post);
return comments.stream()
.map(comment -> {
List<ReactionResponseDTO.getReaction> reactions = reactionService.getCommentReactions(treeId, comment.getId(), token);
List<ReplyResponseDTO.getReply> repliesForComment = replyService.getReplies(comment);
return commentConverter.toGetComment(comment, repliesForComment); // toGetComment 메서드 수정 필요
return commentConverter.toGetComment(comment, reactions, repliesForComment); // toGetComment 메서드 수정 필요
})
.collect(Collectors.toList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.example.tree.domain.comment.entity.Reply;
import org.example.tree.domain.profile.entity.Profile;
import org.example.tree.domain.profile.service.ProfileService;
import org.example.tree.domain.reaction.dto.ReactionResponseDTO;
import org.example.tree.domain.reaction.service.ReactionService;
import org.example.tree.global.exception.GeneralException;
import org.example.tree.global.exception.GlobalErrorCode;
import org.springframework.stereotype.Component;
Expand All @@ -24,6 +26,7 @@ public class ReplyService {
private final ReplyConverter replyConverter;
private final CommentQueryService commentQueryService;
private final ProfileService profileService;
private final ReactionService reactionService;

@Transactional
public void createReply(Long treeId, Long commentId, ReplyRequestDTO.createReply request, String token) {
Expand All @@ -37,7 +40,10 @@ public void createReply(Long treeId, Long commentId, ReplyRequestDTO.createReply
public List<ReplyResponseDTO.getReply> getReplies(Comment comment) {
List<Reply> replies = replyQueryService.getReplies(comment);
return replies.stream()
.map(reply -> replyConverter.toGetReply(reply))
.map(reply -> {
List<ReactionResponseDTO.getReaction> reactions = reactionService.getReplyReactions(comment.getPost().getTree().getId(), reply.getId(), comment.getProfile());
return replyConverter.toGetReply(reply, reactions);
})
.collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.example.tree.domain.post.dto.PostResponseDTO;
import org.example.tree.domain.post.entity.Post;
import org.example.tree.domain.profile.entity.Profile;
import org.example.tree.domain.reaction.dto.ReactionResponseDTO;
import org.springframework.stereotype.Component;

import java.util.List;
Expand Down Expand Up @@ -32,24 +33,26 @@ public PostResponseDTO.createPost toCreatePost(Post savedPost) {
.build();
}

public PostResponseDTO.getFeed toGetFeed(Post post) {
public PostResponseDTO.getFeed toGetFeed(Post post, List<ReactionResponseDTO.getReaction> reactions) {
return PostResponseDTO.getFeed.builder()
.profileImageUrl(post.getProfile().getProfileImageUrl())
.memberName(post.getProfile().getMemberName())
.content(post.getContent())
.postImageUrls(post.getPostImages())
.createdAt(post.getCreatedAt())
.reactions(reactions)
.commentCount(post.getCommentCount())
.build();
}

public PostResponseDTO.getPost toGetPost(Post post) {
public PostResponseDTO.getPost toGetPost(Post post, List<ReactionResponseDTO.getReaction> reactions) {
return PostResponseDTO.getPost.builder()
.postId(post.getId())
.profileImageUrl(post.getProfile().getProfileImageUrl())
.memberName(post.getProfile().getMemberName())
.content(post.getContent())
.postImageUrls(post.getPostImages())
.reactions(reactions)
.createdAt(post.getCreatedAt())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.example.tree.domain.post.dto;

import lombok.*;
import org.example.tree.domain.reaction.dto.ReactionResponseDTO;

import java.time.LocalDateTime;
import java.util.List;
Expand Down Expand Up @@ -32,6 +33,7 @@ public static class getFeed {
private List<String> postImageUrls;
private LocalDateTime createdAt;
private Integer commentCount;
private List<ReactionResponseDTO.getReaction> reactions;

}

Expand All @@ -45,6 +47,7 @@ public static class getPost {
private String memberName;
private String content;
private List<String> postImageUrls;
private List<ReactionResponseDTO.getReaction> reactions;
private LocalDateTime createdAt;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.example.tree.domain.profile.entity.Profile;
import org.example.tree.domain.profile.service.ProfileQueryService;
import org.example.tree.domain.profile.service.ProfileService;
import org.example.tree.domain.reaction.dto.ReactionResponseDTO;
import org.example.tree.domain.reaction.service.ReactionService;
import org.example.tree.global.exception.GeneralException;
import org.example.tree.global.exception.GlobalErrorCode;
import org.springframework.stereotype.Component;
Expand All @@ -26,6 +28,7 @@ public class PostService {
private final PostQueryService postQueryService;
private final PostConverter postConverter;
private final ProfileService profileService;
private final ReactionService reactionService;

@Transactional
public PostResponseDTO.createPost createPost(Long treeId, PostRequestDTO.createPost request, String token) {
Expand All @@ -40,15 +43,21 @@ public List<PostResponseDTO.getFeed> getFeed(Long treeId, String token) {
Profile profile = profileService.getTreeProfile(token, treeId);
List<Post> posts = postQueryService.getPosts(profile.getTree());
return posts.stream()
.map(post -> postConverter.toGetFeed(post))
.map(post -> {
// 각 포스트에 대한 반응들을 가져옵니다.
List<ReactionResponseDTO.getReaction> reactions = reactionService.getPostReactions(treeId, post.getId(), token);
// Post와 해당 Post의 반응들을 포함하여 DTO를 생성합니다.
return postConverter.toGetFeed(post, reactions);
})
.collect(Collectors.toList());
}

@Transactional
public PostResponseDTO.getPost getPost(Long treeId, Long postId, String token) {
Profile profile = profileService.getTreeProfile(token, treeId);
Post post = postQueryService.findById(postId);
return postConverter.toGetPost(post);
List<ReactionResponseDTO.getReaction> reactions = reactionService.getPostReactions(treeId, postId, token);
return postConverter.toGetPost(post, reactions);
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,52 @@
package org.example.tree.domain.reaction.controller;

import lombok.RequiredArgsConstructor;
import org.example.tree.domain.reaction.dto.ReactionRequestDTO;
import org.example.tree.domain.reaction.service.ReactionService;
import org.example.tree.global.common.ApiResponse;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping
public class ReactionController {
private final ReactionService reactionService;

@PostMapping("/trees/{treeId}/feed/posts/{postId}/reaction")
public ApiResponse createPostReaction(
@PathVariable Long treeId,
@PathVariable Long postId,
@RequestHeader("Authorization") String header,
@RequestBody ReactionRequestDTO.createReaction request
) {
String token = header.replace("Bearer ", "");
reactionService.reactToPost(treeId, postId, request, token);
return ApiResponse.onSuccess("");
}

@PostMapping("/trees/{treeId}/feed/comments/{commentId}/reaction")
public ApiResponse createCommentReaction(
@PathVariable Long treeId,
@PathVariable Long commentId,
@RequestHeader("Authorization") String header,
@RequestBody ReactionRequestDTO.createReaction request
) {
String token = header.replace("Bearer ", "");
reactionService.reactToComment(treeId, commentId, request, token);
return ApiResponse.onSuccess("");
}

@PostMapping("/trees/{treeId}/feed/replies/{replyId}/reaction")
public ApiResponse createReplyReaction(
@PathVariable Long treeId,
@PathVariable Long replyId,
@RequestHeader("Authorization") String header,
@RequestBody ReactionRequestDTO.createReaction request
) {
String token = header.replace("Bearer ", "");
reactionService.reactToReply(treeId, replyId, request, token);
return ApiResponse.onSuccess("");
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.example.tree.domain.reaction.converter;

import org.example.tree.domain.profile.entity.Profile;
import org.example.tree.domain.reaction.dto.ReactionResponseDTO;
import org.example.tree.domain.reaction.entity.Reaction;
import org.example.tree.domain.reaction.entity.ReactionType;
import org.example.tree.domain.reaction.entity.TargetType;
import org.springframework.stereotype.Component;

@Component
public class ReactionConverter {
public Reaction toPostReaction(Profile profile, Long postId, ReactionType type) {
return Reaction.builder()
.profile(profile)
.type(type)
.targetId(postId)
.targetType(TargetType.POST)
.build();
}

public Reaction toCommentReaction(Profile profile, Long commentId, ReactionType type) {
return Reaction.builder()
.profile(profile)
.type(type)
.targetId(commentId)
.targetType(TargetType.COMMENT)
.build();
}

public Reaction toReplyReaction(Profile profile, Long replyId, ReactionType type) {
return Reaction.builder()
.profile(profile)
.type(type)
.targetId(replyId)
.targetType(TargetType.REPLY)
.build();
}

public ReactionResponseDTO.getReaction toGetReaction(ReactionType type, Integer count, Boolean isPushed) {
return ReactionResponseDTO.getReaction.builder()
.content(type.name())
.number(count)
.isPushed(isPushed)
.build();
}


}
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
package org.example.tree.domain.reaction.dto;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.example.tree.domain.reaction.entity.ReactionType;

@NoArgsConstructor(access = AccessLevel.PRIVATE)

public class ReactionRequestDTO {

@Getter
public static class createReaction {
private ReactionType type;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
package org.example.tree.domain.reaction.dto;

import lombok.*;

@NoArgsConstructor(access = AccessLevel.PRIVATE)

public class ReactionResponseDTO {
@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class getReaction {
private String content;
private Integer number;
private Boolean isPushed;
}
}
Loading

0 comments on commit 9f7711d

Please sign in to comment.