-
Notifications
You must be signed in to change notification settings - Fork 4
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
신고 도메인 리팩터링 #540
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f0aad00
refactor: (#513) Report 도메인 코드 가독성 리팩토링
tjdtls690 f7a1f0e
refactor: (#513) Report 도메인 관련 테스트 코드 리팩토링
tjdtls690 bbb6891
refactor: (#513) Report의 Controller, Service를 컨벤션에 맞춰 클래스명 개선
tjdtls690 3310f36
refactor: (#513) 전략 패턴 클래스들에 대한 테스트 코드 추가
tjdtls690 61e928b
refactor: (#513) branch 최신화
tjdtls690 331f908
refactor: (#513) Transactional 어노테이션 클래스에 붙는 것으로 위치 개선
tjdtls690 9e65141
Merge branch 'dev' of github.com:woowacourse-teams/2023-votogether in…
tjdtls690 92c1d0e
Merge branch 'dev' of github.com:woowacourse-teams/2023-votogether in…
tjdtls690 174aade
refactor: (#513) 닉네임 3회 신고 받은 후 해당 멤버에 대한 닉네임 신고 기록 삭제
tjdtls690 9222720
refactor: (#513) 각 신고 전략에 관한 상수는 각 클래스에 두는 것으로 개선
tjdtls690 591282c
refactor: (#513) 파라미터가 2개인 메서드의 선언문 개행 되돌리기
tjdtls690 ddb8935
refactor: (#513) deleteByReportTypeAndTargetId 메서드의 테스트 코드 추가
tjdtls690 5592f3a
refactor: (#513) 파라미터 2개인 메서드 선언부 개행 삭제
tjdtls690 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
backend/src/main/java/com/votogether/domain/report/service/ReportCommandService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
|
||
} |
164 changes: 0 additions & 164 deletions
164
backend/src/main/java/com/votogether/domain/report/service/ReportService.java
This file was deleted.
Oops, something went wrong.
55 changes: 55 additions & 0 deletions
55
...nd/src/main/java/com/votogether/domain/report/service/strategy/ReportCommentStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
...d/src/main/java/com/votogether/domain/report/service/strategy/ReportNicknameStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2
이 메서드에 대한 테스트 코드도 추가되면 좋을 것 같아요!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
레벨 4 미션하면서 테스트 코드 안하다보니까 프로젝트에서도 자연스럽게 안해버렸네요. 테스트 코드 추가하겠습니다.