-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathApplyFormFacade.java
79 lines (69 loc) · 3.35 KB
/
ApplyFormFacade.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
package com.cruru.applyform.service.facade;
import com.cruru.answer.service.AnswerService;
import com.cruru.applicant.controller.dto.ApplicantCreateRequest;
import com.cruru.applicant.domain.Applicant;
import com.cruru.applicant.service.ApplicantService;
import com.cruru.applyform.controller.dto.AnswerCreateRequest;
import com.cruru.applyform.controller.dto.ApplyFormResponse;
import com.cruru.applyform.controller.dto.ApplyFormSubmitRequest;
import com.cruru.applyform.domain.ApplyForm;
import com.cruru.applyform.exception.badrequest.InvalidSubmitDateException;
import com.cruru.applyform.exception.badrequest.PersonalDataCollectDisagreeException;
import com.cruru.applyform.service.ApplyFormService;
import com.cruru.dashboard.domain.Dashboard;
import com.cruru.process.domain.Process;
import com.cruru.process.service.ProcessService;
import com.cruru.question.domain.Question;
import com.cruru.question.service.QuestionService;
import java.time.LocalDateTime;
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 ApplyFormFacade {
private final QuestionService questionService;
private final ApplyFormService applyFormService;
private final ProcessService processService;
private final ApplicantService applicantService;
private final AnswerService answerService;
public ApplyFormResponse readApplyFormById(long applyFormId) {
ApplyForm applyForm = applyFormService.findById(applyFormId);
List<Question> questions = questionService.findByApplyForm(applyForm);
return new ApplyFormResponse(
applyForm.getTitle(),
applyForm.getDescription(),
applyForm.getStartDate(),
applyForm.getEndDate(),
questionService.toQuestionResponses(questions)
);
}
@Transactional
public void submit(long applyFormId, ApplyFormSubmitRequest request) {
validatePersonalDataCollection(request);
ApplyForm applyForm = applyFormService.findById(applyFormId);
validateSubmitDate(applyForm);
Dashboard dashboard = applyForm.getDashboard();
Process firstProcess = processService.findApplyProcessOnDashboard(dashboard);
ApplicantCreateRequest applicantCreateRequest = request.applicantCreateRequest();
Applicant applicant = applicantService.create(applicantCreateRequest, firstProcess);
List<AnswerCreateRequest> answerCreateRequests = request.answerCreateRequest();
for (AnswerCreateRequest answerCreateRequest : answerCreateRequests) {
Question targetQuestion = questionService.findById(answerCreateRequest.questionId());
answerService.saveAnswerReplies(answerCreateRequest, targetQuestion, applicant);
}
}
private void validatePersonalDataCollection(ApplyFormSubmitRequest request) {
if (!request.personalDataCollection()) {
throw new PersonalDataCollectDisagreeException();
}
}
private void validateSubmitDate(ApplyForm applyForm) {
LocalDateTime now = LocalDateTime.now();
if (now.isBefore(applyForm.getStartDate()) || now.isAfter(applyForm.getEndDate())) {
throw new InvalidSubmitDateException();
}
}
}