Skip to content

Commit

Permalink
Merge pull request #202 from shared-solutions/feat#195
Browse files Browse the repository at this point in the history
Feat#195 글/댓글 신고 추가
  • Loading branch information
MinYeongPark authored Jul 14, 2024
2 parents 6276b3b + c6c771c commit 601337b
Show file tree
Hide file tree
Showing 18 changed files with 275 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@

@Getter
@AllArgsConstructor
public enum
ErrorStatus implements BaseErrorCode {

public enum ErrorStatus implements BaseErrorCode {

// 가장 일반적인 응답
_INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "COMMON5000", "서버 에러, 관리자에게 문의 바랍니다."),
Expand Down Expand Up @@ -61,7 +59,9 @@
POST_SCRAP_DUPLICATE(HttpStatus.BAD_REQUEST, "POST4015", "글에 대한 스크랩 데이터가 이미 존재합니다."),
DEADLINE_OVER(HttpStatus.BAD_REQUEST, "POST4016", "투표 마감 시간이 지났습니다"),
ALREADY_VOTE(HttpStatus.BAD_REQUEST, "POST4017", "이미 투표 하셨습니다."),
// USER_VOTE(HttpStatus.BAD_REQUEST,"POST4017","작성자는 투표가 불가능 합니다 하셨습니다."),
// USER_VOTE(HttpStatus.BAD_REQUEST,"POST4017","작성자는 투표가 불가능 합니다 하셨습니다."), // 이거 필요없으면 지워야 할 것 같습니다!

POST_REPORT_DUPLICATE(HttpStatus.BAD_REQUEST, "POST4018", "이 유저가 해당 글을 신고한 신고 내역 데이터가 이미 존재합니다."),

// 댓글 관련 응답
COMMENT_NOT_FOUND(HttpStatus.NOT_FOUND, "COMMENT4001", "댓글을 찾을 수 없습니다."),
Expand All @@ -71,6 +71,7 @@
COMMENT_NOT_CORRECT_USER(HttpStatus.BAD_REQUEST, "COMMENT4005", "올바른 사용자(댓글 작성자)가 아닙니다."),
COMMENT_POST_NOT_MATCH(HttpStatus.BAD_REQUEST, "COMMENT4006", "해당 글에 작성된 댓글이 아닙니다."),
COMMENT_LIKE_DUPLICATE(HttpStatus.BAD_REQUEST, "COMMENT4007", "댓글에 대한 좋아요 데이터가 이미 존재합니다."),
COMMENT_REPORT_DUPLICATE(HttpStatus.BAD_REQUEST, "COMMENT4008", "이 유저가 해당 댓글을 신고한 신고 내역 데이터가 이미 존재합니다."),

// 알림 관련 응답
ALARM_NOT_FOUND(HttpStatus.NOT_FOUND, "ALARM4001", "알림이 없습니다"),
Expand Down
22 changes: 19 additions & 3 deletions src/main/java/friend/spring/converter/CommentConverter.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package friend.spring.converter;

import friend.spring.domain.Comment;
import friend.spring.domain.Post;
import friend.spring.domain.User;
import friend.spring.domain.*;
import friend.spring.domain.enums.CommentState;
import friend.spring.domain.enums.ReportType;
import friend.spring.domain.mapping.Comment_choice;
import friend.spring.domain.mapping.Comment_like;
import friend.spring.web.dto.CommentRequestDTO;
import friend.spring.web.dto.CommentResponseDTO;
import friend.spring.web.dto.PostResponseDTO;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -116,4 +116,20 @@ public static CommentResponseDTO.myCommentRes toMyCommentResDTO(Comment comment)
.build();
}

public static CommentResponseDTO.CommentReportRes toCommentReportRes(PostResponseDTO.ReportResult reportResult) {
return CommentResponseDTO.CommentReportRes.builder()
.reportId(reportResult.getReport().getId())
.createdAt(reportResult.getReport().getCreatedAt())
.duplicatedReport(reportResult.getDuplicatedReport())
.build();
}

public static Report toReportComment(Comment comment, User user, ReportCategory reportCategory) {
return Report.builder()
.targetType(ReportType.COMMENT)
.targetId(comment.getId())
.reportCategory(reportCategory)
.user(user)
.build();
}
}
20 changes: 18 additions & 2 deletions src/main/java/friend/spring/converter/PostConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import friend.spring.domain.enums.PostState;
import friend.spring.domain.enums.PostType;
import friend.spring.domain.enums.PostVoteType;
import friend.spring.domain.enums.ReportType;
import friend.spring.service.PostQueryService;
import friend.spring.web.dto.*;
import org.springframework.data.domain.Page;
Expand All @@ -19,10 +20,8 @@

import friend.spring.domain.mapping.Post_like;
import friend.spring.domain.mapping.Post_scrap;
import friend.spring.web.dto.CandidateResponseDTO;
import friend.spring.web.dto.PostRequestDTO;
import friend.spring.web.dto.PostResponseDTO;
import software.amazon.awssdk.services.s3.endpoints.internal.Value;

import java.util.List;

Expand Down Expand Up @@ -996,6 +995,23 @@ public static PostResponseDTO.ScrapCreateRes toScrapCreateRes(Post_scrap post_sc
.build();
}

public static Report toReportPost(Post post, User user, ReportCategory reportCategory) {
return Report.builder()
.targetType(ReportType.POST)
.targetId(post.getId())
.reportCategory(reportCategory)
.user(user)
.build();
}

public static PostResponseDTO.PostReportRes toPostReportRes(PostResponseDTO.ReportResult reportResult) {
return PostResponseDTO.PostReportRes.builder()
.reportId(reportResult.getReport().getId())
.createdAt(reportResult.getReport().getCreatedAt())
.duplicatedReport(reportResult.getDuplicatedReport())
.build();
}

