forked from solid-connection/solid-connect-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdminMentorApplicationService.java
More file actions
110 lines (92 loc) · 4.98 KB
/
AdminMentorApplicationService.java
File metadata and controls
110 lines (92 loc) · 4.98 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
package com.example.solidconnection.admin.service;
import static com.example.solidconnection.common.exception.ErrorCode.MENTOR_APPLICATION_NOT_FOUND;
import static com.example.solidconnection.common.exception.ErrorCode.USER_NOT_FOUND;
import com.example.solidconnection.admin.dto.MentorApplicationCountResponse;
import com.example.solidconnection.admin.dto.MentorApplicationHistoryResponse;
import com.example.solidconnection.admin.dto.MentorApplicationRejectRequest;
import com.example.solidconnection.admin.dto.MentorApplicationSearchCondition;
import com.example.solidconnection.admin.dto.MentorApplicationSearchResponse;
import com.example.solidconnection.common.exception.CustomException;
import com.example.solidconnection.mentor.domain.MentorApplication;
import com.example.solidconnection.mentor.domain.MentorApplicationStatus;
import com.example.solidconnection.mentor.repository.MentorApplicationRepository;
import com.example.solidconnection.siteuser.domain.SiteUser;
import com.example.solidconnection.siteuser.repository.SiteUserRepository;
import com.example.solidconnection.university.domain.University;
import com.example.solidconnection.university.repository.UniversityRepository;
import java.util.List;
import java.util.stream.IntStream;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@RequiredArgsConstructor
@Service
public class AdminMentorApplicationService {
private final MentorApplicationRepository mentorApplicationRepository;
private final UniversityRepository universityRepository;
private final SiteUserRepository siteUserRepository;
@Transactional(readOnly = true)
public Page<MentorApplicationSearchResponse> searchMentorApplications(
MentorApplicationSearchCondition mentorApplicationSearchCondition,
Pageable pageable
) {
return mentorApplicationRepository.searchMentorApplications(mentorApplicationSearchCondition, pageable);
}
@Transactional
public void approveMentorApplication(Long mentorApplicationId) {
MentorApplication mentorApplication = mentorApplicationRepository.findById(mentorApplicationId)
.orElseThrow(() -> new CustomException(MENTOR_APPLICATION_NOT_FOUND));
mentorApplication.approve();
}
@Transactional
public void rejectMentorApplication(
Long mentorApplicationId,
MentorApplicationRejectRequest request
) {
MentorApplication mentorApplication = mentorApplicationRepository.findById(mentorApplicationId)
.orElseThrow(() -> new CustomException(MENTOR_APPLICATION_NOT_FOUND));
mentorApplication.reject(request.rejectedReason());
}
@Transactional(readOnly = true)
public MentorApplicationCountResponse getMentorApplicationCount() {
long approvedCount = mentorApplicationRepository.countByMentorApplicationStatus(MentorApplicationStatus.APPROVED);
long pendingCount = mentorApplicationRepository.countByMentorApplicationStatus(MentorApplicationStatus.PENDING);
long rejectedCount = mentorApplicationRepository.countByMentorApplicationStatus(MentorApplicationStatus.REJECTED);
return new MentorApplicationCountResponse(
approvedCount,
pendingCount,
rejectedCount
);
}
@Transactional
public void assignUniversity(
Long mentorApplicationId,
Long universityId
) {
MentorApplication mentorApplication = mentorApplicationRepository.findById(mentorApplicationId)
.orElseThrow(() -> new CustomException(MENTOR_APPLICATION_NOT_FOUND));
mentorApplication.validateCanAssignUniversity();
University university = universityRepository.getUniversityById(universityId);
mentorApplication.assignUniversity(university.getId());
}
@Transactional(readOnly = true)
public List<MentorApplicationHistoryResponse> findMentorApplicationHistory(Long siteUserId) {
SiteUser siteUser = siteUserRepository.findById(siteUserId)
.orElseThrow(() -> new CustomException(USER_NOT_FOUND));
long totalCount = mentorApplicationRepository.countBySiteUserId(siteUser.getId());
List<MentorApplication> mentorApplications = mentorApplicationRepository.findTop5BySiteUserIdOrderByCreatedAtDesc(siteUser.getId());
return IntStream.range(0, mentorApplications.size())
.mapToObj(index -> {
MentorApplication app = mentorApplications.get(index);
return new MentorApplicationHistoryResponse(
app.getId(),
app.getMentorApplicationStatus(),
app.getRejectedReason(),
app.getCreatedAt(),
totalCount - index
);
}).toList();
}
}