-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathApplicationSubmissionService.java
More file actions
121 lines (104 loc) · 6.36 KB
/
ApplicationSubmissionService.java
File metadata and controls
121 lines (104 loc) · 6.36 KB
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
111
112
113
114
115
116
117
118
119
120
121
package com.example.solidconnection.application.service;
import com.example.solidconnection.application.domain.Application;
import com.example.solidconnection.application.dto.ApplyRequest;
import com.example.solidconnection.application.dto.UniversityChoiceRequest;
import com.example.solidconnection.application.repository.ApplicationRepository;
import com.example.solidconnection.custom.exception.CustomException;
import com.example.solidconnection.score.domain.GpaScore;
import com.example.solidconnection.score.domain.LanguageTestScore;
import com.example.solidconnection.score.repository.GpaScoreRepository;
import com.example.solidconnection.score.repository.LanguageTestScoreRepository;
import com.example.solidconnection.siteuser.domain.SiteUser;
import com.example.solidconnection.siteuser.repository.SiteUserRepository;
import com.example.solidconnection.type.VerifyStatus;
import com.example.solidconnection.university.domain.UniversityInfoForApply;
import com.example.solidconnection.university.repository.UniversityInfoForApplyRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import static com.example.solidconnection.custom.exception.ErrorCode.APPLY_UPDATE_LIMIT_EXCEED;
import static com.example.solidconnection.custom.exception.ErrorCode.CANT_APPLY_FOR_SAME_UNIVERSITY;
import static com.example.solidconnection.custom.exception.ErrorCode.INVALID_GPA_SCORE;
import static com.example.solidconnection.custom.exception.ErrorCode.INVALID_GPA_SCORE_STATUS;
import static com.example.solidconnection.custom.exception.ErrorCode.INVALID_LANGUAGE_TEST_SCORE;
import static com.example.solidconnection.custom.exception.ErrorCode.INVALID_LANGUAGE_TEST_SCORE_STATUS;
@RequiredArgsConstructor
@Service
public class ApplicationSubmissionService {
public static final int APPLICATION_UPDATE_COUNT_LIMIT = 3;
private final ApplicationRepository applicationRepository;
private final UniversityInfoForApplyRepository universityInfoForApplyRepository;
private final GpaScoreRepository gpaScoreRepository;
private final LanguageTestScoreRepository languageTestScoreRepository;
@Value("${university.term}")
private String term;
// 학점 및 어학성적이 모두 유효한 경우에만 지원서 등록이 가능하다.
// 기존에 있던 status field 우선 APRROVED로 입력시킨다.
@Transactional
public boolean apply(SiteUser siteUser, ApplyRequest applyRequest) {
UniversityChoiceRequest universityChoiceRequest = applyRequest.universityChoiceRequest();
Long gpaScoreId = applyRequest.gpaScoreId();
Long languageTestScoreId = applyRequest.languageTestScoreId();
GpaScore gpaScore = getValidGpaScore(siteUser, gpaScoreId);
LanguageTestScore languageTestScore = getValidLanguageTestScore(siteUser, languageTestScoreId);
Optional<Application> application = applicationRepository.findBySiteUserAndTerm(siteUser, term);
UniversityInfoForApply firstChoiceUniversity = universityInfoForApplyRepository
.getUniversityInfoForApplyByIdAndTerm(universityChoiceRequest.firstChoiceUniversityId(), term);
UniversityInfoForApply secondChoiceUniversity = Optional.ofNullable(universityChoiceRequest.secondChoiceUniversityId())
.map(id -> universityInfoForApplyRepository.getUniversityInfoForApplyByIdAndTerm(id, term))
.orElse(null);
UniversityInfoForApply thirdChoiceUniversity = Optional.ofNullable(universityChoiceRequest.thirdChoiceUniversityId())
.map(id -> universityInfoForApplyRepository.getUniversityInfoForApplyByIdAndTerm(id, term))
.orElse(null);
if (application.isEmpty()) {
Application newApplication = new Application(siteUser, gpaScore.getGpa(), languageTestScore.getLanguageTest(),
term, firstChoiceUniversity, secondChoiceUniversity, thirdChoiceUniversity, getRandomNickname());
newApplication.setVerifyStatus(VerifyStatus.APPROVED);
applicationRepository.save(newApplication);
} else {
Application before = application.get();
validateUpdateLimitNotExceed(before);
before.setIsDeleteTrue(); // 기존 이력 soft delete 수행한다.
Application newApplication = new Application(siteUser, gpaScore.getGpa(), languageTestScore.getLanguageTest(),
term, before.getUpdateCount() + 1, firstChoiceUniversity, secondChoiceUniversity, thirdChoiceUniversity, getRandomNickname());
newApplication.setVerifyStatus(VerifyStatus.APPROVED);
applicationRepository.save(newApplication);
}
return true;
}
private GpaScore getValidGpaScore(SiteUser siteUser, Long gpaScoreId) {
GpaScore gpaScore = gpaScoreRepository.findGpaScoreBySiteUserAndId(siteUser, gpaScoreId)
.orElseThrow(() -> new CustomException(INVALID_GPA_SCORE));
if (gpaScore.getVerifyStatus() != VerifyStatus.APPROVED) {
throw new CustomException(INVALID_GPA_SCORE_STATUS);
}
return gpaScore;
}
private LanguageTestScore getValidLanguageTestScore(SiteUser siteUser, Long languageTestScoreId) {
LanguageTestScore languageTestScore = languageTestScoreRepository
.findLanguageTestScoreBySiteUserAndId(siteUser, languageTestScoreId)
.orElseThrow(() -> new CustomException(INVALID_LANGUAGE_TEST_SCORE));
if (languageTestScore.getVerifyStatus() != VerifyStatus.APPROVED) {
throw new CustomException(INVALID_LANGUAGE_TEST_SCORE_STATUS);
}
return languageTestScore;
}
private String getRandomNickname() {
String randomNickname = NicknameCreator.createRandomNickname();
while (applicationRepository.existsByNicknameForApply(randomNickname)) {
randomNickname = NicknameCreator.createRandomNickname();
}
return randomNickname;
}
private void validateUpdateLimitNotExceed(Application application) {
if (application.getUpdateCount() >= APPLICATION_UPDATE_COUNT_LIMIT) {
throw new CustomException(APPLY_UPDATE_LIMIT_EXCEED);
}
}
}