Skip to content

Commit

Permalink
Merge pull request #49 from Team-Shaka/feat/39
Browse files Browse the repository at this point in the history
✨ Feat : 댓글 작성, 목록 조회, 삭제 API 구현
  • Loading branch information
HyoBN authored Jun 1, 2024
2 parents 8c8624d + 5fcef2b commit 8115734
Show file tree
Hide file tree
Showing 15 changed files with 273 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,53 @@
package treehouse.server.api.comment.business;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import treehouse.server.api.comment.presentation.dto.CommentResponseDTO;
import treehouse.server.api.member.business.MemberMapper;
import treehouse.server.global.entity.User.User;
import treehouse.server.global.entity.comment.Comment;
import treehouse.server.global.entity.member.Member;
import treehouse.server.global.entity.post.Post;

import java.util.List;
import java.util.stream.Collectors;

public class CommentMapper {


public static CommentResponseDTO.CommentInfoDto toCommentInfoDto(Comment comment) {
return CommentResponseDTO.CommentInfoDto.builder()
.commentedAt(comment.getCreatedAt().toString())
.commentId(comment.getId())
.context(comment.getContent())
// .reactionList(comment.) 리액션 관련 로직 구현 후 추가.
.memberProfile(MemberMapper.toGetMemberProfile(comment.getWriter()))
.build();

}

public static CommentResponseDTO.CommentListDto toCommentListDto(List<Comment> commentList) {
List<CommentResponseDTO.CommentInfoDto> commentInfoDtoList = commentList.stream()
.map(comment ->
toCommentInfoDto(comment)).toList();
return CommentResponseDTO.CommentListDto.builder()
.commentList(commentInfoDtoList)
.build();
}

public static Comment toComment(Member writer, Post post, String content) {
return Comment.builder()
.writer(writer)
.post(post)
.content(content)
.build();
}

public static CommentResponseDTO.CommentIdResponseDto toIdResponseDto(Long commentId) {
return CommentResponseDTO.CommentIdResponseDto.builder()
.commentId(commentId)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,53 @@

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import treehouse.server.api.comment.implementation.CommentCommandAdapter;
import treehouse.server.api.comment.implementation.CommentQueryAdapter;
import treehouse.server.api.comment.presentation.dto.CommentRequestDTO;
import treehouse.server.api.comment.presentation.dto.CommentResponseDTO;
import treehouse.server.api.member.implementation.MemberQueryAdapter;
import treehouse.server.api.post.implement.PostQueryAdapter;
import treehouse.server.api.report.business.ReportMapper;
import treehouse.server.api.report.implementation.ReportCommandAdapter;
import treehouse.server.api.report.implementation.ReportQueryAdapter;
import treehouse.server.api.treehouse.implementation.TreehouseQueryAdapter;
import treehouse.server.api.user.implement.UserQueryAdapter;
import treehouse.server.global.entity.User.User;
import treehouse.server.global.entity.comment.Comment;
import treehouse.server.global.entity.member.Member;
import treehouse.server.global.entity.post.Post;
import treehouse.server.global.entity.report.Report;
import treehouse.server.global.entity.treeHouse.TreeHouse;
import treehouse.server.global.exception.GlobalErrorCode;
import treehouse.server.global.exception.ThrowClass.CommentException;

import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
@Slf4j
@Transactional
public class CommentService {

private final ReportCommandAdapter reportCommandAdapter;
private final ReportQueryAdapter reportQueryAdapter;

private final MemberQueryAdapter memberQueryAdapter;

private final CommentQueryAdapter commentQueryAdapter;
private final CommentCommandAdapter commentCommandAdapter;

private final PostQueryAdapter postQueryAdapter;

private final TreehouseQueryAdapter treehouseQueryAdapter;
private final UserQueryAdapter userQueryAdapter;


public void reportComment(User user, CommentRequestDTO.reportComment reqeust, Long treehouseId, Long postId, Long commentId){
Expand All @@ -56,4 +71,37 @@ public void reportComment(User user, CommentRequestDTO.reportComment reqeust, Lo

reportCommandAdapter.createReport(report);
}

@Transactional(readOnly = true)
public CommentResponseDTO.CommentListDto getCommentResponseList(User user, Long postId, int page) {

Pageable pageable = PageRequest.of(page, 10, Sort.by(Sort.Direction.DESC, "createdAt"));
List<Comment> commentListByPostId = commentQueryAdapter.getCommentListByPostId(postId, pageable);

for(Comment comment : commentListByPostId){
if(reportQueryAdapter.isReportedComment(comment)){
commentListByPostId.remove(comment);
}
}
CommentResponseDTO.CommentListDto commentListDto = CommentMapper.toCommentListDto(commentListByPostId);
return commentListDto;
}

public CommentResponseDTO.CommentIdResponseDto createComment(User user, Long treehouseId, Long postId, CommentRequestDTO.createComment request){

TreeHouse treehouse = treehouseQueryAdapter.getTreehouseById(treehouseId);
Post post = postQueryAdapter.findById(postId);
Member writer = memberQueryAdapter.findByUserAndTreehouse(user, treehouse);

Comment comment = CommentMapper.toComment(writer, post, request.getComment());
Long commentId = commentCommandAdapter.createComment(comment).getId();
return CommentMapper.toIdResponseDto(commentId);
}

public void deleteComment(User user, Long treehouseId, Long postId, Long commentId) {

Comment comment = commentQueryAdapter.getCommentById(commentId);

commentCommandAdapter.deleteComment(comment);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
package treehouse.server.api.comment.implementation;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import treehouse.server.api.comment.persistence.CommentRepository;
import treehouse.server.global.annotations.Adapter;
import treehouse.server.global.entity.comment.Comment;

@Adapter
@RequiredArgsConstructor
@Slf4j
public class CommentCommandAdapter {
private final CommentRepository commentRepository;

public Comment createComment(Comment comment) {
return commentRepository.save(comment);
}

public void deleteComment(Comment comment) {
commentRepository.delete(comment);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
import treehouse.server.global.exception.GlobalErrorCode;
import treehouse.server.global.exception.ThrowClass.CommentException;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

import java.util.List;

@Adapter
@RequiredArgsConstructor
@Slf4j
Expand All @@ -18,4 +23,8 @@ public class CommentQueryAdapter {
public Comment getCommentById(Long commentId){
return commentRepository.findById(commentId).orElseThrow(()->new CommentException(GlobalErrorCode.COMMENT_NOT_FOUND));
}

public List<Comment> getCommentListByPostId(Long postId, Pageable pageable) {
return commentRepository.findAllByPostId(postId, pageable);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package treehouse.server.api.comment.persistence;

import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import treehouse.server.global.entity.comment.Comment;

import java.util.List;

public interface CommentRepository extends JpaRepository<Comment, Long> {

List<Comment> findAllByPostId(Long postId, Pageable pageable);

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@
import org.springframework.web.bind.annotation.*;
import treehouse.server.api.comment.business.CommentService;
import treehouse.server.api.comment.presentation.dto.CommentRequestDTO;
import treehouse.server.api.comment.presentation.dto.CommentResponseDTO;
import treehouse.server.global.common.CommonResponse;
import treehouse.server.global.entity.User.User;
import treehouse.server.global.security.handler.annotation.AuthMember;

@RequestMapping("/treehouses/{treehouseId}/feeds/posts/{postId}/comments")
@RequiredArgsConstructor
@RestController
@Slf4j
@Tag(name = "📃 Comment API", description = "트리하우스 댓글 관련 API 입니다.")
@RequestMapping("/treehouses/{treehouseId}/feeds/posts/{postId}/comments")
public class CommentApi {

private final CommentService commentService;
Expand All @@ -36,4 +37,41 @@ public CommonResponse reportComment(
return CommonResponse.onSuccess(null);
}

@Operation(summary = "댓글 목록 조회 API 🔑", description = "댓글 목록을 조회하는 API 입니다.")
@GetMapping()
public CommonResponse<CommentResponseDTO.CommentListDto> getComments(
@PathVariable(name = "treehouseId") Long treehouseId,
@PathVariable(name = "postId") Long postId,
@RequestParam(name = "page", defaultValue = "0") int page,
@AuthMember @Parameter(hidden = true) User user
)
{
return CommonResponse.onSuccess(commentService.getCommentResponseList(user, postId, page));
}

@PostMapping("")
@Operation(summary = "댓글 작성 API 🔑", description = "특정 Post에 대해서 댓글을 작성하는 API 입니다.")
public CommonResponse<CommentResponseDTO.CommentIdResponseDto> createComment(
@PathVariable(name = "treehouseId")Long treehouseId,
@PathVariable(name = "postId")Long postId,
@AuthMember @Parameter(hidden = true) User user,
@RequestBody CommentRequestDTO.createComment request
)
{
return CommonResponse.onSuccess(commentService.createComment(user, treehouseId, postId, request));
}

@DeleteMapping("/{commentId}")
@Operation(summary = "댓글 삭제 API 🔑", description = "댓글을 삭제하는 API 입니다.")
public CommonResponse deleteComment(
@PathVariable(name = "treehouseId")Long treehouseId,
@PathVariable(name = "postId")Long postId,
@PathVariable(name = "commentId")Long commentId,
@AuthMember @Parameter(hidden = true) User user
)
{
commentService.deleteComment(user,commentId,treehouseId,postId);
return CommonResponse.onSuccess(null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@ public static class reportComment{
@NotNull(message = "신고 대상 멤버 아이디는 필수입니다.")
private Long targetMemberId;
}

@Getter
public static class createComment{
// Comment 입력 조건에 따른 valid 조건 추가하기
private String comment;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,44 @@
package treehouse.server.api.comment.presentation.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import treehouse.server.api.member.presentation.dto.MemberResponseDTO;
import treehouse.server.api.reaction.presentation.dto.ReactionResponseDTO;

import java.util.List;

public class CommentResponseDTO {


@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class CommentInfoDto{

MemberResponseDTO.getMemberProfile memberProfile;
ReactionResponseDTO.reactionSimpleResponseDTO reactionList;
Long commentId;
String context;
String commentedAt;
}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class CommentListDto{
List<CommentInfoDto> commentList;

}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class CommentIdResponseDto{
Long commentId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public class MemberQueryAdapter {
private final MemberRepository memberRepository;

public Member getMember(User user){

return memberRepository.findByUser(user).orElseThrow(()-> new MemberException(GlobalErrorCode.USER_NOT_FOUND));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,10 @@ public PostResponseDTO.getPostDetails getPostDetails(User user, Long postId, Lon
}

public PostResponseDTO.createPostResult createPost(User user, PostRequestDTO.createPost request, Long treehouseId) {

Member member = memberQueryAdapter.getMember(user);

TreeHouse treehouse = treehouseQueryAdapter.getTreehouseById(treehouseId);

Member member = memberQueryAdapter.findByUserAndTreehouse(user, treehouse);

Post post = PostMapper.toPost(request, member, treehouse);
List<PostImage> postImageList = PostMapper.toPostImageList(request);
postImageList.forEach(postImage -> postImage.setPost(post));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package treehouse.server.api.reaction.presentation.dto;

public class ReactionRequestDTO {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package treehouse.server.api.reaction.presentation.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;

public class ReactionResponseDTO {

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class reactionSimpleResponseDTO{
String reactionName;
Long reactionCount;
boolean isPushed;
}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class reactionSimpleListDTO{
List<reactionSimpleResponseDTO> simpleResponseDTOList;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package treehouse.server.api.report.implementation;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import treehouse.server.api.report.persistent.ReportRepository;
import treehouse.server.global.annotations.Adapter;
import treehouse.server.global.entity.comment.Comment;

@Adapter
@RequiredArgsConstructor
@Slf4j
public class ReportQueryAdapter {
private final ReportRepository reportRepository;

public boolean isReportedComment(Comment comment){
return reportRepository.existsByComment(comment);
}
}
Loading

0 comments on commit 8115734

Please sign in to comment.