Skip to content

Commit

Permalink
신고 도메인 리팩터링 (#540)
Browse files Browse the repository at this point in the history
* refactor: (#513) Report 도메인 코드 가독성 리팩토링

* refactor: (#513) Report 도메인 관련 테스트 코드 리팩토링

* refactor: (#513) Report의 Controller, Service를 컨벤션에 맞춰 클래스명 개선

* refactor: (#513) 전략 패턴 클래스들에 대한 테스트 코드 추가

* refactor: (#513) Transactional 어노테이션 클래스에 붙는 것으로 위치 개선

* refactor: (#513) 닉네임 3회 신고 받은 후 해당 멤버에 대한 닉네임 신고 기록 삭제

* refactor: (#513) 각 신고 전략에 관한 상수는 각 클래스에 두는 것으로 개선

* refactor: (#513) 파라미터가 2개인 메서드의 선언문 개행 되돌리기

* refactor: (#513) deleteByReportTypeAndTargetId 메서드의 테스트 코드 추가

* refactor: (#513) 파라미터 2개인 메서드 선언부 개행 삭제
  • Loading branch information
tjdtls690 authored and chsua committed Sep 19, 2023
1 parent a68c06b commit 46aa064
Show file tree
Hide file tree
Showing 21 changed files with 1,083 additions and 310 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@Embeddable
class Content {
public class Content {

private static final int MAXIMUM_LENGTH = 500;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.votogether.domain.member.entity.Member;
import com.votogether.domain.report.dto.request.ReportRequest;
import com.votogether.domain.report.service.ReportService;
import com.votogether.domain.report.service.ReportCommandService;
import com.votogether.global.jwt.Auth;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
Expand All @@ -13,13 +13,13 @@

@RequiredArgsConstructor
@RestController
public class ReportController implements ReportControllerDocs {
public class ReportCommandCommandController implements ReportCommandControllerDocs {

private final ReportService reportService;
private final ReportCommandService reportCommandService;

@PostMapping("/report")
public ResponseEntity<Void> report(@Valid @RequestBody final ReportRequest request, @Auth final Member member) {
reportService.report(member, request);
reportCommandService.report(member, request);
return ResponseEntity.ok().build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.springframework.http.ResponseEntity;

@Tag(name = "신고", description = "신고 API")
public interface ReportControllerDocs {
public interface ReportCommandControllerDocs {

@Operation(summary = "신고", description = "게시글, 댓글, 닉네임을 신고한다.")
@ApiResponses({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ Optional<Report> findByMemberAndReportTypeAndTargetId(

List<Report> findAllByReportTypeAndTargetId(final ReportType reportType, final Long targetId);

void deleteByReportTypeAndTargetId(final ReportType reportType, final Long targetId);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.votogether.domain.report.service;

import com.votogether.domain.member.entity.Member;
import com.votogether.domain.report.dto.request.ReportRequest;
import com.votogether.domain.report.entity.vo.ReportType;
import com.votogether.domain.report.service.strategy.ReportCommentStrategy;
import com.votogether.domain.report.service.strategy.ReportNicknameStrategy;
import com.votogether.domain.report.service.strategy.ReportPostStrategy;
import com.votogether.domain.report.service.strategy.ReportStrategy;
import java.util.EnumMap;
import java.util.Map;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service
public class ReportCommandService {

private final Map<ReportType, ReportStrategy> reportActions;

public ReportCommandService(
final ReportPostStrategy reportPostStrategy,
final ReportCommentStrategy reportCommentStrategy,
final ReportNicknameStrategy reportNicknameStrategy
) {
this.reportActions = new EnumMap<>(ReportType.class);
this.reportActions.put(ReportType.POST, reportPostStrategy);
this.reportActions.put(ReportType.COMMENT, reportCommentStrategy);
this.reportActions.put(ReportType.NICKNAME, reportNicknameStrategy);
}

public void report(final Member reporter, final ReportRequest request) {
final ReportStrategy reportStrategy = reportActions.get(request.type());
reportStrategy.report(reporter, request);
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.votogether.domain.report.service.strategy;

import com.votogether.domain.member.entity.Member;
import com.votogether.domain.post.entity.comment.Comment;
import com.votogether.domain.post.exception.CommentExceptionType;
import com.votogether.domain.post.repository.CommentRepository;
import com.votogether.domain.report.dto.request.ReportRequest;
import com.votogether.domain.report.exception.ReportExceptionType;
import com.votogether.domain.report.repository.ReportRepository;
import com.votogether.global.exception.NotFoundException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@RequiredArgsConstructor
@Component
public class ReportCommentStrategy implements ReportStrategy {

private static final int NUMBER_OF_COMMENT_BLIND_BASED_REPORTS = 5;

private final CommentRepository commentRepository;
private final ReportRepository reportRepository;

@Override
public void report(final Member reporter, final ReportRequest request) {
final Comment reportedComment = commentRepository.findById(request.id())
.orElseThrow(() -> new NotFoundException(CommentExceptionType.COMMENT_NOT_FOUND));
validateComment(reporter, request, reportedComment);

saveReport(reporter, request, reportRepository);
blindComment(request, reportedComment);
}

private void validateComment(
final Member reporter,
final ReportRequest request,
final Comment reportedComment
) {
reportedComment.validateMine(reporter);
reportedComment.validateHidden();
validateDuplicatedReport(
reporter,
request,
ReportExceptionType.DUPLICATE_COMMENT_REPORT,
reportRepository
);
}

private void blindComment(final ReportRequest request, final Comment reportedComment) {
final int reportCount = reportRepository.countByReportTypeAndTargetId(request.type(), request.id());
if (reportCount >= NUMBER_OF_COMMENT_BLIND_BASED_REPORTS) {
reportedComment.blind();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.votogether.domain.report.service.strategy;

import com.votogether.domain.member.entity.Member;
import com.votogether.domain.member.exception.MemberExceptionType;
import com.votogether.domain.member.repository.MemberRepository;
import com.votogether.domain.report.dto.request.ReportRequest;
import com.votogether.domain.report.entity.vo.ReportType;
import com.votogether.domain.report.exception.ReportExceptionType;
import com.votogether.domain.report.repository.ReportRepository;
import com.votogether.global.exception.BadRequestException;
import com.votogether.global.exception.NotFoundException;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@RequiredArgsConstructor
@Component
public class ReportNicknameStrategy implements ReportStrategy {

private static final int NUMBER_OF_NICKNAME_CHANGE_REPORTS = 3;

private final MemberRepository memberRepository;
private final ReportRepository reportRepository;

@Override
public void report(final Member reporter, final ReportRequest request) {
final Member reportedMember = memberRepository.findById(request.id())
.orElseThrow(() -> new NotFoundException(MemberExceptionType.NONEXISTENT_MEMBER));
validateNickname(reporter, request);

saveReport(reporter, request, reportRepository);
changeNicknameByReport(reportedMember, request);
}

private void validateNickname(final Member reporter, final ReportRequest request) {
validateMyNickname(reporter, request);
validateDuplicatedReport(
reporter,
request,
ReportExceptionType.DUPLICATE_NICKNAME_REPORT,
reportRepository
);
}

private void validateMyNickname(final Member reporter, final ReportRequest request) {
if (Objects.equals(reporter.getId(), request.id())) {
throw new BadRequestException(ReportExceptionType.REPORT_MY_NICKNAME);
}
}

private void changeNicknameByReport(final Member reportedMember, final ReportRequest request) {
final int reportCount = reportRepository.countByReportTypeAndTargetId(request.type(), reportedMember.getId());
if (reportCount >= NUMBER_OF_NICKNAME_CHANGE_REPORTS) {
reportedMember.changeNicknameByReport();
reportRepository.deleteByReportTypeAndTargetId(ReportType.NICKNAME, request.id());
}
}

}
Loading

0 comments on commit 46aa064

Please sign in to comment.