-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathDashboardFacade.java
151 lines (130 loc) · 6.38 KB
/
DashboardFacade.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package com.cruru.dashboard.service.facade;
import com.cruru.applicant.domain.Applicant;
import com.cruru.applicant.service.ApplicantService;
import com.cruru.applyform.controller.dto.ApplyFormWriteRequest;
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.club.domain.Club;
import com.cruru.club.service.ClubService;
import com.cruru.dashboard.controller.dto.DashboardCreateRequest;
import com.cruru.dashboard.controller.dto.DashboardCreateResponse;
import com.cruru.dashboard.controller.dto.DashboardPreviewResponse;
import com.cruru.dashboard.controller.dto.DashboardsOfClubResponse;
import com.cruru.dashboard.controller.dto.StatsResponse;
import com.cruru.dashboard.domain.Dashboard;
import com.cruru.dashboard.service.DashboardService;
import com.cruru.member.domain.Member;
import com.cruru.member.service.MemberService;
import com.cruru.process.domain.Process;
import com.cruru.process.service.ProcessService;
import com.cruru.question.controller.dto.QuestionCreateRequest;
import com.cruru.question.service.QuestionService;
import java.time.Clock;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
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 DashboardFacade {
private final MemberService memberService;
private final ClubService clubService;
private final DashboardService dashboardService;
private final ApplyFormService applyFormService;
private final QuestionService questionService;
private final ProcessService processService;
private final ApplicantService applicantService;
private final Clock clock;
@Transactional
public Dashboard create(LoginProfile loginProfile, long clubId, DashboardCreateRequest request) {
Member member = memberService.findByEmail(loginProfile.email());
Club club = clubService.findById(clubId);
AuthChecker.checkAuthority(club, member);
Dashboard createdDashboard = dashboardService.create(club);
ApplyForm applyForm = applyFormService.create(toApplyFormWriteRequest(request), createdDashboard);
for (QuestionCreateRequest questionCreateRequest : request.questions()) {
questionService.create(questionCreateRequest, applyForm);
}
return createdDashboard;
}
private ApplyFormWriteRequest toApplyFormWriteRequest(DashboardCreateRequest request) {
return new ApplyFormWriteRequest(
request.title(),
request.postingContent(),
request.startDate(),
request.endDate()
);
}
public DashboardCreateResponse findApplyFormByDashboard(Dashboard dashboard) {
ApplyForm applyForm = applyFormService.findByDashboard(dashboard);
return new DashboardCreateResponse(applyForm.getId(), dashboard.getId());
}
public DashboardsOfClubResponse findAllDashboardsByClubId(LoginProfile loginProfile, long clubId) {
Member member = memberService.findByEmail(loginProfile.email());
Club club = clubService.findById(clubId);
AuthChecker.checkAuthority(club, member);
List<Dashboard> dashboards = dashboardService.findAllByClub(club);
String clubName = clubService.findById(clubId).getName();
LocalDateTime now = LocalDateTime.now(clock);
List<DashboardPreviewResponse> dashboardResponses = dashboards.stream()
.map(this::createDashboardPreviewResponse)
.toList();
List<DashboardPreviewResponse> sortedDashboardPreviews = sortDashboardPreviews(dashboardResponses, now);
return new DashboardsOfClubResponse(clubName, sortedDashboardPreviews);
}
private DashboardPreviewResponse createDashboardPreviewResponse(Dashboard dashboard) {
ApplyForm applyForm = applyFormService.findByDashboard(dashboard);
List<Applicant> allApplicants = getAllApplicantsByDashboardId(dashboard);
StatsResponse stats = calculateStats(allApplicants);
return new DashboardPreviewResponse(
dashboard.getId(),
applyForm.getId(),
applyForm.getTitle(),
stats,
applyForm.getStartDate(),
applyForm.getEndDate()
);
}
private List<DashboardPreviewResponse> sortDashboardPreviews(
List<DashboardPreviewResponse> dashboardResponses,
LocalDateTime currentTime
) {
List<DashboardPreviewResponse> nonExpiredDashboards = dashboardResponses.stream()
.filter(d -> d.endDate().isAfter(currentTime))
.sorted(Comparator.comparing(DashboardPreviewResponse::endDate)
.thenComparing(DashboardPreviewResponse::startDate))
.toList();
List<DashboardPreviewResponse> expiredDashboards = dashboardResponses.stream()
.filter(d -> d.endDate().isBefore(currentTime))
.sorted(Comparator.comparing(DashboardPreviewResponse::startDate))
.toList();
List<DashboardPreviewResponse> sortedDashboards = new ArrayList<>();
sortedDashboards.addAll(nonExpiredDashboards);
sortedDashboards.addAll(expiredDashboards);
return sortedDashboards;
}
private List<Applicant> getAllApplicantsByDashboardId(Dashboard dashboard) {
List<Process> processes = processService.findAllByDashboard(dashboard);
return processes.stream()
.flatMap(process -> applicantService.findAllByProcess(process)
.stream())
.toList();
}
private StatsResponse calculateStats(List<Applicant> allApplicants) {
int totalApplicants = allApplicants.size();
int totalFails = (int) allApplicants.stream()
.filter(Applicant::isRejected).count();
int totalAccepts = (int) allApplicants.stream()
.filter(Applicant::isNotRejected)
.filter(Applicant::isApproved)
.count();
int totalPending = totalApplicants - (totalFails + totalAccepts);
return new StatsResponse(totalAccepts, totalFails, totalPending, totalApplicants);
}
}