Skip to content

Commit

Permalink
Merge pull request #48 from Team-Shaka/feat/47
Browse files Browse the repository at this point in the history
✨  Feat : 댓글 신고 API 구현
  • Loading branch information
CYY1007 authored Jun 1, 2024
2 parents 03f0a91 + 4fbb7a4 commit ee3c9b6
Show file tree
Hide file tree
Showing 14 changed files with 208 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package treehouse.server.api.comment.business;

public class CommentMapper {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package treehouse.server.api.comment.business;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import treehouse.server.api.comment.implementation.CommentQueryAdapter;
import treehouse.server.api.comment.presentation.dto.CommentRequestDTO;
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.treehouse.implementation.TreehouseQueryAdapter;
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.report.Report;
import treehouse.server.global.entity.treeHouse.TreeHouse;
import treehouse.server.global.exception.GlobalErrorCode;
import treehouse.server.global.exception.ThrowClass.CommentException;

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

private final ReportCommandAdapter reportCommandAdapter;

private final MemberQueryAdapter memberQueryAdapter;

private final CommentQueryAdapter commentQueryAdapter;

private final PostQueryAdapter postQueryAdapter;

private final TreehouseQueryAdapter treehouseQueryAdapter;


public void reportComment(User user, CommentRequestDTO.reportComment reqeust, Long treehouseId, Long postId, Long commentId){

TreeHouse treehouse = treehouseQueryAdapter.getTreehouseById(treehouseId);

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

Comment comment = commentQueryAdapter.getCommentById(commentId);

if (!comment.getPost().getId().equals(postId))
throw new CommentException(GlobalErrorCode.COMMENT_NOT_FOUND);

Member target = comment.getWriter();

if (target.equals(reporter))
throw new CommentException(GlobalErrorCode.COMMENT_SELF_REPORT);

Report report = ReportMapper.toCommentReport(reqeust, comment, reporter, target);

reportCommandAdapter.createReport(report);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package treehouse.server.api.comment.implementation;

public class CommentCommandAdapter {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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;
import treehouse.server.global.exception.GlobalErrorCode;
import treehouse.server.global.exception.ThrowClass.CommentException;

@Adapter
@RequiredArgsConstructor
@Slf4j
public class CommentQueryAdapter {

private final CommentRepository commentRepository;

public Comment getCommentById(Long commentId){
return commentRepository.findById(commentId).orElseThrow(()->new CommentException(GlobalErrorCode.COMMENT_NOT_FOUND));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package treehouse.server.api.comment.persistence;

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

public interface CommentRepository extends JpaRepository<Comment, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package treehouse.server.api.comment.presentation;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import treehouse.server.api.comment.business.CommentService;
import treehouse.server.api.comment.presentation.dto.CommentRequestDTO;
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 입니다.")
public class CommentApi {

private final CommentService commentService;

@Operation(summary = "댓글 신고 API 🔑", description = "댓글을 신고하는 API 입니다.")
@PostMapping("/{commentId}/reports")
public CommonResponse reportComment(
@PathVariable(name = "treehouseId") Long treehouseId,
@PathVariable(name = "postId") Long postId,
@PathVariable(name = "commentId") Long commentId,
@Parameter(hidden = true) @AuthMember User user,
@RequestBody @Validated CommentRequestDTO.reportComment request
)
{
commentService.reportComment(user,request,treehouseId,postId,commentId);
return CommonResponse.onSuccess(null);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package treehouse.server.api.comment.presentation.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;

public class CommentRequestDTO {

@Getter
public static class reportComment{

@JsonProperty("reason")
@Schema(description = "신고 사유", example = "부적절한 댓글")
private String reason;

@JsonProperty("targetMemberId")
@Schema(description = "신고 대상 멤버 아이디", example = "1")
@NotNull(message = "신고 대상 멤버 아이디는 필수입니다.")
private Long targetMemberId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package treehouse.server.api.comment.presentation.dto;

public class CommentResponseDTO {
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,4 @@ public static PostResponseDTO.updatePostResult toUpdatePostResult(Post post) {
.postId(post.getId())
.build();
}

public static Report toReport(PostRequestDTO.reportPost request, Post post, Member reporter, Member target){
return Report.builder()
.reason(request.getReason())
.post(post)
.reporterMember(reporter)
.targetMember(target)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import treehouse.server.api.post.implement.PostQueryAdapter;
import treehouse.server.api.post.presentation.dto.PostRequestDTO;
import treehouse.server.api.post.presentation.dto.PostResponseDTO;
import treehouse.server.api.report.business.ReportMapper;
import treehouse.server.api.report.implementation.ReportCommandAdapter;
import treehouse.server.api.treehouse.implementation.TreehouseQueryAdapter;
import treehouse.server.global.constants.Consts;
Expand Down Expand Up @@ -178,7 +179,7 @@ public void reportPost(User user, Long treehouseId, Long postId,PostRequestDTO.r
if (reporter.equals(target))
throw new PostException(GlobalErrorCode.POST_SELF_REPORT);

Report report = PostMapper.toReport(request, post, reporter, target);
Report report = ReportMapper.toPostReport(request, post, reporter, target);

reportCommandAdapter.createReport(report);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package treehouse.server.api.report.business;

import treehouse.server.api.comment.presentation.dto.CommentRequestDTO;
import treehouse.server.api.post.presentation.dto.PostRequestDTO;
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;

public class ReportMapper {

public static Report toPostReport(PostRequestDTO.reportPost request, Post post, Member reporter, Member target){
return Report.builder()
.reason(request.getReason())
.post(post)
.reporterMember(reporter)
.targetMember(target)
.build();
}

public static Report toCommentReport(CommentRequestDTO.reportComment request, Comment comment, Member reporter, Member target){
return Report.builder()
.reason(request.getReason())
.comment(comment)
.reporterMember(reporter)
.targetMember(target)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public class Comment extends BaseDateTimeEntity {
@ManyToOne(fetch = FetchType.LAZY)
private Member writer;

private String content;

@JoinColumn(name = "postId")
@ManyToOne(fetch = FetchType.LAZY)
private Post post;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ public enum GlobalErrorCode implements BaseErrorCode{
// COMMENT + 404 Not Found - 찾을 수 없음
COMMENT_NOT_FOUND(NOT_FOUND, "COMMENT404_1", "존재하지 않는 댓글입니다."),

// COMMENT + 403 Forbidden - 금지됨
COMMENT_SELF_REPORT(FORBIDDEN, "COMMENT403_1", "자신의 댓글은 신고할 수 없습니다."),

// REPLY + 404 Not Found - 찾을 수 없음
REPLY_NOT_FOUND(NOT_FOUND, "REPLY404_1", "존재하지 않는 답글입니다."),

Expand All @@ -76,7 +79,7 @@ public enum GlobalErrorCode implements BaseErrorCode{


// NOTIFICATION + 404 Not Found - 찾을 수 없음
NOTIFICATION_NOT_FOUND(NOT_FOUND, "NOTIFICATION", "존재하지 않는 알림입니다."),
NOTIFICATION_NOT_FOUND(NOT_FOUND, "NOTIFICATION404_1", "존재하지 않는 알림입니다."),

//FEIGN + 400 BAD_REQUEST - 잘못된 요청
FEIGN_CLIENT_ERROR_400(BAD_REQUEST, "FEIGN400", "feignClient 에서 400번대 에러가 발생했습니다."),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package treehouse.server.global.exception.ThrowClass;

import treehouse.server.global.exception.BaseErrorCode;

public class CommentException extends GeneralException{

public CommentException(BaseErrorCode errorCode){
super(errorCode);
}
}

0 comments on commit ee3c9b6

Please sign in to comment.