-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathApplicantService.java
105 lines (91 loc) · 4.16 KB
/
ApplicantService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package com.cruru.applicant.service;
import com.cruru.answer.domain.Answer;
import com.cruru.answer.domain.repository.AnswerRepository;
import com.cruru.applicant.controller.dto.ApplicantBasicResponse;
import com.cruru.applicant.controller.dto.ApplicantDetailResponse;
import com.cruru.applicant.controller.dto.ApplicantMoveRequest;
import com.cruru.applicant.controller.dto.ApplicantResponse;
import com.cruru.applicant.controller.dto.ApplicantUpdateRequest;
import com.cruru.applicant.controller.dto.QnaResponse;
import com.cruru.applicant.domain.Applicant;
import com.cruru.applicant.domain.repository.ApplicantRepository;
import com.cruru.applicant.exception.ApplicantNotFoundException;
import com.cruru.applicant.exception.badrequest.ApplicantRejectException;
import com.cruru.process.controller.dto.ProcessSimpleResponse;
import com.cruru.process.domain.Process;
import com.cruru.process.domain.repository.ProcessRepository;
import com.cruru.process.exception.ProcessNotFoundException;
import com.cruru.question.domain.Question;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class ApplicantService {
private final ApplicantRepository applicantRepository;
private final ProcessRepository processRepository;
private final AnswerRepository answerRepository;
@Transactional
public void updateApplicantProcess(long processId, ApplicantMoveRequest moveRequest) {
Process process = processRepository.findById(processId)
.orElseThrow(ProcessNotFoundException::new);
List<Applicant> applicants = applicantRepository.findAllById(moveRequest.applicantIds());
applicants.forEach(applicant -> applicant.updateProcess(process));
}
public ApplicantBasicResponse findById(long id) {
Applicant applicant = applicantRepository.findById(id)
.orElseThrow(ApplicantNotFoundException::new);
return toApplicantBasicResponse(applicant);
}
private ApplicantBasicResponse toApplicantBasicResponse(Applicant applicant) {
return new ApplicantBasicResponse(
new ApplicantResponse(
applicant.getId(),
applicant.getName(),
applicant.getEmail(),
applicant.getPhone(),
applicant.getCreatedDate()
),
new ProcessSimpleResponse(
applicant.getProcess().getId(),
applicant.getProcess().getName()
)
);
}
public ApplicantDetailResponse findDetailById(long id) {
Applicant applicant = applicantRepository.findById(id)
.orElseThrow(ApplicantNotFoundException::new);
List<Answer> answers = answerRepository.findAllByApplicant(applicant);
List<QnaResponse> qnaResponses = toQnaResponses(answers);
return new ApplicantDetailResponse(qnaResponses);
}
private List<QnaResponse> toQnaResponses(List<Answer> answers) {
return answers.stream()
.map(this::toQnaResponse)
.toList();
}
private QnaResponse toQnaResponse(Answer answer) {
Question question = answer.getQuestion();
return new QnaResponse(question.getSequence(), question.getContent(), answer.getContent());
}
@Transactional
public void reject(long id) {
Applicant applicant = applicantRepository.findById(id)
.orElseThrow(ApplicantNotFoundException::new);
validateRejectable(applicant);
applicant.reject();
}
private void validateRejectable(Applicant applicant) {
if (applicant.getIsRejected()) {
throw new ApplicantRejectException();
}
}
@Transactional
public void update(ApplicantUpdateRequest request, long applicantId) {
Applicant applicant = applicantRepository.findById(applicantId)
.orElseThrow(ApplicantNotFoundException::new);
applicant.updateInfo(request.name(), request.email(), request.phone());
}
}