public static PostResponseDTO.PostSearchRes PostSearchResDTO(Post post,Long userId) {
Integer likeCount = post.getPostLikeList().size();
Integer commentCount = post.getCommentList().size();
Expand Down
12 changes: 4 additions & 8 deletions src/main/java/friend/spring/domain/Report.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package friend.spring.domain;

import friend.spring.domain.common.BaseEntity;
import friend.spring.domain.enums.ReportCategory;
import friend.spring.domain.enums.ReportType;
import lombok.*;

Expand All @@ -22,14 +21,11 @@ public class Report extends BaseEntity {
private ReportType targetType;

@Column(nullable = false)
private Integer targetId;
private Long targetId;

@Column(nullable = false, length = 500)
private String content;

@Enumerated(EnumType.STRING)
@Column
private ReportCategory category;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "report_category_id")
private ReportCategory reportCategory;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/friend/spring/domain/ReportCategory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package friend.spring.domain;

import friend.spring.domain.common.BaseEntity;
import lombok.*;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity
@Getter
@Builder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public class ReportCategory {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(nullable = false)
private String name;

@Builder.Default
@OneToMany(mappedBy = "reportCategory")
private List<Report> reportList = new ArrayList<>();

}

5 changes: 0 additions & 5 deletions src/main/java/friend/spring/domain/enums/ReportCategory.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package friend.spring.repository;

import friend.spring.domain.Category;
import friend.spring.domain.Report;
import friend.spring.domain.ReportCategory;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ReportCategoryRepository extends JpaRepository<ReportCategory, Long> {
ReportCategory findByName(String category);
}
11 changes: 11 additions & 0 deletions src/main/java/friend/spring/repository/ReportRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package friend.spring.repository;

import friend.spring.domain.Report;
import friend.spring.domain.enums.ReportType;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface ReportRepository extends JpaRepository<Report, Long> {
Optional<Report> findByTargetTypeAndTargetIdAndUserId(ReportType post, Long postId, Long userId);
}
4 changes: 4 additions & 0 deletions src/main/java/friend/spring/service/CommentService.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package friend.spring.service;

import friend.spring.domain.Comment;
import friend.spring.domain.Report;
import friend.spring.domain.mapping.Comment_choice;
import friend.spring.domain.mapping.Comment_like;
import friend.spring.web.dto.CommentRequestDTO;
import friend.spring.web.dto.CommentResponseDTO;
import friend.spring.web.dto.PostResponseDTO;
import org.springframework.data.domain.Page;

import javax.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -36,4 +38,6 @@ public interface CommentService {
Page<Comment> getMyCommentList(Long userId, Integer page);

void deleteComment(Long postId, Long commentId, HttpServletRequest request);

PostResponseDTO.ReportResult createReportComment(Long commentId, CommentRequestDTO.CommentReportReq request, HttpServletRequest request2);
}
40 changes: 40 additions & 0 deletions src/main/java/friend/spring/service/CommentServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@
import friend.spring.apiPayload.GeneralException;
import friend.spring.apiPayload.code.status.ErrorStatus;
import friend.spring.apiPayload.handler.CommentHandler;
import friend.spring.apiPayload.handler.PostHandler;
import friend.spring.converter.AlarmConverter;
import friend.spring.converter.CommentConverter;
import friend.spring.converter.PostConverter;
import friend.spring.converter.SseConverter;
import friend.spring.domain.*;
import friend.spring.domain.enums.AlarmType;
import friend.spring.domain.enums.ReportType;
import friend.spring.domain.mapping.Comment_choice;
import friend.spring.domain.mapping.Comment_like;
import friend.spring.repository.*;
import friend.spring.security.JwtTokenProvider;
import friend.spring.web.dto.CommentRequestDTO;
import friend.spring.web.dto.CommentResponseDTO;
import friend.spring.web.dto.PostResponseDTO;
import friend.spring.web.dto.SseResponseDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
Expand All @@ -38,6 +42,8 @@ public class CommentServiceImpl implements CommentService {
private final CommentChoiceRepository commentChoiceRepository;
private final PointRepository pointRepository;
private final AlarmRepository alarmRepository;
private final ReportRepository reportRepository;
private final ReportCategoryRepository reportCategoryRepository;
private final UserService userService;
private final PostService postService;
private final JwtTokenProvider jwtTokenProvider;
Expand Down Expand Up @@ -400,5 +406,39 @@ public void deleteComment(Long postId, Long commentId, HttpServletRequest reques
}
comment.updateStateToDeleted();
}

@Override
public PostResponseDTO.ReportResult createReportComment(Long commentId, CommentRequestDTO.CommentReportReq request, HttpServletRequest request2) {
Long userId = jwtTokenProvider.getCurrentUser(request2);

Optional<Comment> optionalComment = commentRepository.findById(commentId);
if (optionalComment.isEmpty()) {
this.checkComment(false);
}

Optional<User> optionalUser = userRepository.findById(userId);
if (optionalUser.isEmpty()) {
userService.checkUser(false);
}

Comment comment = optionalComment.get();
User user = optionalUser.get();

// 이미 신고된 건인지 확인
Optional<Report> optionalReport = reportRepository.findByTargetTypeAndTargetIdAndUserId(ReportType.COMMENT, commentId, userId);
if (!optionalReport.isEmpty()) {
return PostResponseDTO.ReportResult.builder()
.report(optionalReport.get())
.duplicatedReport(true)
.build();
}

ReportCategory reportCategory = reportCategoryRepository.findByName(request.getReportCategory());
Report report = CommentConverter.toReportComment(comment, user, reportCategory);
return PostResponseDTO.ReportResult.builder()
.report(reportRepository.save(report))
.duplicatedReport(false)
.build();
}
}

