-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMainViewController.java
215 lines (187 loc) · 9.44 KB
/
MainViewController.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package com.oing.controller;
import com.oing.domain.*;
import com.oing.dto.response.*;
import com.oing.restapi.MainViewApi;
import com.oing.service.*;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.*;
/**
* no5ing-server
* User: CChuYong
* Date: 4/16/24
* Time: 4:03 PM
*/
@Controller
@RequiredArgsConstructor
public class MainViewController implements MainViewApi {
private final PostService postService;
private final MemberService memberService;
private final MemberPickService memberPickService;
private final MemberController memberController;
private final MemberBridge memberBridge;
private final MissionBridge missionBridge;
private final PostController postController;
private final FamilyBridge familyBridge;
private static final int PAGE_FETCH_SIZE = 1000;
@Override
public DaytimePageResponse getDaytimePage(
String loginMemberId
) {
String familyId = memberBridge.getFamilyIdByMemberId(loginMemberId);
LocalDate today = ZonedDateTime.now().toLocalDate();
Map<String, FamilyMemberProfileResponse> memberMap = new HashMap<>();
Page<FamilyMemberProfileResponse> members = memberService.getFamilyMembersProfilesByFamilyId(familyId, 1, PAGE_FETCH_SIZE);
members.forEach(member -> memberMap.put(member.memberId(), member));
Collection<Post> survivalPosts = postService.searchMemberPost(
1, PAGE_FETCH_SIZE, today, null, loginMemberId, familyId,
true, PostType.SURVIVAL
).results();
Collection<Post> missionPosts = postService.searchMemberPost(
1, PAGE_FETCH_SIZE, today, null, loginMemberId, familyId,
true, PostType.MISSION
).results();
Map<String, Integer> postUploaderRankMap = new HashMap<>();
int lastIdx = 1;
for (Post survivalPost : survivalPosts) {
postUploaderRankMap.put(survivalPost.getMemberId(), lastIdx++);
}
Comparator<FamilyMemberProfileResponse> comparator = Comparator
.comparing((FamilyMemberProfileResponse member) -> !member.memberId().equals(loginMemberId))
.thenComparing((FamilyMemberProfileResponse member) -> {
Integer rank = postUploaderRankMap.get(member.memberId());
return rank != null ? rank : Integer.MAX_VALUE;
});
Set<String> pickedSet = new HashSet<>();
memberPickService.getPickedMembers(familyId, loginMemberId)
.forEach(pick -> pickedSet.add(pick.getToMemberId()));
String todayMissionId = missionBridge.getTodayMissionId();
String dailyMissionContent = missionBridge.getContentByMissionId(todayMissionId);
boolean isMissionUnlocked = postController.getMissionAvailableStatus(loginMemberId, loginMemberId, familyId)
.isMissionUnlocked();
boolean isMeSurvivalUploadedToday = postController.getSurvivalUploadStatus(loginMemberId, loginMemberId, familyId)
.isMeSurvivalUploadedToday();
boolean isMeMissionUploadedToday = postController.getMissionUploadStatus(loginMemberId, loginMemberId, familyId)
.isMeMissionUploadedToday();
int leftUploadCountUntilMissionUnlock = postController.getRemainingSurvivalPostCount(loginMemberId, loginMemberId, familyId)
.leftUploadCountUntilMissionUnlock();
String familyName = familyBridge.findFamilyNameByFamilyId(familyId);
return new DaytimePageResponse(
members.stream().sorted(comparator).map((member) -> new MainPageTopBarResponse(
member.memberId(),
familyName,
member.imageUrl(),
String.valueOf(member.name().charAt(0)),
member.name(),
postUploaderRankMap.get(member.memberId()),
member.dayOfBirth().getMonth() == today.getMonth()
&& member.dayOfBirth().getDayOfMonth() == today.getDayOfMonth(),
!pickedSet.contains(member.memberId())
&& !member.memberId().equals(loginMemberId)
&& !postUploaderRankMap.containsKey(member.memberId())
)).toList(),
memberPickService.getPickMembers(familyId, loginMemberId).stream().map(pickMember -> {
FamilyMemberProfileResponse member = memberMap.get(pickMember.getFromMemberId());
if (member == null) {
return new MainPagePickerResponse(
pickMember.getFromMemberId(),
null,
null
);
}
return new MainPagePickerResponse(
member.memberId(),
member.imageUrl(),
member.name()
);
}).toList(),
leftUploadCountUntilMissionUnlock,
isMissionUnlocked,
isMeSurvivalUploadedToday,
isMeMissionUploadedToday,
dailyMissionContent,
survivalPosts.stream().map(post -> {
FamilyMemberProfileResponse member = memberMap.get(post.getMemberId());
return new MainPageFeedResponse(
post.getId(),
post.getPostImgUrl(),
member != null ? member.name() : null,
post.getCreatedAt().atZone(ZoneId.systemDefault())
);
}).toList(),
missionPosts.stream().map(post -> {
FamilyMemberProfileResponse member = memberMap.get(post.getMemberId());
return new MainPageFeedResponse(
post.getId(),
post.getPostImgUrl(),
member != null ? member.name() : null,
post.getCreatedAt().atZone(ZoneId.systemDefault())
);
}).toList()
);
}
@Override
public NighttimePageResponse getNighttimePage(String loginMemberId, String loginFamilyId) {
Page<FamilyMemberProfileResponse> members = memberService.getFamilyMembersProfilesByFamilyId(loginFamilyId, 1, PAGE_FETCH_SIZE);
LocalDate today = ZonedDateTime.now().toLocalDate();
String familyName = familyBridge.findFamilyNameByFamilyId(loginFamilyId);
List<MainPageTopBarResponse> mainPageTopBarResponses = members.stream().map((member) -> new MainPageTopBarResponse(
member.memberId(),
familyName,
member.imageUrl(),
String.valueOf(member.name().charAt(0)),
member.name(),
null,
member.dayOfBirth().getMonth() == today.getMonth()
&& member.dayOfBirth().getDayOfMonth() == today.getDayOfMonth(),
false
)).toList();
FamilyMemberMonthlyRankingResponse familyMemberMonthlyRanking = getFamilyMemberMonthlyRanking(loginMemberId, loginFamilyId);
return new NighttimePageResponse(
mainPageTopBarResponses,
familyMemberMonthlyRanking
);
}
@Override
public FamilyMemberMonthlyRankingResponse getFamilyMemberMonthlyRanking(String loginMemberId, String loginFamilyId) {
List<PostRankerResponse> ranking = postController.getFamilyMembersMonthlySurvivalRanking(loginFamilyId).results().stream().toList();
FamilyMemberRankerResponse first = null;
if (ranking.size() >= 1) {
first = getFamilyMemberRankerResponse(ranking.get(0), loginFamilyId);
}
FamilyMemberRankerResponse second = null;
if (ranking.size() >= 2) {
second = getFamilyMemberRankerResponse(ranking.get(1), loginFamilyId);
}
FamilyMemberRankerResponse third = null;
if (ranking.size() >= 3) {
third = getFamilyMemberRankerResponse(ranking.get(2), loginFamilyId);
}
LocalDate mostRecentSurvivalPostDate = null;
LocalDate startOfMonth = ZonedDateTime.now().withDayOfMonth(1).toLocalDate();
LocalDate tomorrow = ZonedDateTime.now().plusDays(1).toLocalDate();
PostResponse mostRecentPost = postController.findLatestPost(startOfMonth, tomorrow, PostType.SURVIVAL, loginFamilyId);
if (mostRecentPost != null) {
mostRecentSurvivalPostDate = mostRecentPost.createdAt().toLocalDate();
}
return new FamilyMemberMonthlyRankingResponse(
ZonedDateTime.now().getMonthValue(),
first,
second,
third,
mostRecentSurvivalPostDate
);
}
private FamilyMemberRankerResponse getFamilyMemberRankerResponse(PostRankerResponse postRankerResponse, String loginFamilyId) {
MemberResponse postRankerMember = memberController.getMember(postRankerResponse.memberId(), loginFamilyId);
return new FamilyMemberRankerResponse(
postRankerMember.imageUrl(),
postRankerMember.name(),
postRankerResponse.postCount()
);
}
}