Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

신고 도메인 리팩터링 #540

Merged
merged 13 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2
이 메서드에 대한 테스트 코드도 추가되면 좋을 것 같아요!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

레벨 4 미션하면서 테스트 코드 안하다보니까 프로젝트에서도 자연스럽게 안해버렸네요. 테스트 코드 추가하겠습니다.


}
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);
}

Comment on lines +19 to +31
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

코드를 정말 맛있게 바꾸셨군요 👍🏻

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