2 changes: 2 additions & 0 deletions src/main/java/friend/spring/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,6 @@ public interface PostService {

void deleteScrapPost(Long postId, HttpServletRequest request);

PostResponseDTO.ReportResult createReportPost(Long postId, PostRequestDTO.PostReportReq request, HttpServletRequest request2);

}
36 changes: 36 additions & 0 deletions src/main/java/friend/spring/service/PostServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import friend.spring.domain.*;
import friend.spring.domain.enums.PostState;
import friend.spring.domain.enums.PostType;
import friend.spring.domain.enums.ReportType;
import friend.spring.domain.enums.S3ImageType;
import friend.spring.domain.mapping.Post_like;
import friend.spring.domain.mapping.Post_scrap;
Expand Down Expand Up @@ -42,6 +43,8 @@ public class PostServiceImpl implements PostService {
private final Card_PollRepository cardPollRepository;
private final PointRepository pointRepository;
private final CategoryRepository categoryRepository;
private final ReportCategoryRepository reportCategoryRepository;
private final ReportRepository reportRepository;

private final PostLikeRepository postLikeRepository;
private final CommentRepository commentRepository;
Expand Down Expand Up @@ -476,4 +479,37 @@ public void deleteScrapPost(Long postId, HttpServletRequest request) {
postScrapRepository.delete(post_scrap);
}

@Override
public PostResponseDTO.ReportResult createReportPost(Long postId, PostRequestDTO.PostReportReq request, HttpServletRequest request2) {
Long userId = jwtTokenProvider.getCurrentUser(request2);

Optional<Post> optionalPost = postRepository.findById(postId);
if (optionalPost.isEmpty()) {
this.checkPost(false);
}

Optional<User> optionalUser = userRepository.findById(userId);
if (optionalUser.isEmpty()) {
userService.checkUser(false);
}

Post post = optionalPost.get();
User user = optionalUser.get();

// 이미 신고된 건인지 확인
Optional<Report> optionalReport = reportRepository.findByTargetTypeAndTargetIdAndUserId(ReportType.POST, postId, userId);
if (!optionalReport.isEmpty()) {
return PostResponseDTO.ReportResult.builder()
.report(optionalReport.get())
.duplicatedReport(true)
.build();
}

ReportCategory reportCategory = reportCategoryRepository.findByName(request.getReportCategory());
Report report = PostConverter.toReportPost(post, user, reportCategory);
return PostResponseDTO.ReportResult.builder()
.report(reportRepository.save(report))
.duplicatedReport(false)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

import friend.spring.apiPayload.ApiResponse;
import friend.spring.converter.CommentConverter;
import friend.spring.converter.PostConverter;
import friend.spring.domain.Comment;
import friend.spring.domain.Report;
import friend.spring.domain.mapping.Comment_choice;
import friend.spring.domain.mapping.Comment_like;
import friend.spring.service.CommentService;
import friend.spring.web.dto.CommentRequestDTO;
import friend.spring.web.dto.CommentResponseDTO;
import friend.spring.web.dto.PostResponseDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
Expand Down Expand Up @@ -197,4 +200,27 @@ public ApiResponse<Void> deleteComment(
commentService.deleteComment(postId, commentId, request);
return ApiResponse.onSuccess(null);
}

// 댓글 신고
@PostMapping("/posts/{post-id}/comment/{comment-id}/report")
@Operation(summary = "댓글 신고 API", description = "댓글 신고하는 API입니다. ex) /posts/1/report")
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 요청에 성공했습니다."),
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "USER4001", description = "NOT_FOUND, 사용자를 찾을 수 없습니다."),
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "POST4001", description = "NOT_FOUND, 글을 찾을 수 없습니다."),
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMENT4008", description = "BAD_REQUEST, 이 유저가 해당 댓글을 신고한 신고 내역 데이터가 이미 존재합니다."),
})
@Parameters({
@Parameter(name = "post-id", description = "path variable - 글 아이디"),
@Parameter(name = "atk", description = "RequestHeader - 로그인한 사용자의 accessToken"),
})
public ApiResponse<CommentResponseDTO.CommentReportRes> createReportComment(
@PathVariable("comment-id") Long commentId,
@RequestBody CommentRequestDTO.CommentReportReq request,
@RequestHeader("atk") String atk,
HttpServletRequest request2
) {
PostResponseDTO.ReportResult commentReport = commentService.createReportComment(commentId, request, request2);
return ApiResponse.onSuccess(CommentConverter.toCommentReportRes(commentReport));
}
}
Loading

0 comments on commit 601337b

Please sign in to comment.