-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathProcessFacade.java
110 lines (95 loc) · 4.63 KB
/
ProcessFacade.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
106
107
108
109
110
package com.cruru.process.service.facade;
import com.cruru.applicant.controller.dto.ApplicantCardResponse;
import com.cruru.applicant.domain.Applicant;
import com.cruru.applicant.service.ApplicantService;
import com.cruru.applyform.domain.ApplyForm;
import com.cruru.applyform.service.ApplyFormService;
import com.cruru.auth.controller.dto.LoginProfile;
import com.cruru.auth.util.AuthChecker;
import com.cruru.dashboard.domain.Dashboard;
import com.cruru.dashboard.service.DashboardService;
import com.cruru.evaluation.service.EvaluationService;
import com.cruru.member.domain.Member;
import com.cruru.member.service.MemberService;
import com.cruru.process.controller.dto.ProcessCreateRequest;
import com.cruru.process.controller.dto.ProcessResponse;
import com.cruru.process.controller.dto.ProcessResponses;
import com.cruru.process.controller.dto.ProcessUpdateRequest;
import com.cruru.process.domain.Process;
import com.cruru.process.service.ProcessService;
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 ProcessFacade {
private final MemberService memberService;
private final ProcessService processService;
private final DashboardService dashboardService;
private final ApplicantService applicantService;
private final EvaluationService evaluationService;
private final ApplyFormService applyFormService;
@Transactional
public void create(LoginProfile loginProfile, ProcessCreateRequest request, long dashboardId) {
Member member = memberService.findByEmail(loginProfile.email());
Dashboard dashboard = dashboardService.findById(dashboardId);
AuthChecker.checkAuthority(dashboard, member);
processService.create(request, dashboard);
}
public ProcessResponses readAllByDashboardId(LoginProfile loginProfile, long dashboardId) {
Member member = memberService.findByEmail(loginProfile.email());
Dashboard dashboard = dashboardService.findById(dashboardId);
AuthChecker.checkAuthority(dashboard, member);
ApplyForm applyForm = applyFormService.findByDashboard(dashboard);
List<Process> processes = processService.findAllByDashboard(dashboard);
List<ProcessResponse> processResponses = toProcessResponses(processes);
return new ProcessResponses(applyForm.getId(), processResponses, applyForm.getTitle());
}
private List<ProcessResponse> toProcessResponses(List<Process> processes) {
return processes.stream()
.map(this::toProcessResponse)
.toList();
}
private ProcessResponse toProcessResponse(Process process) {
List<Applicant> applicantsOfProcess = applicantService.findAllByProcess(process);
List<ApplicantCardResponse> applicantCardResponses = toApplicantCardResponses(process, applicantsOfProcess);
return new ProcessResponse(
process.getId(),
process.getSequence(),
process.getName(),
process.getDescription(),
applicantCardResponses
);
}
private List<ApplicantCardResponse> toApplicantCardResponses(
Process process,
List<Applicant> applicantsOfProcess
) {
return applicantsOfProcess.stream()
.map(applicant -> toApplicantCardResponse(process, applicant))
.toList();
}
private ApplicantCardResponse toApplicantCardResponse(Process process, Applicant applicant) {
int evaluationCount = evaluationService.count(process, applicant);
double averageScore = evaluationService.calculateAverageScore(process, applicant);
return applicantService.toApplicantCardResponse(applicant, evaluationCount, averageScore);
}
@Transactional
public ProcessResponse update(LoginProfile loginProfile, ProcessUpdateRequest request, long processId) {
Member member = memberService.findByEmail(loginProfile.email());
Process process = processService.findById(processId);
AuthChecker.checkAuthority(process, member);
processService.update(request, process.getId());
return toProcessResponse(process);
}
@Transactional
public void delete(LoginProfile loginProfile, long processId) {
Member member = memberService.findByEmail(loginProfile.email());
Process process = processService.findById(processId);
AuthChecker.checkAuthority(process, member);
evaluationService.deleteByProcess(process);
processService.delete(processId);
}
}