Skip to content

Commit

Permalink
refactor: findByIdOrThrow 메서드 repository에 정의
Browse files Browse the repository at this point in the history
related to: #18
  • Loading branch information
heejjinkim committed Oct 28, 2024
1 parent a1142fd commit e909bdb
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package com._119.wepro.member.domain.repository;

import com._119.wepro.global.exception.RestApiException;
import com._119.wepro.global.exception.errorcode.UserErrorCode;
import com._119.wepro.member.domain.Member;
import com._119.wepro.member.domain.OauthInfo;
import io.lettuce.core.dynamic.annotation.Param;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

@Repository
public interface MemberRepository extends JpaRepository<Member, Long> {

Optional<Member> findByOauthInfo(OauthInfo oauthInfo);

@Query("SELECT m FROM Member m WHERE m.oauthInfo.providerId = :providerId")
Optional<Member> findByProviderId(@Param("providerId") String providerId);
default Member findByIdOrThrow(Long memberId) {
return findById(memberId).orElseThrow(() -> new RestApiException(UserErrorCode.USER_NOT_FOUND));
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package com._119.wepro.project.domain.repository;

import com._119.wepro.global.exception.RestApiException;
import com._119.wepro.global.exception.errorcode.ProjectErrorCode;
import com._119.wepro.project.domain.Project;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ProjectRepository extends JpaRepository<Project, Long> {

default Project findByIdOrThrow(Long projectId) {
return findById(projectId).orElseThrow(() -> new RestApiException(ProjectErrorCode.PROJECT_NOT_FOUND));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com._119.wepro.review.domain.repository;

import com._119.wepro.global.enums.CategoryType;
import com._119.wepro.global.exception.RestApiException;
import com._119.wepro.global.exception.errorcode.ReviewErrorCode;
import com._119.wepro.review.domain.ChoiceQuestion;
import java.util.List;
import java.util.Optional;
Expand All @@ -11,4 +13,8 @@
public interface ChoiceQuestionRepository extends JpaRepository<ChoiceQuestion, Long> {

Optional<List<ChoiceQuestion>> findByCategoryType(CategoryType categoryType);

default ChoiceQuestion findByIdOrThrow(Long id) {
return findById(id).orElseThrow(() -> new RestApiException(ReviewErrorCode.QUESTION_NOT_FOUND));
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com._119.wepro.review.domain.repository;

import com._119.wepro.global.exception.RestApiException;
import com._119.wepro.global.exception.errorcode.ReviewErrorCode;
import com._119.wepro.review.domain.ReviewForm;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ReviewFormRepository extends JpaRepository<ReviewForm, Long> {

default ReviewForm findByIdOrThrow(Long reviewFormId) {
return findById(reviewFormId).orElseThrow(() -> new RestApiException(ReviewErrorCode.REVIEW_FORM_NOT_FOUND));
}
}
13 changes: 4 additions & 9 deletions src/main/java/com/_119/wepro/review/service/QuestionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,16 @@ public class QuestionService {
public QuestionInCategoriesGetResponse getQuestionsInCategories(
List<CategoryType> categoryTypes) {

Map<CategoryType, List<ChoiceQuestion>> groupedChoiceQuestions = findChoiceQuestionsByCategories(
Map<CategoryType, List<ChoiceQuestion>> groupedChoiceQuestions = getGroupedQuestionsByCategories(
categoryTypes);
return QuestionInCategoriesGetResponse.of(groupedChoiceQuestions);
}

public QuestionInReviewFormGetResponse getQuestionsInReviewForm(Long reviewFormId) {

ReviewForm reviewForm = findReviewFormById(reviewFormId);
ReviewForm reviewForm = reviewFormRepository.findByIdOrThrow(reviewFormId);
Optional<ReviewRecord> reviewRecord = reviewRecordRepository.findByReviewForm(reviewForm);

validateReviewForm(reviewForm, reviewRecord);

String revieweeName = reviewForm.getMember().getProfile().getName();
Expand All @@ -54,7 +55,7 @@ public QuestionInReviewFormGetResponse getQuestionsInReviewForm(Long reviewFormI
return createResponseFromRecord(revieweeName, choiceQuestions, subQuestions, reviewRecord);
}

private Map<CategoryType, List<ChoiceQuestion>> findChoiceQuestionsByCategories(
private Map<CategoryType, List<ChoiceQuestion>> getGroupedQuestionsByCategories(
List<CategoryType> categoryTypes) {

return categoryTypes.stream()
Expand All @@ -67,12 +68,6 @@ private Map<CategoryType, List<ChoiceQuestion>> findChoiceQuestionsByCategories(
));
}

private ReviewForm findReviewFormById(Long reviewFormId) {

return reviewFormRepository.findById(reviewFormId)
.orElseThrow(() -> new RestApiException(ReviewErrorCode.REVIEW_FORM_NOT_FOUND));
}

private void validateReviewForm(ReviewForm reviewForm, Optional<ReviewRecord> reviewRecord) {

if (reviewForm.getDueDate().isBefore(LocalDate.now())) {
Expand Down
48 changes: 12 additions & 36 deletions src/main/java/com/_119/wepro/review/service/ReviewService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import com._119.wepro.alarm.service.AlarmService;
import com._119.wepro.global.enums.AlarmType;
import com._119.wepro.global.exception.RestApiException;
import com._119.wepro.global.exception.errorcode.ProjectErrorCode;
import com._119.wepro.global.exception.errorcode.ReviewErrorCode;
import com._119.wepro.global.exception.errorcode.UserErrorCode;
import com._119.wepro.member.domain.Member;
import com._119.wepro.member.domain.repository.MemberRepository;
import com._119.wepro.project.domain.Project;
Expand All @@ -18,13 +16,12 @@
import com._119.wepro.review.domain.repository.ReviewFormRepository;
import com._119.wepro.review.domain.repository.ReviewRecordRepository;
import com._119.wepro.review.dto.request.ReviewRequest.ReviewAskRequest;
import com._119.wepro.review.dto.request.ReviewRequest.ReviewSaveRequest;
import com._119.wepro.review.dto.request.ReviewRequest.ReviewFormCreateRequest;
import com._119.wepro.review.dto.request.ReviewRequest.ReviewSaveRequest;
import com._119.wepro.review.dto.response.ReviewResponse.ProjectMemberGetResponse;
import com._119.wepro.review.dto.response.ReviewResponse.ReviewFormCreateResponse;
import jakarta.transaction.Transactional;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand All @@ -45,8 +42,8 @@ public class ReviewService {
@Transactional
public ReviewFormCreateResponse createReviewForm(ReviewFormCreateRequest request, Long memberId) {

Member member = findMemberById(memberId);
Project project = findProjectById(request.getProjectId());
Member member = memberRepository.findByIdOrThrow(memberId);
Project project = projectRepository.findByIdOrThrow(request.getProjectId());
validateQuestionIds(request.getQuestionIdList());

ReviewForm reviewForm = ReviewForm.of(member, project, request.getQuestionIdList());
Expand All @@ -57,18 +54,15 @@ public ReviewFormCreateResponse createReviewForm(ReviewFormCreateRequest request

public void requestReview(ReviewAskRequest request, Long memberId) {

Member member = findMemberById(memberId);

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

public ProjectMemberGetResponse getProjectMembers(Long reviewFormId) {
reviewFormRepository.findById(reviewFormId)
.orElseThrow(() -> new RestApiException(ReviewErrorCode.REVIEW_FORM_NOT_FOUND));
reviewFormRepository.findByIdOrThrow(reviewFormId);
List<ProjectMember> filteredMembers = projectMemberCustomRepository.getProjectMembersWithoutReviewRequest(
reviewFormId);

Expand All @@ -78,8 +72,8 @@ public ProjectMemberGetResponse getProjectMembers(Long reviewFormId) {
@Transactional
public void draft(Long memberId, Long reviewFormId, ReviewSaveRequest request) {

Member writer = findMemberById(memberId);
ReviewForm reviewForm = findReviewFormById(reviewFormId);
Member writer = memberRepository.findByIdOrThrow(memberId);
ReviewForm reviewForm = reviewFormRepository.findByIdOrThrow(reviewFormId);
ReviewRecord reviewRecord = getOrCreateReviewRecord(writer, reviewForm, request);

reviewRecordRepository.save(reviewRecord);
Expand All @@ -88,11 +82,11 @@ public void draft(Long memberId, Long reviewFormId, ReviewSaveRequest request) {
@Transactional
public void submitReview(Long memberId, Long reviewFormId, ReviewSaveRequest request) {

Member writer = findMemberById(memberId);
ReviewForm reviewForm = findReviewFormById(reviewFormId);
Member writer = memberRepository.findByIdOrThrow(memberId);
ReviewForm reviewForm = reviewFormRepository.findByIdOrThrow(reviewFormId);
ReviewRecord reviewRecord = getOrCreateReviewRecord(writer, reviewForm, request);

reviewRecord.submit();

reviewRecordRepository.save(reviewRecord);
}

Expand All @@ -116,25 +110,7 @@ private void checkIfDraft(ReviewRecord savedRecord) {
}
}

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

private Project findProjectById(Long projectId) {
return projectRepository.findById(projectId)
.orElseThrow(() -> new RestApiException(ProjectErrorCode.PROJECT_NOT_FOUND));
}

private ReviewForm findReviewFormById(Long reviewFormId) {
return reviewFormRepository.findById(reviewFormId)
.orElseThrow(() -> new RestApiException(ReviewErrorCode.REVIEW_FORM_NOT_FOUND));
}

private void validateQuestionIds(List<Long> questionIdList) {
questionIdList.forEach(id ->
choiceQuestionRepository.findById(id)
.orElseThrow(() -> new RestApiException(ReviewErrorCode.QUESTION_NOT_FOUND))
);
questionIdList.forEach(choiceQuestionRepository::findByIdOrThrow);
}
}

0 comments on commit e909bdb

Please sign in to comment.