Skip to content

Commit

Permalink
[BE] refactor: 서비스 리팩터링 (#540)
Browse files Browse the repository at this point in the history
* feat: 리뷰를 검증하는 서비스 구현

* refactor: 리뷰 등록 서비스가 리뷰를 생성하고 검증 서비스를 통해서 검증하도록 변경

- 서비스명 변경 : CreateReviewService → ReviewRegisterService

* refactor: 답변 검증 서비스가 요청이 아닌 객체를 검증하도록 변경

* feat: 리뷰에서 제공하는 기능 추가

* refactor: dto명 변경 및 사용하지 않는 메서드 삭제

* refactor: 예외명 및 메세지 변경

* refactor: 리뷰 목록 조회 서비스명 변경 및 응답 객체 mapper를 활용하도록 변경

* feat: 리뷰 목록 응답 객체 매핑 서비스 추가

* refactor: dto명 변경으로 인한 수정

* refactor: ReviewMapper를 통해 리뷰를 생성하도록 변경

* refactor: 리뷰 상세 응답 mapper를 활용하도록 변경 및 일부 로직 리팩터링

* refactor: 템플릿을 찾는 로직을 mapper에서 하도록 변경

* refactor: 리팩터링에 따른 컨트롤러 변경사항

* test: 변경사항 반영

* refactor: 질문이 없는 섹션은 응답 생성하지 않도록 변경

* refactor: 답변이 null인 경우에 대한 예외 추가

* refactor: CheckBoxAnswerValidator null 검증 제외 및 질문 유효 검증 추가

* refactor: TextAnswerValidator 질문 유효 검증 추가

* test: Validator 테스트 작성

* test: Mapper 테스트 작성

* test: ReviewValidator 테스트 작성

* test: ReviewRegisterService 테스트 간소화

* test: 리팩터링 사항 반영

* test: Review 테스트 작성

* chore: secret 변경사항 반영

* refactor: 답변 생성 시, null 이 들어올 수 없도록 변경

* refactor: ReviewMapper 에서 답변에 대한 검증 없이 엔티티 매핑만 하도록 변경

* refactor: ReviewValidator 에서 답변에 대한 검증도 진행하도록 변경

* refactor: 메서드명 변경 (getAllQuestionIdsFromAnswers -> getAnsweredQuestionIds)

* refactor: @transactional 어노테이션을 클래스 범위에만 적용하도록 변경

* refactor: 메서드 분리 변경

* refactor: 메서드 시그니쳐 변경

* style : 개행 수정

* refactor: 존재하는 메서드 활용하도록 변경

* refactor: 리뷰에 특적 질문에 대한 답변이 있는지 확인하는 기능 추가

* style: 개행 수정

* refactor: 객체 메서드를 활용하도록 변경

* test: AnswerMapperTest 작성

* refactor: 답변 검증 시, 답변이 빈 경우에 대한 검증도 객체 생성자에서 진행 및 QuestionNotAnsweredException을 domain 하위의 exception 패키지로 변경

* refactor: 메서드명 변경 (hasQuestions -> hasAnsweredQuestion)

* style: 개행 변경

* refactor: ReviewValidator 파라미터를 나눠보 보낼 수 있는 부분 적용

* test: develop 브랜치 머치 후, 미변경 사항 적용
  • Loading branch information
Kimprodp authored Sep 7, 2024
1 parent 6099b31 commit ae477fc
Show file tree
Hide file tree
Showing 50 changed files with 1,426 additions and 1,021 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reviewme.global.HeaderProperty;
import reviewme.review.service.CreateReviewService;
import reviewme.review.service.ReviewRegisterService;
import reviewme.review.service.ReviewDetailLookupService;
import reviewme.review.service.ReviewService;
import reviewme.review.service.dto.request.CreateReviewRequest;
import reviewme.review.service.dto.response.detail.TemplateAnswerResponse;
import reviewme.review.service.ReviewListLookupService;
import reviewme.review.service.dto.request.ReviewRegisterRequest;
import reviewme.review.service.dto.response.detail.ReviewDetailResponse;
import reviewme.review.service.dto.response.list.ReceivedReviewsResponse;

@RestController
Expand All @@ -24,13 +24,13 @@ public class ReviewController {

private static final String GROUP_ACCESS_CODE_HEADER = "GroupAccessCode";

private final CreateReviewService createReviewService;
private final ReviewService reviewService;
private final ReviewRegisterService reviewRegisterService;
private final ReviewListLookupService reviewListLookupService;
private final ReviewDetailLookupService reviewDetailLookupService;

@PostMapping("/v2/reviews")
public ResponseEntity<Void> createReview(@Valid @RequestBody CreateReviewRequest request) {
long savedReviewId = createReviewService.createReview(request);
public ResponseEntity<Void> createReview(@Valid @RequestBody ReviewRegisterRequest request) {
long savedReviewId = reviewRegisterService.registerReview(request);
return ResponseEntity.created(URI.create("/reviews/" + savedReviewId)).build();
}

Expand All @@ -39,17 +39,17 @@ public ResponseEntity<ReceivedReviewsResponse> findReceivedReviews(
@RequestParam String reviewRequestCode,
@HeaderProperty(GROUP_ACCESS_CODE_HEADER) String groupAccessCode
) {
ReceivedReviewsResponse response = reviewService.findReceivedReviews(reviewRequestCode, groupAccessCode);
ReceivedReviewsResponse response = reviewListLookupService.getReceivedReviews(reviewRequestCode, groupAccessCode);
return ResponseEntity.ok(response);
}

@GetMapping("/v2/reviews/{id}")
public ResponseEntity<TemplateAnswerResponse> findReceivedReviewDetail(
public ResponseEntity<ReviewDetailResponse> findReceivedReviewDetail(
@PathVariable long id,
@RequestParam String reviewRequestCode,
@HeaderProperty(GROUP_ACCESS_CODE_HEADER) String groupAccessCode
) {
TemplateAnswerResponse response = reviewDetailLookupService.getReviewDetail(
ReviewDetailResponse response = reviewDetailLookupService.getReviewDetail(
id, reviewRequestCode, groupAccessCode
);
return ResponseEntity.ok(response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import reviewme.review.domain.exception.QuestionNotAnsweredException;

@Entity
@Table(name = "checkbox_answer")
Expand All @@ -38,9 +39,16 @@ public class CheckboxAnswer {
private List<CheckBoxAnswerSelectedOption> selectedOptionIds;

public CheckboxAnswer(long questionId, List<Long> selectedOptionIds) {
validateSelectedOptionIds(questionId, selectedOptionIds);
this.questionId = questionId;
this.selectedOptionIds = selectedOptionIds.stream()
.map(CheckBoxAnswerSelectedOption::new)
.toList();
}

private void validateSelectedOptionIds(long questionId, List<Long> selectedOptionIds) {
if (selectedOptionIds == null || selectedOptionIds.isEmpty()) {
throw new QuestionNotAnsweredException(questionId);
}
}
}
21 changes: 21 additions & 0 deletions backend/src/main/java/reviewme/review/domain/Review.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
Expand Down Expand Up @@ -55,6 +58,24 @@ public Review(long templateId, long reviewGroupId,
this.createdAt = LocalDateTime.now();
}

public Set<Long> getAnsweredQuestionIds() {
return Stream.concat(
textAnswers.stream().map(TextAnswer::getQuestionId),
checkboxAnswers.stream().map(CheckboxAnswer::getQuestionId)
).collect(Collectors.toSet());
}

public Set<Long> getAllCheckBoxOptionIds() {
return checkboxAnswers.stream()
.flatMap(answer -> answer.getSelectedOptionIds().stream())
.map(CheckBoxAnswerSelectedOption::getSelectedOptionId)
.collect(Collectors.toSet());
}

public boolean hasAnsweredQuestion(long questionId) {
return getAnsweredQuestionIds().contains(questionId);
}

public LocalDate getCreatedDate() {
return createdAt.toLocalDate();
}
Expand Down
8 changes: 8 additions & 0 deletions backend/src/main/java/reviewme/review/domain/TextAnswer.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import reviewme.review.domain.exception.QuestionNotAnsweredException;

@Entity
@Table(name = "text_answer")
Expand All @@ -29,7 +30,14 @@ public class TextAnswer {
private String content;

public TextAnswer(long questionId, String content) {
validateContent(questionId, content);
this.questionId = questionId;
this.content = content;
}

private void validateContent(long questionId, String content) {
if (content == null || content.isEmpty()) {
throw new QuestionNotAnsweredException(questionId);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package reviewme.review.domain.exception;

import lombok.extern.slf4j.Slf4j;
import reviewme.global.exception.BadRequestException;

@Slf4j
public class QuestionNotAnsweredException extends BadRequestException {

public QuestionNotAnsweredException(long questionId) {
super("질문에 대한 답변을 작성하지 않았어요.");
log.warn("question must be answered - questionId: {}", questionId, this);
}
}

This file was deleted.

144 changes: 0 additions & 144 deletions backend/src/main/java/reviewme/review/service/CreateReviewService.java

This file was deleted.

Loading

0 comments on commit ae477fc

Please sign in to comment.