Skip to content

Commit

Permalink
Merge pull request #80 from Team-Shaka/feat/79
Browse files Browse the repository at this point in the history
Feat/79 트리하우스 멤버 프로필, 멤버가 작성한 게시글 조회 API 구현, 댓글 시각 형식 변경
  • Loading branch information
HyoBN authored Jul 10, 2024
2 parents 406f985 + d70e56a commit b4e3a8e
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import treehouse.server.api.comment.presentation.dto.CommentResponseDTO;
import treehouse.server.api.member.business.MemberMapper;
import treehouse.server.api.reaction.presentation.dto.ReactionResponseDTO;
import treehouse.server.global.common.util.TimeFormatter;
import treehouse.server.global.entity.comment.Comment;
import treehouse.server.global.entity.comment.CommentType;
import treehouse.server.global.entity.member.Member;
Expand All @@ -16,7 +17,7 @@ public class CommentMapper {
public static CommentResponseDTO.CommentInfoDto toCommentInfoDto(Comment comment, ReactionResponseDTO.getReactionList reactionList,
List<CommentResponseDTO.ReplyInfoDto> replyInfoDtoList) {
return CommentResponseDTO.CommentInfoDto.builder()
.commentedAt(comment.getCreatedAt().toString())
.commentedAt(TimeFormatter.format(comment.getCreatedAt()))
.commentId(comment.getId())
.context(comment.getContent())
.reactionList(reactionList)
Expand All @@ -28,7 +29,7 @@ public static CommentResponseDTO.CommentInfoDto toCommentInfoDto(Comment comment

public static CommentResponseDTO.ReplyInfoDto toReplyInfoDto(Comment comment, ReactionResponseDTO.getReactionList reactionList) {
return CommentResponseDTO.ReplyInfoDto.builder()
.commentedAt(comment.getCreatedAt().toString())
.commentedAt(TimeFormatter.format(comment.getCreatedAt()))
.commentId(comment.getId())
.context(comment.getContent())
.reactionList(reactionList)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,8 @@ public CommentResponseDTO.CommentListDto getCommentResponseList(User user, Long
));
ReactionResponseDTO.getReactionList reactionDtoList = ReactionMapper.toGetReactionList(reactionMap);

// 여기서 comment 에 대한 reply 목록을 조회해서 List<ReplyInfoDto> 를 만든 다음, mapper에 매개변수로 넣고, mapper 코드 업데이트 하자.

// 1. queryAdapter 에 parentId 가지고 childList 조회하는 메서드 만들기
List<Comment> childCommentList = commentQueryAdapter.getChildCommentListByPostIdAndParentId(postId, comment.getId(), pageable);

// 2. mapper 에 childList를 각각 comment 에서 ReplyInfoDto 로 바꾸는 메서드 만들기
// 각 답글마다 reactionList를 생성
List<CommentResponseDTO.ReplyInfoDto> replyInfoDtoList = childCommentList.stream()
.map(reply -> {
List<Reaction> replyReactions = reactionQueryAdapter.findAllByComment(reply);
Expand All @@ -143,9 +138,6 @@ public CommentResponseDTO.CommentListDto getCommentResponseList(User user, Long
})
.collect(Collectors.toList());


// 3. mapper 에 이렇게 만든 childList 를 포함해서 최종 응답 형태인 CommentList 로 바꾸는 메서드 만들고 호출하기

return CommentMapper.toCommentInfoDto(comment, reactionDtoList,replyInfoDtoList);
})
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ public static MemberResponseDTO.getProfile toGetProfile(List<Branch> branches, M
.build();
}

public static MemberResponseDTO.getProfile toGetMemberProfile(List<Branch> branches, Member member, Member targetMember) {
return MemberResponseDTO.getProfile.builder()
.memberId(targetMember.getId())
.memberName(targetMember.getName())
.userName(targetMember.getUser().getName())
.closestMemberCount(BranchUtil.countClosestMembers(branches, targetMember.getId())) // ClosestMember 기능 개발 이후 변경 예정
.treehouseCount(targetMember.getUser().getMemberList().size()) // TreehouseCount 기능 개발 이후 변경 예정
.fromMe(BranchUtil.calculateBranchDegree(branches, member.getId(), targetMember.getId()))
.profileImageUrl(targetMember.getProfileImageUrl())
.bio(targetMember.getBio())
.build();
}

public static MemberResponseDTO.updateProfile toUpdateProfile(Member member) {
return MemberResponseDTO.updateProfile.builder()
.memberId(member.getId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ public MemberResponseDTO.getProfile getMyProfile(User user, Long treehouseId){
return MemberMapper.toGetProfile(branches, member, member);
}

public MemberResponseDTO.getProfile getMemberProfile(User user, Long memberId, Long treehouseId){
TreeHouse treeHouse = treehouseQueryAdapter.getTreehouseById(treehouseId);
Member member = memberQueryAdapter.findByUserAndTreehouse(user, treeHouse);
Member targetMember = memberQueryAdapter.findById(memberId);
List<Branch> branches = branchQueryAdapter.findAllByTreeHouse(treeHouse); // 트리하우스 내 모든 브랜치 조회
return MemberMapper.toGetMemberProfile(branches, member, targetMember);
}



@Transactional
public MemberResponseDTO.updateProfile updateProfile(User user, Long treehouseId, MemberRequestDTO.updateProfile request){
TreeHouse treeHouse = treehouseQueryAdapter.getTreehouseById(treehouseId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import treehouse.server.api.member.business.MemberService;
import treehouse.server.api.member.presentation.dto.MemberRequestDTO;
import treehouse.server.api.member.presentation.dto.MemberResponseDTO;
import treehouse.server.api.post.business.PostService;
import treehouse.server.api.post.presentation.dto.PostResponseDTO;
import treehouse.server.global.common.CommonResponse;
import treehouse.server.global.entity.User.User;
import treehouse.server.global.security.handler.annotation.AuthMember;
Expand All @@ -22,13 +24,14 @@
public class MemberApi {

private final MemberService memberService;
private final PostService postService;

@PostMapping("/members/register")
@Operation(summary = "트리하우스 회원가입 \uD83D\uDD11✅", description = "트리하우스 멤버로 가입합니다.")
public CommonResponse<MemberResponseDTO.registerMember> registerTreehouseMember(
@RequestBody final MemberRequestDTO.registerMember request,
@AuthMember @Parameter(hidden = true) User user
){
) {
return CommonResponse.onSuccess(memberService.register(user, request));
}

Expand All @@ -37,17 +40,39 @@ public CommonResponse<MemberResponseDTO.registerMember> registerTreehouseMember(
public CommonResponse<MemberResponseDTO.getProfile> getMyProfile(
@PathVariable final Long treehouseId,
@AuthMember @Parameter(hidden = true) User user
){
) {
return CommonResponse.onSuccess(memberService.getMyProfile(user, treehouseId));
}

@GetMapping("/treehouses/{treehouseId}/profiles/{memberId}")
@Operation(summary = "멤버 프로필 조회 \uD83D\uDC64 ✅", description = "특정 트리하우스에서 특정 멤버의 프로필을 조회합니다.")
public CommonResponse<MemberResponseDTO.getProfile> getMemberProfile(
@PathVariable(name = "treehouseId") Long treehouseId,
@PathVariable(name = "memberId") Long memberId,
@AuthMember @Parameter(hidden = true) User user
) {
return CommonResponse.onSuccess(memberService.getMemberProfile(user, memberId, treehouseId));
}


@PatchMapping("/treehouses/{treehouseId}/profiles/myProfile")
@Operation(summary = "내 프로필 수정 \uD83D\uDC64 ✅", description = "특정 트리하우스에서 내 프로필을 수정합니다.")
public CommonResponse<MemberResponseDTO.updateProfile> updateProfile(
@PathVariable final Long treehouseId,
@RequestBody final MemberRequestDTO.updateProfile request,
@AuthMember @Parameter(hidden = true) User user
){
) {
return CommonResponse.onSuccess(memberService.updateProfile(user, treehouseId, request));
}

@GetMapping("/treehouses/{treehouseId}/profiles/{memberId}/posts")
@Operation(summary = "멤버가 작성한 게시글 조회 \uD83D\uDC64 ✅", description = "특정 트리하우스에서 특정 멤버가 작성한 게시글 목록을 조회합니다.")
public CommonResponse<PostResponseDTO.getMemberPostList> getPosts(
@PathVariable(name = "treehouseId") Long treehouseId,
@PathVariable(name = "memberId") Long memberId,
@RequestParam(defaultValue = "0") int page,
@AuthMember @Parameter(hidden = true) User user
) {
return CommonResponse.onSuccess(postService.getMemberPosts(user, memberId, treehouseId, page));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ public static class getProfile {
public static class updateProfile {
private Long memberId;
}

}
18 changes: 18 additions & 0 deletions src/main/java/treehouse/server/api/post/business/PostMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@ public static PostResponseDTO.getPostDetails toGetPostDetails(Post post, List<St
.build();
}

public static PostResponseDTO.getOnlyPostDetail toGetOnlyPostDetails(Post post, List<String> postImageUrlList, ReactionResponseDTO.getReactionList reactionList) {
return PostResponseDTO.getOnlyPostDetail.builder()
.postId(post.getId())
.context(post.getContent())
.pictureUrlList(postImageUrlList)
.commentCount(post.getCommentList().size())
.reactionList(reactionList)
.postedAt(TimeFormatter.format(post.getCreatedAt()))
.build();
}

public static PostResponseDTO.getMemberPostList toGetMemberPostList(Member targetMember, List<PostResponseDTO.getOnlyPostDetail> onlyPostDetailList) {
return PostResponseDTO.getMemberPostList.builder()
.memberProfile(MemberMapper.toGetWriterProfile(targetMember))
.postList(onlyPostDetailList)
.build();
}

public static Post toPost(PostRequestDTO.createPost request, Member member, TreeHouse treeHouse){
return Post.builder()
.content(request.getContext())
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/treehouse/server/api/post/business/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,44 @@ public List<PostResponseDTO.getPostDetails> getPosts (User user, Long treehouseI
return postDtoList;
}

public PostResponseDTO.getMemberPostList getMemberPosts(User user, Long targetMemberId, Long treehouseId, int page) {

Pageable pageable = PageRequest.of(page, 10, Sort.by(Sort.Direction.DESC, "createdAt"));
TreeHouse treehouse = treehouseQueryAdapter.getTreehouseById(treehouseId);
Member member = memberQueryAdapter.findByUserAndTreehouse(user, treehouse);
Member targetMember = memberQueryAdapter.findById(targetMemberId);

List<Post> postListByMember = postQueryAdapter.findAllByTreeHouseAndWriter(treehouse, targetMember, pageable);


List<PostResponseDTO.getOnlyPostDetail> postDtoList = postListByMember.stream()
.map(post -> {
List<PostImage> postImageList = post.getPostImageList();
List<String> postImageUrlList= postImageList.stream()
.map(PostImage::getImageUrl)
.toList();
List<Reaction> reactions = reactionQueryAdapter.findAllByPost(post);
Map<String, ReactionResponseDTO.getReaction> reactionMap = reactions.stream()
.collect(Collectors.toMap(
Reaction::getReactionName,
reaction -> {
String reactionName = reaction.getReactionName();
Integer reactionCount = reactionQueryAdapter.countReactionsByReactionNameAndPostId(reactionName, post.getId());
Boolean isPushed = reactionQueryAdapter.existByMemberAndPostAndReactionName(member, post, reactionName);
return ReactionMapper.toGetReaction(reaction, reactionCount, isPushed);
},
(existing, replacement) -> existing // 중복되는 경우 기존 값을 사용
));

ReactionResponseDTO.getReactionList reactionDtoList = ReactionMapper.toGetReactionList(reactionMap);
return PostMapper.toGetOnlyPostDetails(post, postImageUrlList, reactionDtoList);
})
.collect(Collectors.toList());

return PostMapper.toGetMemberPostList(targetMember, postDtoList);
}


@Transactional
public PostResponseDTO.updatePostResult updatePost(User user, Long treehouseId, Long postId, PostRequestDTO.updatePost request) {
//TODO 현재 로그인 한 사용자가 게시글 작성자인지 확인하는 로직 개선
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.data.domain.Pageable;
import treehouse.server.api.post.persistence.PostRepository;
import treehouse.server.global.annotations.Adapter;
import treehouse.server.global.entity.member.Member;
import treehouse.server.global.entity.post.Post;
import treehouse.server.global.entity.treeHouse.TreeHouse;
import treehouse.server.global.exception.GlobalErrorCode;
Expand All @@ -27,4 +28,8 @@ public Post findById(Long postId) {
public List<Post> findAllByTreehouse(TreeHouse treehouse, Pageable pageable) {
return postRepository.findAllByTreeHouse(treehouse, pageable);
}

public List<Post> findAllByTreeHouseAndWriter(TreeHouse treeHouse, Member writer, Pageable pageable) {
return postRepository.findAllByTreeHouseAndWriter(treeHouse, writer, pageable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import treehouse.server.global.entity.member.Member;
import treehouse.server.global.entity.post.Post;
import treehouse.server.global.entity.treeHouse.TreeHouse;

Expand All @@ -13,4 +14,6 @@

public interface PostRepository extends JpaRepository<Post, Long> {
List<Post> findAllByTreeHouse(TreeHouse treehouse, Pageable pageable);

List<Post> findAllByTreeHouseAndWriter(TreeHouse treeHouse, Member writer, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,28 @@ public static class getPostDetails {
private String postedAt;
}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class getOnlyPostDetail {
private Long postId;
private String context;
private List<String> pictureUrlList;
private Integer commentCount;
private ReactionResponseDTO.getReactionList reactionList;
private String postedAt;
}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class getMemberPostList {
private MemberResponseDTO.getWriterProfile memberProfile;
List<getOnlyPostDetail> postList;
}

@Builder
@Getter
@NoArgsConstructor
Expand Down

0 comments on commit b4e3a8e

Please sign in to comment.