-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathApplicantService.java
105 lines (89 loc) · 3.8 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.applicant.controller.request.ApplicantCreateRequest;
import com.cruru.applicant.controller.request.ApplicantMoveRequest;
import com.cruru.applicant.controller.request.ApplicantUpdateRequest;
import com.cruru.applicant.controller.response.ApplicantResponse;
import com.cruru.applicant.domain.Applicant;
import com.cruru.applicant.domain.dto.ApplicantCard;
import com.cruru.applicant.domain.repository.ApplicantRepository;
import com.cruru.applicant.exception.ApplicantNotFoundException;
import com.cruru.applicant.exception.badrequest.ApplicantRejectException;
import com.cruru.applicant.exception.badrequest.ApplicantUnrejectException;
import com.cruru.process.domain.Process;
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;
@Transactional
public Applicant create(ApplicantCreateRequest request, Process firstProcess) {
return applicantRepository.save(new Applicant(request.name(), request.email(), request.phone(), firstProcess));
}
public List<Applicant> findAllByProcess(Process process) {
return applicantRepository.findAllByProcess(process);
}
@Transactional
public void updateApplicantInformation(long applicantId, ApplicantUpdateRequest request) {
Applicant applicant = findById(applicantId);
if (changeExists(request, applicant)) {
applicant.updateInfo(request.name(), request.email(), request.phone());
}
}
public Applicant findById(Long applicantId) {
return applicantRepository.findById(applicantId)
.orElseThrow(ApplicantNotFoundException::new);
}
private boolean changeExists(ApplicantUpdateRequest request, Applicant applicant) {
return !(applicant.getName().equals(request.name())
&& applicant.getEmail().equals(request.email())
&& applicant.getPhone().equals(request.phone())
);
}
@Transactional
public void moveApplicantProcess(Process process, ApplicantMoveRequest moveRequest) {
List<Applicant> applicants = applicantRepository.findAllById(moveRequest.applicantIds());
applicants.forEach(applicant -> applicant.updateProcess(process));
}
@Transactional
public void reject(long applicantId) {
Applicant applicant = findById(applicantId);
validateRejectable(applicant);
applicant.reject();
}
private void validateRejectable(Applicant applicant) {
if (applicant.isRejected()) {
throw new ApplicantRejectException();
}
}
@Transactional
public void unreject(long applicantId) {
Applicant applicant = findById(applicantId);
validateUnrejectable(applicant);
applicant.unreject();
}
private void validateUnrejectable(Applicant applicant) {
if (applicant.isNotRejected()) {
throw new ApplicantUnrejectException();
}
}
public ApplicantResponse toApplicantResponse(Applicant applicant) {
return new ApplicantResponse(
applicant.getId(),
applicant.getName(),
applicant.getEmail(),
applicant.getPhone(),
applicant.isRejected(),
applicant.getCreatedDate()
);
}
public List<ApplicantCard> findApplicantCards(List<Process> processes) {
return applicantRepository.findApplicantCardsByProcesses(processes);
}
public List<ApplicantCard> findApplicantCards(Process process) {
return applicantRepository.findApplicantCardsByProcesses(process);
}
}