Conversation
There was a problem hiding this comment.
Code Review
This pull request implements a post reporting feature, introducing the Report entity, a ReportReason enum, and the necessary service and controller logic to handle report submissions. The reviewer suggested enhancing the CreateReportService with validation logic to prevent users from reporting their own posts and to block duplicate reports. Additionally, it was recommended to replace the static factory method in the Report entity with the Builder pattern to improve object creation and maintainability.
| User reporter = userRepository.findByUserId(userDetails.getUserId()) | ||
| .orElseThrow(() -> UserNotFoundException.EXCEPTION); | ||
|
|
||
| Report report = Report.create(post, reporter, request.getReason(), request.getDetail()); |
There was a problem hiding this comment.
신고 기능에 대한 몇 가지 중요한 검증 로직이 누락된 것으로 보입니다. 아래 두 가지 검증을 추가하는 것을 강력히 권장합니다.
- 자신이 작성한 게시물 신고 방지: 사용자가 자신의 게시물을 신고하는 것은 일반적인 시나리오가 아니므로, 게시물 작성자와 신고자가 동일 인물인지 확인하여 이를 방지해야 합니다.
- 중복 신고 방지: 한 사용자가 동일한 게시물에 대해 여러 번 신고하는 것을 막아 시스템 악용을 방지해야 합니다.
아래와 같이 reporter를 조회한 후, Report 객체를 생성하기 전에 검증 로직을 추가할 수 있습니다.
// 자신의 게시물 신고 방지
if (post.getWriter().getId().equals(reporter.getId())) {
// 적절한 예외(e.g., 400 Bad Request)를 발생시켜야 합니다.
throw new SelfReportNotAllowedException("자신의 게시물은 신고할 수 없습니다.");
}
// 중복 신고 방지 (ReportRepository에 existsByPostAndReporter 메서드 추가 필요)
if (reportRepository.existsByPostAndReporter(post, reporter)) {
// 적절한 예외(e.g., 409 Conflict)를 발생시켜야 합니다.
throw new DuplicateReportException("이미 신고한 게시물입니다.");
}위의 중복 신고 방지 로직을 위해 ReportRepository에 다음 메서드를 추가해야 합니다.
// ReportRepository.java
boolean existsByPostAndReporter(Post post, User reporter);| public static Report create(Post post, User reporter, ReportReason reason, String detail) { | ||
| Report report = new Report(); | ||
| report.post = post; | ||
| report.reporter = reporter; | ||
| report.reason = reason; | ||
| report.detail = detail; | ||
| return report; | ||
| } |
There was a problem hiding this comment.
현재의 정적 팩토리 메서드는 new Report()로 빈 객체를 생성한 후 필드를 수동으로 할당하고 있습니다. 이 방식보다는 빌더 패턴을 사용하여 객체의 불변성을 보장하고 생성 로직을 더 명확하게 만드는 것이 좋습니다.
@Builder 어노테이션을 클래스에 추가하고, 이 create 메서드를 제거하는 것을 고려해보세요. 서비스 레이어에서는 빌더를 사용하여 Report 객체를 생성하게 됩니다.
Report.java 수정 제안:
@Entity
@Getter
@Builder
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Report extends BaseIdEntity {
// ... 필드들 ...
// create() 메서드는 제거합니다.
}CreateReportService.java에서의 사용 예시:
Report report = Report.builder()
.post(post)
.reporter(reporter)
.reason(request.getReason())
.detail(request.getDetail())
.build();
💡 배경 및 개요
📃 작업내용
🔀 변경사항
🙋♂️ 리뷰노트
✅ PR 체크리스트
.env,노션,README)"API 개발 완료됐어요","환경값 추가되었어요")🎸 기타