Skip to content

Commit

Permalink
refactor: 중복 유효성 검사 제거 및 메소드 추출
Browse files Browse the repository at this point in the history
related to: #18
  • Loading branch information
heejjinkim committed Oct 28, 2024
1 parent 2860184 commit a1142fd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public ResponseEntity<Void> requestReview(@RequestBody @Valid ReviewAskRequest r
@Operation(summary = "리뷰 임시저장 API")
@PostMapping("/draft/{reviewFormId}")
public ResponseEntity<Void> draftReview(@PathVariable(name = "reviewFormId") Long reviewFormId,
@RequestBody @Valid ReviewRequest.ReviewSaveRequest request) {
@RequestBody @Valid ReviewSaveRequest request) {
Long memberId = securityUtil.getCurrentMemberId();
reviewService.draft(memberId, reviewFormId, request);
return ResponseEntity.ok().build();
Expand Down
27 changes: 17 additions & 10 deletions src/main/java/com/_119/wepro/review/service/ReviewService.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public void requestReview(ReviewAskRequest request, Long memberId) {

List<Long> memberIdList = request.getMemberIdList();
memberIdList.forEach(reviewerId -> {
findMemberById(reviewerId);
alarmService.createAlarm(member, reviewerId, AlarmType.REVIEW_REQUEST, request.getReviewFormId());
alarmService.createAlarm(member, reviewerId, AlarmType.REVIEW_REQUEST,
request.getReviewFormId());
});
}

Expand Down Expand Up @@ -96,19 +96,26 @@ public void submitReview(Long memberId, Long reviewFormId, ReviewSaveRequest req
reviewRecordRepository.save(reviewRecord);
}

private ReviewRecord getOrCreateReviewRecord(Member writer, ReviewForm reviewForm, ReviewSaveRequest request) {
private ReviewRecord getOrCreateReviewRecord(Member writer, ReviewForm reviewForm,
ReviewSaveRequest request) {

return reviewRecordRepository.findByReviewForm(reviewForm)
.map(savedRecord -> {
if (!savedRecord.getIsDraft()) {
throw new RestApiException(ReviewErrorCode.ALREADY_SUBMITTED);
}
savedRecord.update(request);
return savedRecord;
})
.map(savedRecord -> updateIfDraft(savedRecord, request))
.orElseGet(() -> ReviewRecord.of(writer, reviewForm, request));
}

private ReviewRecord updateIfDraft(ReviewRecord savedRecord, ReviewSaveRequest request) {
checkIfDraft(savedRecord);
savedRecord.update(request);
return savedRecord;
}

private void checkIfDraft(ReviewRecord savedRecord) {
if (!savedRecord.getIsDraft()) {
throw new RestApiException(ReviewErrorCode.ALREADY_SUBMITTED);
}
}

private Member findMemberById(Long memberId) {
return memberRepository.findById(memberId)
.orElseThrow(() -> new RestApiException(UserErrorCode.USER_NOT_FOUND));
Expand Down

0 comments on commit a1142fd

Please sign in to comment.