|
| 1 | +package com.moplus.moplus_server.client.submit.service; |
| 2 | + |
| 3 | +import com.moplus.moplus_server.admin.publish.domain.Publish; |
| 4 | +import com.moplus.moplus_server.client.submit.domain.ProblemSubmit; |
| 5 | +import com.moplus.moplus_server.client.submit.dto.response.ChildProblemDetailResponse; |
| 6 | +import com.moplus.moplus_server.client.submit.dto.response.CommentaryGetResponse; |
| 7 | +import com.moplus.moplus_server.client.submit.dto.response.PrescriptionResponse; |
| 8 | +import com.moplus.moplus_server.client.submit.dto.response.ProblemDetailResponse; |
| 9 | +import com.moplus.moplus_server.client.submit.repository.ChildProblemSubmitRepository; |
| 10 | +import com.moplus.moplus_server.client.submit.repository.ProblemSubmitRepository; |
| 11 | +import com.moplus.moplus_server.domain.problem.domain.problem.Problem; |
| 12 | +import com.moplus.moplus_server.domain.problem.repository.ProblemRepository; |
| 13 | +import com.moplus.moplus_server.domain.problemset.domain.ProblemSet; |
| 14 | +import com.moplus.moplus_server.domain.problemset.repository.ProblemSetRepository; |
| 15 | +import com.moplus.moplus_server.domain.publish.repository.PublishRepository; |
| 16 | +import com.moplus.moplus_server.global.error.exception.ErrorCode; |
| 17 | +import com.moplus.moplus_server.global.error.exception.InvalidValueException; |
| 18 | +import com.moplus.moplus_server.global.error.exception.NotFoundException; |
| 19 | +import java.time.LocalDate; |
| 20 | +import java.util.List; |
| 21 | +import lombok.RequiredArgsConstructor; |
| 22 | +import org.springframework.stereotype.Service; |
| 23 | +import org.springframework.transaction.annotation.Transactional; |
| 24 | + |
| 25 | +@Service |
| 26 | +@RequiredArgsConstructor |
| 27 | +public class CommentaryGetService { |
| 28 | + |
| 29 | + private final PublishRepository publishRepository; |
| 30 | + private final ProblemSubmitRepository problemSubmitRepository; |
| 31 | + private final ProblemRepository problemRepository; |
| 32 | + private final ProblemSetRepository problemSetRepository; |
| 33 | + private final ChildProblemSubmitRepository childProblemSubmitRepository; |
| 34 | + |
| 35 | + @Transactional(readOnly = true) |
| 36 | + public CommentaryGetResponse getCommentary(Long publishId, Long problemId) { |
| 37 | + Long memberId = 1L; |
| 38 | + |
| 39 | + // 발행 조회 |
| 40 | + Publish publish = publishRepository.findByIdElseThrow(publishId); |
| 41 | + denyAccessToFuturePublish(publish); |
| 42 | + |
| 43 | + // 문항 제출 조회 |
| 44 | + ProblemSubmit problemSubmit = problemSubmitRepository.findByMemberIdAndPublishIdAndProblemIdElseThrow(memberId, |
| 45 | + publishId, problemId); |
| 46 | + |
| 47 | + // 문항 해설 생성 |
| 48 | + Problem problem = problemRepository.findByIdElseThrow(problemId); |
| 49 | + ProblemDetailResponse mainProblem = ProblemDetailResponse.of( |
| 50 | + problem.getMainProblemImageUrl(), |
| 51 | + problem.getPrescriptionImageUrls(), |
| 52 | + problemSubmit.getStatus() |
| 53 | + ); |
| 54 | + |
| 55 | + // 새끼문항 해설 생성 |
| 56 | + List<ChildProblemDetailResponse> childProblem = problem.getChildProblems().stream() |
| 57 | + .map(cp -> ChildProblemDetailResponse.of( |
| 58 | + cp.getImageUrl(), |
| 59 | + cp.getPrescriptionImageUrls(), |
| 60 | + childProblemSubmitRepository.findByMemberIdAndPublishIdAndChildProblemIdElseThrow(memberId, publishId, |
| 61 | + cp.getId()).getStatus() |
| 62 | + )).toList(); |
| 63 | + |
| 64 | + // 처방 정보 생성 |
| 65 | + PrescriptionResponse prescription = PrescriptionResponse.of(childProblem, mainProblem); |
| 66 | + |
| 67 | + // 문항 번호 추출 |
| 68 | + ProblemSet problemSet = problemSetRepository.findByIdElseThrow(publish.getProblemSetId()); |
| 69 | + List<Long> problemIds = problemSet.getProblemIds(); |
| 70 | + int number = problemIds.indexOf(problemId); |
| 71 | + if (number == -1) { |
| 72 | + throw new NotFoundException(ErrorCode.PROBLEM_NOT_FOUND_IN_PROBLEM_SET); |
| 73 | + } |
| 74 | + |
| 75 | + return CommentaryGetResponse.of( |
| 76 | + number + 1, |
| 77 | + problem.getAnswer(), |
| 78 | + problem.getMainAnalysisImageUrl(), |
| 79 | + problem.getMainHandwritingExplanationImageUrl(), |
| 80 | + problem.getReadingTipImageUrl(), |
| 81 | + problem.getSeniorTipImageUrl(), |
| 82 | + prescription |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + private void denyAccessToFuturePublish(Publish publish){ |
| 87 | + if (publish.getPublishedDate().isAfter(LocalDate.now())) { |
| 88 | + throw new InvalidValueException(ErrorCode.FUTURE_PUBLISH_NOT_ACCESSIBLE); |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments